Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8706321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:35:15+00:00 2026-06-13T03:35:15+00:00

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

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:35:16+00:00Added an answer on June 13, 2026 at 3:35 am

    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:
    changing the default calling convention in 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .DLL that i include in my Visual Studio 2008 project. The
I have a WPF DLL being called from an unmanaged DLL. The WPF DLL
I have a C DLL being called by a C# app, exchanging data using
I have a COM visible .NET DLL file that is being used as an
I have a simple dll that is being injected into a target process using
I have a DLL that contains all my object I need to build my
I have a DLL that is written in C++ and I want to suppress
I have a DLL that I can use to pull the following information about
I have a DLL that's compiled, and I don't have the source code for
I have a dll that exports extern C __declspec(dllexport) int __stdcall Foo( void );

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.