我做了很多搜索和測試,我一直無法得到這個工作正常。 我正在使用MVS Express 2013,編譯一個我希望從c#GUI調用的C++ win32 DLL。從C#程序調用c + + DLL,未處理的異常
在事物的C++端,我有一個我導出的函數,並且它傳遞了一個結構體。該結構最初包含兩個字符串,但傳遞已知大小的字符數組似乎更容易。
C++代碼:
struct runDetails{
char requestedRuntype[32];
char filename[32];
};
void __declspec(dllexport) WindowRecreatorCall(runDetails* incomingRunRequests);
C#代碼:
試圖重新創建STRUCT在經過:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
public struct runDetails{
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 32)]
public string requestedRuntype;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 32)]
public string filename;
};
設置動態DLL打包機:
class CallWindowRecreator
{
[DllImport("WindowRecreatorDLL.dll", EntryPoint = "WindowRecreatorCall", CharSet = CharSet.Unicode)]
public static extern void WindowRecreatorCall(ref runDetails runDetails);
};
對t的實際呼叫他DLL:
型「System.BadImageFormatException」未處理的異常發生在WindowRecreatorGUI.exe
:
runDetails testing;
testing.requestedRuntype = "Minimize";
testing.filename = "";
CallWindowRecreator.WindowRecreatorCall(ref testing);
因爲它是現在,我當我嘗試的DLL調用得到這個錯誤附加信息:嘗試加載格式不正確的程序。 (來自HRESULT的異常:0x8007000B)
我做了大量的搜索引擎和代碼更改,我學到了很多,但我無法弄清楚這一點。任何提示將非常感謝。
編輯:改變了代碼和錯誤收到
編輯2:我從任何CPU改變了C#程序具體X86,現在我得到這個錯誤:
類型的未處理的異常「 System.EntryPointNotFoundException'發生在WindowRecreatorGUI.exe中
附加信息:無法在DLL'WindowRecreatorDLL.dll'中找到名爲'WindowRecreatorCall'的入口點。
我已經添加了一個外部C {}周圍的C++函數:睡前
和編輯3。現在我收到此錯誤:
託管調試助手'PInvokeStackImbalance'在'C:\ Users \ Tom \ workspace \ WindowRecreatorGUI \ WindowRecreatorGUI \ bin \ x86 \ Debug \ WindowRecreatorGUI.vshost.exe'中檢測到問題。
附加信息:對PInvoke函數「WindowRecreatorGUI!WindowRecreatorGUI.CallWindowRecreator :: WindowRecreatorCall」的調用使堆棧不平衡。這很可能是因爲託管的PInvoke簽名與非託管目標籤名不匹配。檢查PInvoke簽名的調用約定和參數是否與目標非託管簽名相匹配。
哦,拍,好抓。好的,改變了,錯誤已經改變!我編輯了這篇文章以反映這一點。 – user2887053
@ user2887053:查看CallingConvention。 – SLaks