C#中的C#表单应用程序 [英] C# Form Application in C++ DLL

查看:415
本文介绍了C#中的C#表单应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows Form应用程序,我在C#中开发了一个网络摄像头捕获。我需要从名为Magic的应用程序开发环境(不要问为什么)调用这个C#表单。



如果我所要做的只是调用一个.exe我会很好,但由于复杂的原因,我不允许在生产环境中使用.exe。因此,我需要一个可以加载到应用程序中的DLL。我不能使用C#DLL,因为它没有像C ++那样暴露其功能的.def文件(如果可以为我解决这个问题,那么不用再进一步)。所以我需要找到一种创建一个C ++包装器的方法,它将提供在应用程序和C#窗体/ DLL之间传递数据的功能。



我已经在这里阅读建议将其注册为COM对象,但我无法做到这一点,因为这将被部署到100多台机器,并为每个个人注册,不会与我们的系统管理员一起飞行。任何人都可以帮助提供指导或建议我应该开始调查哪条路线?我尝试了多种方法都失败了,我真的在寻找一些帮助和指导。

解决方案

了解您的问题是您需要显示从本机C ++ DLL调用的托管C#DLL中的表单。这可以做到,我在我的一些项目中使用了这个。我创建了一个快速的C ++控制台应用程序(相同的代码可以放在一个DLL中),项目源代码 CallManagedFromNative



另一个解决方案是创建一个可以捕获网络摄像头数据的本地C ++项目,比如使用Microsoft Media Foundation,如果这是有意义的源代码可以在 MediaFoundation 找到。



无论如何回到本机C ++调用托管的C#表单示例。

  #includestdafx.h
#包括< iostream>

#includeBase\NativeProxy.h
#includeBase\Types.h

使用命名空间Nequeo :: System :: Any;

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

Nequeo :: NativeProxy managedProxy(LClassLibraryManaged.dll,LClassLibraryManaged.Class1);
managedProxy.executeManaged(LOpenForm,param,returnData);

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

指定托管DLL,命名空间和类名。现在调用一个传递参数的方法和可选的返回值。托管DLL中的代码:

 命名空间ClassLibraryManaged 
{
public class Class1
{
public Class1(){}

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

示例项目包含所有包括您需要测试项目的bin和lib,唯一需要的是 boost 我使用版本161进行此项目,您可以使用自己的构建,也可以从 BoostBuild161 下载我的构建


I have a Windows Form application that I developed in C# to do a webcam capture. I need to call this C# form from an application development environment known as Magic (don't ask why).

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; therefor I need to have a DLL that can be loaded into the application. 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). 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.

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

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.

Anyway back to the native C++ calling managed C# form sample.

#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. Now call a method passing parameters and optionally a return value. The code in the managed 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

这篇关于C#中的C#表单应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆