亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang CGO異常0x40010006

Golang CGO異常0x40010006

Go
qq_笑_17 2023-06-12 14:34:35
我正在嘗試使用 cgo 從 dll 運行函數。該庫是用 c++ 編寫的,所以我在這里創建了一個 C 頭文件,其中包含定義的函數和一個.cpp實現的文件。庫.h:#ifdef __cplusplusextern "C" {#endifvoid* LoadEngine(char*);#ifdef __cplusplus}#endif庫.cpp:#include <Engine.h> //the libraryvoid* LoadEngine(char *sn) {  Engine *e;  GetEngineObject(sn, &e); //function from the dll, here it fails  return (void*) e;}然后調用它:package main/*#include "lib.h"*/import "C"func main() {  e := C.LoadEngine(C.CString("foobar")  ...}我有go1.12 windows/amd64 和mingw-w64-posix-seh也試過mingw-w64-posix-sjlj, mingw-w64-win32-seh, mingw-w64-win32-sjlj, 但結果是一樣的, 或者根本不編譯用go buildand 編譯它:#cgo windows CFLAGS: -IC:/Engine/Inc#cgo windows CPPFLAGS: -IC:/Engine/Inc#cgo windows LDFLAGS: -LC:/Engine/Bin64 -lEngine -lEngineObj -lole32 -loleaut32 -luuid我也嘗試用 mingw 捕獲異常__try1,__except1但無論如何它都失敗了。嘗試了不同的 mingw 版本,使用-ldflags="-linkmode internal"但是這不會編譯并且-ldflags="-linkmode external"也會出現此異常。編輯:還嘗試從 C 程序調用LoadEngine函數 (from ),它工作正常。lib.h編譯lib.cpp為. g++_ lib.o并將它鏈接到test.c我剛剛調用LoadEnginejust frommain函數的地方。所以也許有什么不對勁怎么把庫鏈接到cgo?
查看完整描述

1 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

由于某些原因,在 win10+ 上它會提高DBG_PRINTEXCEPTION_C.?這是由OutputDebugStringW沒有調試器正在偵聽引起的。

我認為通常它會用微軟的__try 和 __except來完成,但在 MinGW 中只有 __try1 和 __except1,我認為它們只適用于 32 位系統(很少有相關文檔)

但我發現,你可以添加一個異常處理程序

所以現在 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;

}

因為使用該庫以加載引擎開始并以卸載它結束,并且引擎是一個單例,所以我剛剛將 also 設為PVOID handler全局單例并且AddVectoredContinueHandleris inLoadEngine和RemoveVectoredContinueHandleris in UnloadEngine。


查看完整回答
反對 回復 2023-06-12
  • 1 回答
  • 0 關注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號