文档库 最新最全的文档下载
当前位置:文档库 › c++调用c# com 文档说明

c++调用c# com 文档说明

一 在c# 制作com 说明
在VS2008中C#编写的COM组件
(1)VS2008中使用C#编写COM组件
建立C#编写的COM组件,项目类型为类库
配置:右键点击解决方案资源管理器中的CSharpComInterface,选择“属性”,选择“生成”,选择“为COM Interop注册(_P)”
打开AssemblyInfo.cs文件,设置[assembly: ComVisible(true)]
这用就可以生成CSharpComInterface.tlb文件
程序代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;-- 编写com 需要应用此命名空间
using CSharpComForVCDemo;
namespace CSharpComInterface
{

#region
// com 接口
// {8F6BD6AA-E2F3-4504-8DDC-191C5B5A546A}
// DEFINE_GUID(<>,
// 0x8f6bd6aa, 0xe2f3, 0x4504, 0x8d, 0xdc, 0x19, 0x1c, 0x5b, 0x5a, 0x54, 0x6a);

#endregion
[Guid("8F6BD6AA-E2F3-4504-8DDC-191C5B5A546A")]
public interface IComInterface
{
[DispId(1)]
string ComInterfaceDemoMessage(string str);
[DispId(2)]
int ComInterfaceDemoAdd();
[DispId(3)]
int ComInterfaceDemoRemove();
[DispId(4)]
float ComInterfaceDemoAddF();
[DispId(5)]
float ComInterfaceDemoRemoveF();
[DispId(6)]
object ComInterfaceDemoGetList();
[DispId(7)]
MyTestDemo ComInterfaceDemoTestDemo();
[DispId(8)]
string[] ComInterfaceGetStringList();

}
#region
// com 事件 接口
// {C3D879BE-8631-4bf5-8292-B96CDCB4D965}
// DEFINE_GUID(<>,
// 0xc3d879be, 0x8631, 0x4bf5, 0x82, 0x92, 0xb9, 0x6c, 0xdc, 0xb4, 0xd9, 0x65);

#endregion
[Guid("C3D879BE-8631-4bf5-8292-B96CDCB4D965"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IComInterfaceEvent
{

}
#region
// 接口 实现
// {E3CF2CAA-A38F-4865-9003-CC16D300A337}
// DEFINE_GUID(<>,
// 0xe3cf2caa, 0xa38f, 0x4865, 0x90, 0x3, 0xcc, 0x16, 0xd3, 0x0, 0xa3, 0x37);

#endregion
[Guid("E3CF2CAA-A38F-4865-9003-CC16D300A337"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IComInterfaceEvent))]
public class ComInterfaceDemo:IComInterface
{
public string ComInterfaceDemoMessage(string str)
{
ClassDemo.GetInstance().MyMessage = str;
return ClassDemo.GetInstance().MyMessage;
}
public int ComInterfaceDemoAdd()
{
return ClassDemo.GetInstance().MyAdd();
}
public int ComInterfaceDemoRemove()
{
return ClassDemo.GetInstance().MyRemove();
}
public float ComInterfaceDemoAddF()
{
return ClassDemo.GetInstance().MyAddF();
}
public float ComInterfaceDemoRemoveF()
{
return ClassDemo.GetInstance().MyRemoveF();
}

public object ComInterfaceDemoGetList()
{
return ClassDemo.GetInstance().GetList();
}
public MyTestDemo ComInterfaceDemoTestDemo()
{
return MyTestDemo.GetInstance();
}
public string[] ComInterfaceGetStringList()
{
return ClassDemo.GetInstance().GetStringList();
}
}
}

(2) 这里com 只用来调用CSharpComForVCDemo 项目中的类
参考文献:https://www.wendangku.net/doc/ac2074903.html,/KB/COM/com_object_in_c_.aspx

在 无公共语言运行时支持 c++ 程序中 测试
1) 在 stdafx.h 头文件中 加入
#import "CSharpComInterface.tlb"
using namespace CSharpComInterface;
注意 CSharpComInterface 项目 属性 tlb的生成 文件 路径 ..\vcDemo\
这样确保 每次 CSharpComInterface 项目 修改 编译新生成的tlb 都是新的
那么 c++ 引用的也就都是新的。
2)测试代码 主要 在 vcDemo.cpp 文件
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
// 调用 c# com 的地方
const char *p;
VARIANT mData;
int a;
CString cStr("哈哈。。。。。。");
BSTR ss=cStr.AllocSysString();
CoInitialize(NULL);//NULL换成0也可以
CSharpComInterface::IComInterfacePtr comInterface(__uuidof(ComInterfaceDemo));
_bstr_t data= comInterface->ComInterfaceDemoMessage(ss);// 传入的是字符串,返回的是字符串
p=data;
CString s=(CString)p;
MessageBox(s,0,0);// 显示返回的字符串
a=comInterface->ComInterfaceDemoAdd();// int
mData=comInterface->ComInterfaceDemoGetList();// 变体
comInterface->ComInterfaceDemoTestDemo();

comInterface->Release();
CoUninitialize();

} 这段 代码

运行 后 点击 帮助 关于。。。
就可以 看到 效果。。。。。。
c++ 知识有限 ,还有很多不太明白之处 ,有待 大家共同讨论。。。。。。


相关文档
相关文档 最新文档