1

I have built a C# library that accesses a C dll. The C dll accesses an API that taps a communication bus. When a message is received on that bus, it generates an interrupt that runs a function (defined as a __cdecl pointer to a function in the C# library). I have a simple console application (C#) where this all works properly, however when I try to integrate this into the final application (also C#), I encounter PInvokeStackImbalance system exception. This is not present in my console application.

When I explicitly add CallingConvention=CallingConvention.Cdecl in my PInvoke call, the system exception is resolved, however the interrupt/callback is not tripped in both console and final applications. I have read up on calling conventions but cannot figure out where the disconnect between the console application and final application is. The console app was built to be an analog for the final application aimed at testing the libraries for integration. Where can I look to find out more information about the configs for differences between the two applications, and more importantly, how do I pass a stdcall pointer cleanly to a C library?

Edit: This is for x86 development.

BampyRocket
  • 96
  • 1
  • 11
  • 1
    Get a copy of Adam Nathan's _.NET and COM_. Despite the title, it's the Bible of P/Invoke – Flydog57 Oct 05 '22 at 13:27
  • @Flydog57 just bought it, at least one week to delivery. Thanks for the suggestion. – BampyRocket Oct 05 '22 at 13:34
  • It makes little sense that you don't get the MDA in the console app. Other than that you might have turned it off with Debug > Windows > Exception Settings. And the bitness of the process, CallingConvention is not needed and has no effect in 64-bit code. – Hans Passant Oct 05 '22 at 14:41

0 Answers0