将 c# windows 面板转换为 c HWND

Shi*_*mar 3 c# hwnd

我有一个接受 HWND 的 dll(dll 中的代码);

void VideoCapture::SetVideoWindow(HWND VidWind)
{
    VideoWindow = VidWind;
}
Run Code Online (Sandbox Code Playgroud)

我通过在引用中添加 dll 在示例 c#.net 应用程序中调用上述 dll,在 c#.net 中我有一个带有面板的表单,是否可以将该面板传递给 dll?我在 C# 中给出了如下代码

VidCapWrapper.ManagedVideoCapture cc = new VidCapWrapper.ManagedVideoCapture();

cc.SetVideoWindow( panel1);
Run Code Online (Sandbox Code Playgroud)

我收到如下错误:'错误 2 'VidCapWrapper.ManagedVideoCapture.SetVideoWindow(HWND__ )'的最佳重载方法匹配有一些无效参数 D:\DirectShow_Capture_GUI\DirectShow_Capture_GUI\Form1.cs 44 13 DirectShow_Capture_GUI 错误 3 参数 1:无法转换从“System.Windows.Forms.Panel”到“HWND__ ” D:\DirectShow_Capture_GUI\DirectShow_Capture_GUI\Form1.cs 44 32 DirectShow_Capture_GUI`

任何人都可以告诉我如何将面板传递给 dll,(任何示例都会很好)?(对不起,我对 .net 很陌生,但试图创建一个示例应用程序,它显示可用的设备,如集成网络摄像头......然后在 c#.net 表单面板上显示预览)

编辑:感谢@Blachshma 和@Hans Passant,现在我可以将c# windows 窗体的面板传递给我的c++ dll。

我将 dll 中的 func 更改为

void VideoCapture::SetVideoWindow(IntPtr windowHandle)
{
    VideoWindow = (HWND)windowHandle.ToPointer();
}
Run Code Online (Sandbox Code Playgroud)

在 c# 中,我将其称为 cc.SetVideoWindow(panel1.Handle);

Han*_*ant 5

您必须小心不要将像 HWND 这样的基本非托管类型暴露给 C# 代码。C# 编译器不允许您传递此类类型的值。这里正确的互操作类型是 IntPtr,它可以存储一个句柄值。所以让你的 C++/CLI 方法看起来像这样:

void VideoCapture::SetVideoWindow(IntPtr windowHandle)
{
    VideoWindow = (HWND)windowHandle.ToPointer();
}
Run Code Online (Sandbox Code Playgroud)

您现在可以简单地将 panel1.Handle 传递给方法,也是 IntPtr 类型。