views:

223

answers:

2

Hi,

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 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!

+1  A: 

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

SwDevMan81
This cannot be it, a pointer to the structure is passed.
Hans Passant
+2  A: 

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);
Hans Passant