C++ Call C# COM

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;
}

 

posted on 2020-05-16 15:06  jshchg  阅读(173)  评论(0编辑  收藏  举报

导航