2011-03-25 56 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