Search code examples
c#.net.net-4.0msvcrt

in .Net 4: PInvokeStackImbalance Exception


I was using the strlen function from msvcrt.dll in a .Net 3.5 project. More specifically:

private unsafe static extern int strlen( byte *pByte );

After migrating to .NET 4.0, if I use this function it throws a PInvokeStackImbalance exception.

How can I import the .NET 3.5 msvcrt.dll or fix this exception?


Solution

  • I suspect that the problem is with the calling convention, you should be using Cdecl.

    [DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
    private unsafe static extern int strlen(byte* pByte);