0

I have a c++ function to be exported as a .dll to use:

static __declspec(dllexport) void DiagEncrypt(
    UCHAR              * ptrDataByte, 
    size_t               numBytes, 
    ACCESS_LEVEL         accLevel);
void DiagnosticCrypto::DiagEncrypt(UCHAR * ptrDataByte, size_t numBytes, ACCESS_LEVEL accLevel)

And I import it in my C# program:

[DllImport("DiagnosticCryptoDll.dll", EntryPoint = "?DiagEncrypt@DiagnosticCrypto@DiagnosticCryptoDll@@SAXPAEIW4ACCESS_LEVEL@12@@Z", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
static extern void DiagEncrypt(
    //[MarshalAs(UnmanagedType.LPArray)] byte[] data,
    IntPtr  data,
    uint    numBytes, //UintPtr numBytes,
    ACCESS_LEVEL accLevel);

when I execute it, there's an error of unbalanced stack.

Can somebody help me to figure out where the error is?

The marked part is what I have tried but it failed.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621

1 Answers1

0

Your C++ code uses the cdecl calling convention but your p/invoke uses stdcall. Change or the other to match.

For example, you could change the p/invoke to use CallingConvention.Cdecl.

The other comments I would make are:

  • It may be preferable to use extern "C" to suppress C++ name mangling. Although in the comments, Hans argues for mangling.
  • You've mapped size_t to uint. That will work fine on 32 bit, but not on 64 bit, where size_t is 64 bits wide. I suggest that you use UIntPtr here.
  • Are you quite sure that the function calls SetLastError. It's not a Windows function so I'd guess it does not.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I change the calling convention from stdcall to cdecl, and it works. And I also get to use UintPtr; the extern "C" is new to me, I need to figure out how to use it. Thank you very much! – Eugenia Wu Apr 16 '13 at 09:28
  • @Dave - please don't recommend extern "C". The mangling provides a very good diagnostic when the C++ programmer changes his function and the C# programmer has moved on. – Hans Passant Apr 16 '13 at 10:09
  • @Hans I see your point. I find mangled names untidy, but that should not matter. Of course, exporting mangled names ties you to a specific compiler. Not much good if you want to support multiple compilers. – David Heffernan Apr 16 '13 at 13:36