这有什么问题吗?我似乎不知道如何改变它。请帮忙....!!!! 这是错误消息: 对 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);
最佳答案
我怀疑您需要添加调用约定:
[DllImport("TestDLL.dll",
EntryPoint = "Process",
CharSet = CharSet.Ansi,
CallingConvention=CallingConvention.Cdecl)]
关于c# - 我的 INTEROP 代码片段有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5435601/