c# - 我的 INTEROP 代码片段有什么问题?

标签 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);

最佳答案

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

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

关于c# - 我的 INTEROP 代码片段有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5435601/

相关文章:

c# - 身份服务器 4 : Sorry, 出现错误:unauthorized_client

C# 反射 : Get info for all members of class and base classes

C#转义字符

c - 在 Netbeans 中处理单个 C 文件?

具有 1 个函数的 C++/CLI 库与 native C++ 静态链接

ms-word - Word Interop 在计划任务中不起作用

c# - 控制 MouseLeave 事件的问题

c - 不想立即删除已终止的子进程,需要成为僵尸进程

c - 从字符串中提取数字

c# - 通过 COM 将 C++ 中的复杂对象传递给 C#