C++ – Passing Windows handle into an Unmanaged C++ dll


An unmanaged C++ dll has an exported function, that takes an int type as window hanlde

void SetWindowHandle(int nHandle);

else where in the unmanaged dll code the int is casted to HWNDand is used properly.

And from the windows forms application, I set the handle as follows

_hHandle = this->Handle.ToInt32();

Where _hHandle is a private member inside the class. Am I getting the handle correctly ?. Seems like it is, but apparently the application doesn't give the desire output. I suspect the problem is with the handle.

PS: I have access to the unmanaged dll so I can make modification in there for any suggested changes.

Best Solution

On the face of it, there's nothing wrong. You haven't shown the whole .Net declaration. One thing to watch out for is that the calling conventions need to match. The usual calling convention for DLLs is stdcall, but that's not the default in C++. You have to ask for it, usually by using the WINAPI macro like you see in all the Windows headers.

Since you have access to both sides of the call, why don't you use the debugger to find out whether you're getting the handle correctly? Either set breakpoints and check the variables' values, or print the values to the debug console or a file and inspect the results.

Related Question