简体   繁体   English

C++ DLL 中的 C# 表单应用程序

[英]C# Form Application in C++ DLL

I have a Windows Form application that I developed in C# to do a webcam capture.我有一个用 C# 开发的 Windows 窗体应用程序来执行网络摄像头捕获。 I need to call this C# form from an application development environment known as Magic (don't ask why).我需要从一个名为 Magic 的应用程序开发环境中调用这个 C# 表单(不要问为什么)。

If all i had to do was call a .exe I would be fine, but for complicated reason I'm not allowed to use .exe's in the production environment;如果我只需要调用 .exe 就好了,但由于复杂的原因,我不允许在生产环境中使用 .exe; therefor I need to have a DLL that can be loaded into the application.因此,我需要一个可以加载到应用程序中的 DLL。 I can't use a C# DLL because it doesn't have a .def file like in C++ that exposes it's functions (if you can solve this for me then no need to go further).我不能使用 C# DLL,因为它没有像在 C++ 中那样公开它的函数的 .def 文件(如果你可以为我解决这个问题,那么就不需要再进一步了)。 So therefor I need to find a way to create a C++ wrapper, that will provide the ability to pass data between the application and the C# windows form/DLL.因此,我需要找到一种方法来创建 C++ 包装器,这将提供在应用程序和 C# windows 窗体/DLL 之间传递数据的能力。

Most things I've read on here suggest to register it as a COM object but I can't do that because this will be deployed to 100+ machines and registering it for each individual one wont fly with our systems admin.我在这里读到的大多数内容都建议将其注册为 COM 对象,但我不能这样做,因为这将部署到 100 多台机器,并为每一台机器注册它,而我们的系统管理员不会使用它。 Can anyone please help provide a guide or suggest which route I should start investigating?任何人都可以帮忙提供指南或建议我应该开始调查哪条路线吗? I've tried multiple methods that have all failed and I'm really looking for some help and guidance here.我尝试了多种方法,但都失败了,我真的在这里寻求帮助和指导。

What I understand of your problem is you need to show a form within a managed C# DLL called from a native C++ DLL.我对您的问题的理解是,您需要在从本机 C++ DLL 调用的托管 C# DLL 中显示一个表单。 This can be done, I have used this in some of my projects.这是可以做到的,我已经在我的一些项目中使用了它。 I have created a quick C++ console application (same code can be place in a DLL), project source code CallManagedFromNative .我创建了一个快速的 C++ 控制台应用程序(相同的代码可以放在 DLL 中),项目源代码CallManagedFromNative

The other solution is to create a native C++ project that can capture webcam data, say using Microsoft Media Foundation, if this is of interest the source code can be found at MediaFoundation .另一种解决方案是创建一个可以捕获网络摄像头数据的本机 C++ 项目,例如使用 Microsoft Media Foundation,如果对此感兴趣,可以在MediaFoundation上找到源代码。

Anyway back to the native C++ calling managed C# form sample.无论如何回到本机 C++ 调用托管 C# 表单示例。

#include "stdafx.h"
#include <iostream>

#include "Base\NativeProxy.h"
#include "Base\Types.h"

using namespace Nequeo::System::Any;

int main()
{
    std::vector<boost::any> param;
    param.push_back(3);
    boost::any returnData;

    Nequeo::NativeProxy managedProxy(L"ClassLibraryManaged.dll", L"ClassLibraryManaged.Class1");
    managedProxy.executeManaged(L"OpenForm", param, returnData);

    int retFromCall = boost::any_cast<int>(returnData);
    std::cout << retFromCall;        
    return 0;
}

Specify the managed DLL, the namespace and class name.指定托管 DLL、命名空间和类名。 Now call a method passing parameters and optionally a return value.现在调用传递参数和可选返回值的方法。 The code in the managed DLL:托管DLL中的代码:

namespace ClassLibraryManaged
{
    public class Class1
    {
        public Class1() { }

        public int OpenForm(int a)
        {
            TestForm form = new TestForm();
            form.ShowDialog();
            return a * a;
        }
    }
}

The sample project contains all the includes, bins and libs you will need to test your project, the only thing you will need is boost I used version 161 for this project you can use your own build or you can download my build from BoostBuild161示例项目包含测试项目所需的所有包含、bin 和库,您唯一需要的是boost我为此项目使用了 161 版,您可以使用自己的构建,也可以从BoostBuild161下载我的构建

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

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