Domanda

I have a dll which accepts HWND, (code in the dll);

void VideoCapture::SetVideoWindow(HWND VidWind)
{
    VideoWindow = VidWind;
}

i am calling the above dll in a sample c#.net application by adding the dll in references, in c#.net i have a form with Panel, is it possible to pass that panel to the dll? i gave code as below in c#

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

cc.SetVideoWindow( panel1);

i am getting errors as below: 'Error 2 The best overloaded method match for 'VidCapWrapper.ManagedVideoCapture.SetVideoWindow(HWND__)' has some invalid arguments D:\DirectShow_Capture_GUI\DirectShow_Capture_GUI\Form1.cs 44 13 DirectShow_Capture_GUI Error 3 Argument 1: cannot convert from 'System.Windows.Forms.Panel' to 'HWND__' D:\DirectShow_Capture_GUI\DirectShow_Capture_GUI\Form1.cs 44 32 DirectShow_Capture_GUI`

Can any one please tell me how to pass panel to dll, (any example will be good)? (sorry i am very new to .net, but trying to create a sample app which shows available devices like integrated webcam... and then shows preview on c#.net form panel)

EDIT: Thanks to Both @Blachshma and @Hans Passant, Now i am able to pass c# windows form's panel to my c++ dll.

I changed my func in dll as

void VideoCapture::SetVideoWindow(IntPtr windowHandle)
{
    VideoWindow = (HWND)windowHandle.ToPointer();
}

and in c# i am calling it as cc.SetVideoWindow(panel1.Handle);

È stato utile?

Soluzione

You must be careful to not expose fundamentally unmanaged types like HWND to C# code. The C# compiler will not allow you to pass a value of such a type. The proper interop type here is IntPtr, it can store a handle value. So make your C++/CLI method look like this:

void VideoCapture::SetVideoWindow(IntPtr windowHandle)
{
    VideoWindow = (HWND)windowHandle.ToPointer();
}

You can now simply pass panel1.Handle to the method, also of type IntPtr.

Altri suggerimenti

The panel has a Handle property you can use:

Panel p = new Panel();
IntPtr handle = p.Handle;

This should be enough if you set the SetVideoWindow signature to allow IntPtr i.e.

void VideoCapture::SetVideoWindow(IntPtr VidWind)

If you MUST use a HWND, you'll need to use an unsafe context, and cast it like this:

HWND hwnd=(HWND)this.Handle.ToPointer();

Here is an link with more information

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top