0

So I am working with a c# wrapper to a C++ library, and trying to add another function. I have created the function declaration in C# and it is working. But it only works once. When I try to run it the second time, the program hangs.

The interop definitions and declarations are at https://github.com/joshglenn/interception-cs/blob/master/kchordr/Program.cs

the code I am running is here: https://github.com/joshglenn/interception-cs/blob/master/kchordr/InterceptionDemoForm.cs

The function that runs fine the first time but hangs on the second launch is GetHardwareID().

My question is, how can I troubleshoot this? Does this appear to be a memory leak?

JayGee
  • 577
  • 6
  • 16

1 Answers1

1

to get the error code from the WinAPI call use the Marshal.GetLastWin32Error(); Also remember to decorate your call with "Set Last Error = true";

Here is an example i have for calling a popup on a taskbar icon :

[DllImport("shell32.dll",SetLastError=true)]
public static extern bool Shell_NotifyIcon(uint dwMessage, [In] ref NotifyIconData pnid);

usage:

//call your code like you usually call the method
bool callResult = Caller.Shell_NotifyIcon((uint)NotifyIconMessage.NIM_ADD, ref n);

//afther that call the GetLastError to get the error code
int errorCode = Marshal.GetLastWin32Error();

google the error code and see what it means

Alex Peta
  • 1,407
  • 1
  • 15
  • 26
  • Thanks for that. That will come in handy. It turns out my error was being caused by improper use of the underlying API. My function declarations were correct. Thanks again! – JayGee Mar 19 '14 at 16:19
  • It turns out my function declarations were okay. I was using the underlying API incorrectly. I am marking yours as the answer as this should help others troubleshoot similar issues. – JayGee Mar 19 '14 at 16:23