开发者

Marshal structure pointer in VS2010

开发者 https://www.devze.com 2023-01-03 05:24 出处:网络
I have to call a C++ DLL from my C# program. I\'m trying to do it using PInvoke - everything works fine in VS2005\\ 2008, but after migration to VS 2010, I get this exception:

I have to call a C++ DLL from my C# program. I'm trying to do it using PInvoke - everything works fine in VS2005\ 2008, but after migration to VS 2010, I get this exception:

PInvokeStackImbalance was detected Message: A call to PInvoke function 'sampleFunc' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling 开发者_StackOverflow中文版convention and parameters of the PInvoke signature match the target unmanaged signature.

This is the original C++ prototype:

typedef struct {
    unsigned short field1;
    unsigned short field2;
} sInfo;

_declspec(dllexport) int sampleFunc(sInfo *info, char *txt);

and here is the C# code:

[StructLayout(LayoutKind.Sequential)]
    struct SInfo
    {
        //[MarshalAs(UnmanagedType.U1)] //also tried with the MarshalAs attr. Didn't help.
        public ushort field1;
        //[MarshalAs(UnmanagedType.U1)]
        public ushort field2;
    };
[DllImport("sampleModule.dll", CharSet=CharSet.Ansi)]
        public static extern int sampleFunc(ref SInfo info, [MarshalAs(UnmanagedType.LPStr)] string txt);

I've tried it also with IntPtr instead of the ref SInfo, but got the same result...

Any help will be appreciated,

Thank you all!


Hard to see how this could have worked before. The C++ declaration doesn't declare the calling convention, the default is __cdecl unless overridden in the C++ project with the /Gz compile option. You have to tell the P/Invoke marshaller:

    [DllImport("sampleModule.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    public static extern int sampleFunc(ref SInfo info, string txt);


This probably has to do with how your packing the struct. The default Pack size is 8, so its probably thinking you have too many bytes. Try setting the Pack size to 2 (16 bit aligned) and see if that helps:

[StructLayout(LayoutKind.Sequential, Pack=2)]

Alternatively you can specify the offsets like this:

[StructLayout(LayoutKind.Explicit)]
public struct struct1
{
   [FieldOffset(0)]
   public ushort a;   // 2 bytes
   [FieldOffset(2)]
   public ushort b;    // 2 bytes
}

Here is a good reference on packing

0

精彩评论

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