织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>.NET(C#)>

C#托管代码与C++非托管代码互相调使用二(C++调使用C#代码)

jackyhwei 发布于 2009-12-04 17:45 点击:次 
C++非托管代码调用C#托管代码,分为两部分,首先C#建立COM+组件,其次是C++调用COM+组件
TAG:

现在接着完成第二部分,即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+组件的写法需要注意以下几点:

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

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

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

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

(jacky)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/200912/8086.html]
本文出处:未知 作者:jacky
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容