duai4379 2019-02-26 08:48
浏览 552
已采纳

Golang CGO异常0x40010006

Im trying to run a function from a dll using cgo. The library is in c++ so I created a C header here are the functions defined and a .cpp file where is the implementation.

lib.h:

#ifdef __cplusplus
extern "C" {
#endif

void* LoadEngine(char*);

#ifdef __cplusplus
}
#endif

lib.cpp:

#include <Engine.h> //the library

void* LoadEngine(char *sn) {
  Engine *e;
  GetEngineObject(sn, &e); //function from the dll, here it fails
  return (void*) e;
}

then calling it with:

package main

/*
#include "lib.h"
*/
import "C"

func main() {
  e := C.LoadEngine(C.CString("foobar")
  ...
}

I have go1.12 windows/amd64 and mingw-w64-posix-seh

tried also mingw-w64-posix-sjlj, mingw-w64-win32-seh, mingw-w64-win32-sjlj, but the result is the same, or it doesn't compile at all

compiling it just with go build and:

#cgo windows CFLAGS: -IC:/Engine/Inc
#cgo windows CPPFLAGS: -IC:/Engine/Inc
#cgo windows LDFLAGS: -LC:/Engine/Bin64 -lEngine -lEngineObj -lole32 -loleaut32 -luuid

On win8 it runs ok, but on win10+ it doesn't work, if the function gets to calling the GetEngineObject which is from the dll, it fails with:

Exception 0x40010006 0x1 0x8deb90 0x7ffbc9ada388
PC=0x7ffbc9ada388

runtime: unknown pc 0x7ffbc9ada388
stack: frame={sp:0x8dea80, fp:0x0} stack=[0x0,0x8dfdf0)
00000000008de980:  0000000002000002  000000002a080321
00000000008de990:  0000000090000191  0000000000000321
00000000008de9a0:  0000000500000008  0000000500000000
00000000008de9b0:  feeefeeefeeefeee  00000000024c0150
00000000008de9c0:  0000000000000000  0000000000000000
00000000008de9d0:  00000000024c0000  00007ffbccb730ac
00000000008de9e0:  0000000002680000  00000000024c1e30
00000000008de9f0:  00000000026a2d90  00000000026ad480
00000000008dea00:  0000000000000df1  0000000000000000
00000000008dea10:  0000000000000df1  0000000000000df1
00000000008dea20:  0000000000001bf8  0000000000000000
00000000008dea30:  0000000090000191  0000000000000003
00000000008dea40:  0000bcc3daf1f4cb  00000000026a2d00
00000000008dea50:  0000000000000000  000002fffb442d78
00000000008dea60:  0000000000000000  00000000008dfb00
00000000008dea70:  0000000000000020  00007ffbc9ada388
00000000008dea80: <00000000024c0000  000000002fbe1490
00000000008dea90:  00000000008df970  00000000ffffffff
00000000008deaa0:  0000000040010006  0000000000000000
00000000008deab0:  00007ffbc9ada388  0000000000000002
00000000008deac0:  0000000000000001  00000000008deb90
00000000008dead0:  00000000001b0150  0000000000800000
00000000008deae0:  00000000026ad480  0000000000000000
00000000008deaf0:  00000000001b10b0  0000000000690000
00000000008deb00:  00000000024c0000  00007ffbccbbcafa
00000000008deb10:  00000000024c0000  00007ffbccb76ff8
00000000008deb20:  00000000026a2d90  00000000026a9c30
00000000008deb30:  0000000000000000  0000000000000001
00000000008deb40:  00002c98037a65f6  000000000000001f
00000000008deb50:  00000000026a9430  000000002fd8a367
00000000008deb60:  0000000000006000  00000000024c0000
00000000008deb70:  0000000000000df1  0000000000000000
runtime: unknown pc 0x7ffbc9ada388
stack: frame={sp:0x8dea80, fp:0x0} stack=[0x0,0x8dfdf0)
00000000008de980:  0000000002000002  000000002a080321
00000000008de990:  0000000090000191  0000000000000321
00000000008de9a0:  0000000500000008  0000000500000000
00000000008de9b0:  feeefeeefeeefeee  00000000024c0150
00000000008de9c0:  0000000000000000  0000000000000000
00000000008de9d0:  00000000024c0000  00007ffbccb730ac
00000000008de9e0:  0000000002680000  00000000024c1e30
00000000008de9f0:  00000000026a2d90  00000000026ad480
00000000008dea00:  0000000000000df1  0000000000000000
00000000008dea10:  0000000000000df1  0000000000000df1
00000000008dea20:  0000000000001bf8  0000000000000000
00000000008dea30:  0000000090000191  0000000000000003
00000000008dea40:  0000bcc3daf1f4cb  00000000026a2d00
00000000008dea50:  0000000000000000  000002fffb442d78
00000000008dea60:  0000000000000000  00000000008dfb00
00000000008dea70:  0000000000000020  00007ffbc9ada388
00000000008dea80: <00000000024c0000  000000002fbe1490
00000000008dea90:  00000000008df970  00000000ffffffff
00000000008deaa0:  0000000040010006  0000000000000000
00000000008deab0:  00007ffbc9ada388  0000000000000002
00000000008deac0:  0000000000000001  00000000008deb90
00000000008dead0:  00000000001b0150  0000000000800000
00000000008deae0:  00000000026ad480  0000000000000000
00000000008deaf0:  00000000001b10b0  0000000000690000
00000000008deb00:  00000000024c0000  00007ffbccbbcafa
00000000008deb10:  00000000024c0000  00007ffbccb76ff8
00000000008deb20:  00000000026a2d90  00000000026a9c30
00000000008deb30:  0000000000000000  0000000000000001
00000000008deb40:  00002c98037a65f6  000000000000001f
00000000008deb50:  00000000026a9430  000000002fd8a367
00000000008deb60:  0000000000006000  00000000024c0000
00000000008deb70:  0000000000000df1  0000000000000000

goroutine 1 [syscall]:
path/to/package._Cfunc_LoadEngine(0x23a2c20, 0x0)
        _cgo_gotypes.go:518 +0x51
path/to/package.Load(0x4d05ad, 0x1d, 0x0, 0x0, 0x0)
        C:/Users/microo8/Documents/workspace/src/path/to/package/fre.go:55 +0x89
main.main()
        W:/Workspace/src/path/to/package/test/test.go:12 +0x41
rax     0x3e00003e
rbx     0x2fbe1490
rcx     0xfffffffffffffffe
rdi     0xffffffff
rsi     0x8df970
rbp     0x20
rsp     0x8dea80
r8      0x2fca2793
r9      0x3a0f4c0
r10     0x2fca2701
r11     0x26aadb0
r12     0x8dfb00
r13     0x0
r14     0x2fffb442d78
r15     0x0
rip     0x7ffbc9ada388
rflags  0x202
cs      0x33
fs      0x53
gs      0x2b

I've also tried to catch the exception with mingw's __try1 and __except1 but it fails anyways. Tried different mingw versions, using -ldflags="-linkmode internal" but this doesn't compile and -ldflags="-linkmode external" makes also this exception.

EDIT: also tried to call the LoadEngine function (from lib.h) from a C program and it works fine. Compiled the lib.cpp with g++ to lib.o. And linked it to test.c where I just called the LoadEngine just from main function. So maybe there is something wrong how go links the library to cgo?

  • 写回答

1条回答 默认 最新

  • doukan6564 2019-02-27 12:05
    关注

    For some reason on win10+ it would raise the DBG_PRINTEXCEPTION_C. Which is caused by the OutputDebugStringW and no debugger is listening.

    I think normally it would be done with microsoft's __try and __except, but in MinGW is just __try1 and __except1, which I think only works on 32bit systems (very little documentation is to that)

    But I found, that you can add a Exception Handler

    so now lib.cpp:

    #include <Engine.h> //the library
    #ifdef _WIN32
    #include <windows.h>
    
    LONG WINAPI VectoredHandler(struct _EXCEPTION_POINTERS *ExceptionInfo) {
       UNREFERENCED_PARAMETER(ExceptionInfo);  
       return EXCEPTION_CONTINUE_EXECUTION;  //just continue
    }
    #endif
    
    
    void* LoadEngine(char *sn) {
    #ifdef _WIN32
      PVOID handler = AddVectoredContinueHandler(1, VectoredHandler);
    #endif
      Engine *e;
      GetEngineObject(sn, &e);
    #ifdef _WIN32
      RemoveVectoredContinueHandler(handler); 
    #endif
      return (void*) e;
    }
    

    Because using the library starts with loading the engine and ends with unloading it, and the engine is a singleton, I've just made the PVOID handler also a global singleton and the AddVectoredContinueHandler is in LoadEngine and RemoveVectoredContinueHandler is in UnloadEngine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理
  • ¥50 webrtc-streamer TCP rtsp
  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计