Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

C# Form Application in C++ DLL

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 "BaseNativeProxy.h"
#include "BaseTypes.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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...