将Windows句柄传递给非托管C ++ DLL

时间:2022-09-01 11:27:27

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

非托管C ++ dll有一个导出函数,它将int类型作为window hanlde

void SetWindowHandle(int nHandle);

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

否则在非托管DLL代码中int被转换为HWND并正确使用。

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

从windows窗体应用程序中,我按如下方式设置句柄

_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.

_hHandle是类中的私有成员。我能正确掌握手柄吗?看起来很像,但显然应用程序没有给出欲望输出。我怀疑问题出在手柄上。

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

PS:我可以访问非托管DLL,因此我可以在那里进行修改以进行任何建议的更改。

2 个解决方案

#1


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.

从表面上看,没有错。你还没有展示整个.Net声明。需要注意的一点是调用约定需要匹配。 DLL的通常调用约定是stdcall,但这不是C ++中的默认值。您必须要求它,通常使用WINAPI宏,就像在所有Windows标头中看到的那样。

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.

由于您可以访问呼叫的两端,为什么不使用调试器来确定您是否正确获得了句柄?设置断点并检查变量的值,或将值打印到调试控制台或文件并检查结果。

#2


Could the issue be related to different copies of the CRT library being used? http://msdn.microsoft.com/en-us/library/ms235460%28v=VS.90%29.aspx

该问题可能与正在使用的CRT库的不同副本有关吗? http://msdn.microsoft.com/en-us/library/ms235460%28v=VS.90%29.aspx

#1


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.

从表面上看,没有错。你还没有展示整个.Net声明。需要注意的一点是调用约定需要匹配。 DLL的通常调用约定是stdcall,但这不是C ++中的默认值。您必须要求它,通常使用WINAPI宏,就像在所有Windows标头中看到的那样。

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.

由于您可以访问呼叫的两端,为什么不使用调试器来确定您是否正确获得了句柄?设置断点并检查变量的值,或将值打印到调试控制台或文件并检查结果。

#2


Could the issue be related to different copies of the CRT library being used? http://msdn.microsoft.com/en-us/library/ms235460%28v=VS.90%29.aspx

该问题可能与正在使用的CRT库的不同副本有关吗? http://msdn.microsoft.com/en-us/library/ms235460%28v=VS.90%29.aspx