我做了很多搜索和测试,我一直无法得到这个工作正常。 我正在使用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