110,571
社区成员
发帖
与我相关
我的任务
分享
// Hello.cs
// 1. Compile with: csc.exe /t:library Hello.cs
// 2. Register with: regasm.exe /tlb /codebase Hello.dll
using System;
using System.Runtime.InteropServices;
[assembly: ComVisible(true)]
[assembly: System.Reflection.AssemblyVersion("1.0.0.0")]
[assembly: Guid("2b31035b-7575-4980-8ba7-111111111111")]
[Guid("2b31035b-7575-4980-8ba7-333333333333")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IHello // expose as COM interface
{
[DispId(1)] void SayHello();
}
[Guid("2b31035b-7575-4980-8ba7-222222222222")]
[ClassInterface(ClassInterfaceType.None)]
public class Program : IHello
{
public void SayHello()
{
System.Windows.Forms.MessageBox.Show("Hello from .net");
}
}
// CallHello.cpp : Defines the entry point for the console application.
// 3. Compile with: cl.exe CallHello.cpp
#include <windows.h>
#include <objbase.h>
#import "Hello.tlb" raw_interfaces_only
int main(int argc, char* argv[])
{
CoInitialize(NULL);
printf("Callint .net class as COM (showing a dialog box)");
Hello::IHelloPtr iHello;
HRESULT hr = iHello.CreateInstance( __uuidof(Hello::Program) );
if(SUCCEEDED(hr))
{
iHello->SayHello(); // call .net interface via COM
}
CoUninitialize();
return 0;
}