开发者

What is wrong with my INTEROP snippet?

开发者 https://www.devze.com 2023-02-20 08:22 出处:网络
What\'s wrong with this? I can\'t seem to figure out how to change it. Please help....!!!! Here\'s the error message:

What's wrong with this? I can't seem to figure out how to change it. Please help....!!!! Here's the error message: A call to PInvoke function 'MyClassName::Process' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.


#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  Pro开发者_运维技巧cess(lpUnmagedStruct lpStruct, int size);

And here is my .NET codes:


[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);


I suspect you need to add the calling convention:

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

精彩评论

暂无评论...
验证码 换一张
取 消