简体   繁体   English

如何从C#将窗口句柄传递给C ++ Win32 dll

[英]how to pass window handle to c++ win32 dll from C#

I have one project which capture images from multiple cameras which is in C++ .I want to use that project in my new project which is in C#.I had made dll of that project.My question is ,how can i use that dll in my project.I know by passing window handle to C++ dll we can use it but i dont know how to do it and what changes should i make in dll. 我有一个从C ++的多个摄像机捕获图像的项目。我想在C#的新项目中使用该项目。我已经创建了该项目的dll。我的问题是,如何在我的dll中使用该dll? project.I知道通过将窗口句柄传递给C ++ dll,我们可以使用它,但是我不知道该怎么做以及我应该在dll中进行哪些更改。

Please forgive ,if it is foolish question. 如果是愚蠢的问题,请原谅。

I had the exact problem as you and this article helped me. 您遇到了确切的问题,本文对我有所帮助。 http://blogs.msdn.com/b/jonathanswift/archive/2006/10/03/dynamically-calling-an-unmanaged-dll-from-.net-_2800_c_23002900_.aspx http://blogs.msdn.com/b/jonathanswift/archive/2006/10/03/dynamically-calling-an-unmanaged-dll-from-.net-_2800_c_23002900_.aspx

To pass the handle, you can add another function in C++ end. 要传递句柄,可以在C ++端添加另一个函数。 Something like, 就像是,

(in the header) (在标题中)
extern "C" __declspec(dllexport) void SetParent(HWND hWnd);

To use the SetParent in C# 在C#中使用SetParent

 class Program
 {
    ...

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate void SetParent(IntPtr hWnd);

    public void testMethod()
    {
           ...
           IntPtr getAddress = NativeMethods.GetProcAddress(pDll, "SetParent");
           ...
           SetParent setParent = (SetParent)Marshal.GetDelegateForFunctionPointer(getAddress, typeof(SetParent));
           setParent(this.Handle);
           ...       
    }
 } 

If your C++ project is already in a DLL you can call any functions that it exports using P/Invoke . 如果您的C ++项目已经在DLL中,则可以使用P / Invoke调用它导出的任何函数。 You will have to declare the method in your C# assembly as shown below, but then you can call it like a standard static method. 您将必须在C#程序集中声明该方法,如下所示,但是您可以像标准静态方法一样对其进行调用。

[DllImport("YourDllsName.dll")]
public static extern bool FunctionThatAWindowHandleAndReturnsBool(IntPtr hWnd);

Depending on the types of parameters that your C++ DLL's functions take the marshaling of the .Net datatypes to C data types can get complicated. 根据C ++ DLL函数的参数类型,将.Net数据类型编组为C数据类型会变得很复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM