2011-03-25 58 views
0

这是怎么回事?我似乎无法弄清楚如何改变它。请帮忙....!!!! 以下是错误消息: 对PInvoke函数“MyClassName :: Process”的调用使堆栈不平衡。这很可能是因为托管的PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名相匹配。我的INTEROP片段有什么问题?

 

#include "stdafx.h" 
#include "TestDll.h" 
extern "C" __declspec(dllexport) void Process(lpUnmagedStruct lpStruct, int size) 
{ 
    lpStruct[0].a = 0; 
    lpStruct[0].b = 0; 
    lpStruct[1].a = 1; 
    lpStruct[1].b = 1; 
} 
typedef struct 
{ 
    double a; 
    double b; 
}UnmanagedStruct, far *lpUnmagedStruct; 

extern "C" __declspec(dllexport) void Process(lpUnmagedStruct lpStruct, int size); 
 

这里是我的.NET代码:

 

[DllImport("TestDLL.dll", EntryPoint = "Process", CharSet = CharSet.Ansi)] 
internal static extern void Process([In, Out] ManagedStruct[] aStruct, int size); 

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 
public class ManagedStruct 
{ 
    public double a; 
    public double b; 
} 

const int size = 3; 
ManagedStruct[] aStruct = new ManagedStruct[size]; 
Process(aStruct, size); 
 

回答

1

我怀疑你需要添加调用约定:

[DllImport("TestDLL.dll", 
     EntryPoint = "Process", 
     CharSet = CharSet.Ansi, 
     CallingConvention=CallingConvention.Cdecl)] 
+0

我不能相信这只是这么简单!谢谢! – TexasCoder 2011-03-25 18:53:35