views:

787

answers:

5

After switching to VS2010, the managed debug assistant is displaying an error about an unbalanced stack from a call to an unmanaged C++ function from a C# application.

The usuals suspects don't seem to be causing the issue. Is there something else I should check? The VS2008 built C++ dll and C# application never had a problem, no weird or mysterious bugs - yeah, I know that doesn't mean much.

Here are the things that were checked:

  • The dll name is correct.
  • The entry point name is correct and has been verified with depends.exe - the code has to use the mangled name and it does.
  • The calling convention is correct.
  • The sizes and types all seem to be correct.
  • The character set is correct.
  • There doesn't seem to be any issues after ignoring the error and there isn't an issue when running outside the debugger.

C#:

[DllImport("Correct.dll", EntryPoint = "SuperSpecialOpenFileFunc", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern short SuperSpecialOpenFileFunc(ref SuperSpecialStruct stuff);

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct SuperSpecialStruct
{
   public int field1;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
   public string field2;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
   public string field3;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
   public string field4;
   public ushort field5;
   public ushort field6;
   public ushort field7;
   public short field8;
   public short field9;
   public uint field10;
   public short field11;
};

C++:

short SuperSpecialOpenFileFunc(SuperSpecialStruct * stuff);

struct SuperSpecialStruct
{
   int               field1;
   char              field2[256];
   char              field3[20];
   char              field4[10];
   unsigned short    field5;
   unsigned short    field6;
   unsigned short    field7;
   short             field8;
   short             field9;
   unsigned int      field10;
   short             field11;
};

Here is the error:

Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'Managed application path'.

Additional Information: A call to PInvoke function 'SuperSpecialOpenFileFunc' 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.

+2  A: 

You specify no padding in your C# declaration of the struct, but not in the C++ version. Since you are mixing char arrays that are not all multiples of four and an odd number of 2 byte shorts, the compiler is probably inserting padding within the struct and add the end.

Try wrapping the struct in a #pragma pack to ensure no padding.

#pragma pack(push)
#pragma pack(1)

// The struct

#pragma pack(pop)
Michael
Yeah, the #pragma pack should be put in regardless but I don't think its that either. You can't see it but the C# app passes the size of its struct, calculated with Marshal.Sizeof, to the C++ dll and the C++ dll verifies that the C++ struct size, calculated by sizeof(), is the same as the value passed in.
Just to be sure, I tried using #pragma pack without success.
+2  A: 

You specify stdcall in C# but not in C++, a mismatch here will lead to both the function and the caller popping arguments off of the stack.

On the other hand there is a compiler switch that will turn on stdcall as the default calling convention, (-Gz) are you using that?

Or try this in your C++

short __stdcall SuperSpecialOpenFileFunc(SuperSpecialStruct * stuff);
John Knoeller
+1  A: 

Hi Phill,

Have you managed to resolve the problem? I am about to develop an app in VS2010 that would use a legacy C++ dll and did some basic testing - with string types, something like this:

C++:

extern "C" __declspec(dllexport) LPCTSTR lazeisha(LPCTSTR input)
{
      return input;
}

C#:

namespace CPlusPlusFromDotNet
{
    class Program
    {
        [DllImport("lazeisha.dll", EntryPoint = "lazeisha")]
        public static extern String lazeisha(String s);

        static void Main(string[] args)
        {
            String test = lazeisha("Test string");
            Console.WriteLine(test);
        }
    }
}

Now this works perfectly well in VS2008 but in VS2010 I get the PInvokeStackImbalance problem. It also only shows in the debug mode. I've found a comment on one of the forums about an app that became unstable due to the same error. I would rather not start on the project if this is a real issue..

Many thanks in advance, Jelena

Jelena
If you don't specify the calling convention, P/Invoke assumes StdCall. However, for C/C++-code, the standard calling convention is Cdecl (see C/C++ settings of the project). So either use __stdcall on your C++ function or declare CallingConvention = CallingConvention.Cdecl on your DllImport.
Daniel Rose
+1 to Daniel Rose for solving my problem. It would be nice if this was a separate question so this could be marked as the correct answer.
herbrandson
A: 

Had the same problem as described - unmanaged C++ app that has worked perfectly for years. When we upgraded to VS2010, we started getting PInvokeStackUnbalanced messages.

adding "__stdcall" to the C++ signature as described above made the issue go away.

+1  A: 

As mentioned by Dane Rose comment above, either use __stdcall on your C++ function or declare CallingConvention = CallingConvention.Cdecl on your DllImport.

This is the answer that solves my problem.

Ngu Soon Hui