我的INTEROP片段有什么问题?

时间:2011-03-25 16:55:57

标签: c# c interop

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


#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 个答案:

答案 0 :(得分:1)

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

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