简体   繁体   中英

Calling C++ DLL from C# within VS fails. Run exe outside VS and it works

I have a C++ DLL that is being called like below from a C# console app.

When run in Visual Studio to debug it, it throws an exception saying the stack is unstable and to check that the method arguments are correct. However, if I run the *.exe outside of VS from Windows Explorer it retuns data to the screen as expected.

How can I get this to run within Visual Studio?

Thanks

**From the C++ header file:**
#ifdef RFIDSVRCONNECT_EXPORTS
#define RFID_CONN_API __declspec(dllexport)
#else
#define RFID_CONN_API __declspec(dllimport)
#endif

RFID_CONN_API BSTR rscListDevices( long showall ) ;


[DllImport("MyDLL.dll")]
[return: MarshalAs(UnmanagedType.BStr)]
public static extern string rscListDevices(int showall);

static void Main(string[] args)
{
  string data= rscListDevices(0);
  Console.WriteLine(data);
  Console.ReadKey();
}

Firstly, make sure you're using the same calling convention in both C++ and C# .

I suspect that the /Gd compiler option is set (since it is set by default), so __cdecl is used as default calling convention for unmarked functions.

You can fix crashes by either specifing the same calling convention in your C# code:

[DllImport("MyDLL.dll", CallingConvention=CallingConvention.Cdecl))]
[return: MarshalAs(UnmanagedType.BStr)]
public static extern string rscListDevices(int showall);

Or changing rscListDevices 's calling convention to __stdcall (which is the default in C#):

RFID_CONN_API BSTR __stdcall rscListDevices( long showall ) ;

You can also set __stdcall as the default calling convention for the unmarked functions in your C++ DLL by changing compiler option from /Gd to /Gz in manually or using the Project Properties dialog: 在Visual Studio中更改默认调用约定

But if you really want to disable MDA, you can go Debug->Exceptions and uncheck Managed Debugging Assistance.

You can read more about pInvokeStackImbalance and MDA here and here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM