如何调用从C / C ++ .NET程序集? [英] How do I call a .NET assembly from C/C++?

查看:262
本文介绍了如何调用从C / C ++ .NET程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我写一个应用程序在C ++和C#。我想写低电平部分C ++和C#编写高水平的逻辑。如何从我的C ++程序加载.NET程序集,并开始调用方法和访问我的C#类的属性?

解决方案

  [的Guid(123565C4-C5FA-4512-A560-1D47F9FDFA20)]
公共接口IConfig
{
    [DISPID(1)]
    字符串目标{获得; }

    [DISPID(2)]
    无效反序列化();

    [DISPID(3)]
    无效序列化();
}

[标记有ComVisible特性(真)
[的Guid(12AC8095-BD27-4de8-A30B-991940666927)]
[ClassInterface(ClassInterfaceType.None)
公共密封类配置:IConfig
{
    公共配置()
    {
    }

    公共字符串目的地
    {
    {返回; }
    }

    公共无效序列化()
    {
    }

    公共无效反序列化()
    {
    }
}
 

在这之后,你需要regasm您的程序集。 Regasm将增加必要的注册表项,使您的.NET组件被看作为一个COM组件。之后,你可以打电话给你的.NET组件在C ++中以同样的方式与任何其他COM组件。

Suppose I am writing an application in C++ and C#. I want to write the low level parts in C++ and write the high level logic in C#. How can I load a .NET assembly from my C++ program and start calling methods and accessing the properties of my C# classes?

解决方案

[Guid("123565C4-C5FA-4512-A560-1D47F9FDFA20")]
public interface IConfig
{
    [DispId(1)]
    string Destination{ get; }

    [DispId(2)]
    void Unserialize();

    [DispId(3)]
    void Serialize();
}

[ComVisible(true)]
[Guid("12AC8095-BD27-4de8-A30B-991940666927")]
[ClassInterface(ClassInterfaceType.None)]
public sealed class Config : IConfig
{
    public Config()
    {
    }

    public string Destination
    {
    	get { return ""; }
    }

    public void Serialize()
    {
    }

    public void Unserialize()
    {
    }
}

After that, you need to regasm your assembly. Regasm will add the necessary registry entries to allow your .NET component to be see as a COM Component. After, you can call your .NET Component in C++ in the same way as any other COM component.

这篇关于如何调用从C / C ++ .NET程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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