从非托管code加载混合模式汇编

问题描述:

正如标题所说,我想打电话给从非托管code混合模式的组装。

As the title says i want to call a mixed mode assembly from unmanaged code.

要更precise,我想动态加载混合模式程序集,然后执行一些静态的非托管启动code,它注册了一些托管C ++包装的C#code。

To be more precise, i want to load the mixed mode assembly dynamically and then execute some static unmanaged startup code that registers some Managed C++ Wrappers for C# Code.

这是可能的(或者我需要嵌入.NET运行时或使用COM?)?

Is this possible (or do i need to embed the .Net Runtime or use COM?) ?

有没有人已经做到了这一点,并可以分享一些经验?

Has anybody already done this and can share some experience?

PS:如果混合模式组件包括一个WPF窗口将它启动

PS: If the mixed mode assembly contains a WPF Window will it be started?

您需要让CLR加载和初始化。是的,使得管理类[标记有ComVisible特性]或托管的CLR自己与CorBindToRuntimeEx的()是一个办法做到这一点。一个非常简单的方法就是从您的DLL导出一个管理功能,C ++ / CLI编译器嵌入到code,它需要初始化CLR照顾一个thunk。很容易做到,但它并没有很好地扩展在接口到你的管理code为脂肪。

You need to get the CLR loaded and initialized. Yes, making a managed class [ComVisible] or hosting the CLR yourself with CorBindToRuntimeEx() is a way to do this. A very simple way is to export a managed function from your DLL, the C++/CLI compiler embeds a thunk in the code that takes care of initializing the CLR. Very easy to do but it does not scale well when the interface to your managed code is fat.

ref class Bootstrap
{
public:
    static void Initialize() { 
        // etc..
    }
};

extern "C" __declspec(dllexport) 
void __stdcall LoadAndInitialize()
{
    Bootstrap::Initialize();
}

您可以通过传递函数指针到您的原生界面美化。将其转换为与元帅一个托管委托:: GetDelegateForFunctionPointer()。不要忘了换任何本地声明使用#pragma如果你这样做管理。

You could embellish by passing a function pointer to your native interface. Convert it to a managed delegate with Marshal::GetDelegateForFunctionPointer(). Don't forget to wrap any native declarations with #pragma managed if you do this.