views:

505

answers:

1

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();
m_pViewer->SetWindowHandle(_hHandle);

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.

+1  A: 

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.

Rob Kennedy
Winforms app is also is in managed C++, the wrapper for the unmanaged C++ is included as a separate class header/cpp with in the winforms project.
rptony
Yeah I get the handle (well an integer) inside the dll too; but don't know if that is a right handle. Well, let me call GetDC on that handle, if it returns correctly then it is a valid handle right ?
rptony
To find out whether a value is a window handle, call IsWindow on it.
Rob Kennedy