首页 > 编程语言 > 详细

C++ Call C# COM

时间:2020-05-16 15:38:34      阅读:51      评论:0      收藏:0      [点我收藏+]

1、C# COM:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MyInterop
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    /// 
   [Guid("CE8B0E97-2D79-442B-8BA5-251E6ADB8C64")]
    public interface IMyDotNetInterface
    {
        void ShowDialog();
    }

    [ClassInterface(ClassInterfaceType.None)]
    [Guid("4B1E3C7C-A85C-4338-915F-3789B99B7F6D")]
    public class MyDotNetClass : IMyDotNetInterface
    {
        public void ShowDialog()
        {
            MessageBox.Show("I am a  Managed DotNET C# COM Object Dialog");
        }
    }
}

2、C++ call

// Cplusplus_console_call_com.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <objbase.h>
#import "MyInterop.tlb" named_guids raw_interfaces_only


int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL);
    MyInterop::IMyDotNetInterfacePtr pDotNetCOMPtr;

    // e.g. CreateInstance (<namespace::CLSID_<ClassName>)
    HRESULT hRes = pDotNetCOMPtr.CreateInstance(MyInterop::CLSID_MyDotNetClass);
    if (hRes == S_OK)
    {
        pDotNetCOMPtr->ShowDialog();
    }

    CoUninitialize();
    return 0;
}

技术分享图片

 

C++ Call C# COM

原文:https://www.cnblogs.com/jshchg/p/12900598.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!