C++ 调用C#代码

转自:http://www.cnblogs.com/Jianchidaodi/archive/2009/03/11/1408661.html

 

上篇文章提到,目前项目想做到核心部分代码不被反编译,而考虑到团队成员都是比较熟悉C#,因此核心算法部分采用C++,而其他地方则采用C#(例 如数据访问层,界面层都使用C#语言)。在上一篇文章中完成了C# 托管代码调用C++非托管代码 ,现在接着完成第二部分,即C++非托管代码调用C#托管代码(源代码下载 ), 分为两部分,首先C#建立COM+组件,其次是C++调用COM+组件。

 C#建立COM+组件

1. 在VS中,新建类库ComInterop

2.  在类库新增接口:ComInteropInterface, 及相应的实现ComInterop, ComInterop同时必须继承自ServicedComponent。ComInteropInterface中有两个简单接口:

 int Add(int a, int b);

 int Minus(int a, int b);

具体代码如下:


using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Reflection;
using  System.Runtime.InteropServices;
using  System.EnterpriseServices;


namespace  ComInteropDemo
{
    
// 接口声明
    [Guid( " 7103C10A-2072-49fc-AD61-475BEE1C5FBB " )]   
    
public   interface  ComInteropInterface
    {
        [DispId(
1 )]
        
int  Add( int  a,  int  b);

        [DispId(
2 )]
        
int  Minus( int  a,  int  b);
    }


    
// 对于实现类的声明
    [Guid( " 87796E96-EC28-4570-90C3-A395F4F4A7D6 " )]
    [ClassInterface(ClassInterfaceType.None)]
    
public   class  ComInterop : ServicedComponent, ComInteropInterface
    {
        
public  ComInterop() { }

        
public   int  Add( int  a,  int  b)
        {
            
return  a  +  b;
        }

        
public   int  Minus( int  a,  int  b)
        {
            
return  a  -  b;
        }
    }
}

 

3 . 使用REGASM命令导出虚拟表,当重新编译生产Dll时需要使用REGASM  /u命令将前一次Dll注销

    REGASM  ComInteropDemo.dll /tlb ComInteropDemo.tlb

    REGASM  /u ComInteropDemo.dll

首先对COM+组件的写法需要注意以下几点:

1. 接口,事件,方法,属性必须是public

2.  方法和属性必须在接口中声明,事件也必须在事件接口中声明.

     否则将在VC中无法调用,在接口中声明主要是为了在COM 中的vtab中.

3.  必须对接口中的方法,属性,事件前声明[DispId(1)]

4. 每个接口都必须有一个GUID

5.  而且项目一定需要是COM Interop,并且具有强命名

6.  组件ComVisible属性必须为true,这里强调的原因是VS中默认值为false

 

 C++调用C# COM+组件

 步骤:

1. 建立C++ 项目CppLoader,项目类型选择Win32,控制台应用程序

2.  在头文件中导入类型库tlb

    #import "..//Debug//ComInteropDemo.tlb"

3. 初始化COM以及产生智能指针(一般是在需要调用COM组件中提供的方法时就需要产生指向该接口的智能指针)

4. 调用COM中的方法Add

5. 释放环境 ,具体代码如下

 


#include  " stdafx.h "
#include 
< iostream >
using   namespace  std;


#import 
" ..//Debug//ComInteropDemo.tlb "
// 路径一定要正确

int  _tmain( int  argc, _TCHAR *  argv[])
{
    HRESULT hr;

    
// ComInteropDemo::ComInterop *p; 


    
// 初始化COM
    CoInitialize ( NULL );    

    
// 创建智能指针ComInteropDemo::ComInteropInterface
    ComInteropDemo::ComInteropInterfacePtr ptr;

    
// 创建实例
    hr  =  ptr.CreateInstance(__uuidof (ComInteropDemo::ComInterop));

    
if (hr  ==  S_OK)
    {
        cout 
<<  ptr -> Add ( 1.0 2.0 );
    }        

    CoUninitialize (); 
    
return   0 ;
}

 

附件: 源代码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值