VC++ 调用 C#生成DLL的两种方法

来源:互联网 发布:java如何解决并发问题 编辑:程序博客网 时间:2024/04/30 06:16

今年在C++调用对方用C#写的DLL时,遇到托管和非托管的问题。

     原帖:

     http://topic.csdn.net/u/20110916/12/fcf63501-45ef-46d9-92d1-6ffa91086a80.html

     以及参考文章:

      VisualC、Delphi或者VB等编程语言来编写的DLL文件,在编译完成以后,产生DLL文件已经是一个可以直接供计算机使用的二进制文件,而Visual C#生成的DLL不是独立运行的程序,是某个程序的一个部分,只能由所属的程序调用,用户不能也不需要打开它,Visual C#编译器生成的托管代码虽然也是二进制文件,但不是可以直接供计算机使用的原始代码,实际上是一种中间语言(IL)代码,需要经过"下一代窗口服务"( Next Generation Windows Services,简写为NGWS ) runtime的即时编译器(即JIT)进行编译。用Visual C#生成的DLL文件已经和以前的DLL文件有了本质上的区别。用Visual C#生成的DLL文件在程序设计中更多的表现为一种类(Class)或者类库(Class Library)。

        如果想在VC++的非托管代码中调用已经用VC#生成的托管的DLL,从两个方向进行调整可以产生两种办法:(visual studio 2008)(下面方法都是对于同一平台下,即C#跟VC++都是在windows平台下)


一、对VC++的环境中进行修改使其支持托管代码:

      vc++调用端增加公共语言运行时【clr】的支持以执行C#的程序:解决方案-》Properties(右键)-》Configuration Properties(展开左树)->General(打开子节点)->Common Language Runtime support(选中选项)->【Common Language Runtime support(/clr)】(选中)
 OK,现在就可以引入托管的动态连接库来用了,不过在调用时还是得注意语法(new->gcnew,....),例如下: 
[cpp] view plaincopy
  1. #include "stdafx.h"  
  2.  #using "SmartDeviceDLL.dll"  
  3.  using namespace SmartDeviceDLL;  
  4.  int _tmain(int argc, _TCHAR* argv[])  
  5.  {  
  6.   printf("1111111111111\n");  
  7.    
  8.   SmartDeviceDLL::ICalculator ^pICalc=gcnew SmartDeviceDLL::Class1();  
  9.     
  10.   long lResult =0;  
  11.   lResult=pICalc->Add(5,10);  
  12.   wprintf(L"the result is %d\n",lResult);  
  13.     
  14.   printf("222222222222222222\n");  
  15.    
  16.   char c;  
  17.   scanf("%c",&c);  
  18.   return 0;  
  19.  }  

二、C#生成DLL端编译成COM接口,供VC++以托管格式调用(命令的运行都是在visual studio command prompt (命令窗口)中)

1.新建一个C#的动态连接库(在 模板 ,下单击 类库): 
[cpp] view plaincopy
  1. using System;  
  2.  using System.Linq;  
  3.  using System.Collections.Generic;  
  4.  using System.Text;  
  5.  namespace SmartDeviceDLL  
  6.  {  
  7.      public interface ICalculator  
  8.      {  
  9.          int Add(int Number1, int Number2);  
  10.      }  
  11.    
  12.      public class Class1: ICalculator  
  13.      {  
  14.          public int Add(int Number1, int Number2)  
  15.          {  
  16.              return Number1 * Number2;  
  17.          }  
  18.          public static int TestMethod(String s)  
  19.          {  
  20.              Console.WriteLine("Managed assembly: [0]", s);  
  21.              return s.Length;  
  22.          }  
  23.      }  
  24.  }<span style="font-family: Arial, Verdana, sans-serif; white-space: normal; "> </span>  

2.为程序集创建一个强命名的类库,并在AssemblyInfo.cs文件中用AssemblyKeyFile属性指向它:
    1)、使用强命名工具(Strong Name Utility)产生密钥对:
       sn -k MyKeyFile.snk
    2)、在AssemblyInfo.cs文件中用AssemblyKeyFile属性指向它:
      即在项目的文件AssemblyInfo.cs中将[assembly: ComVisible(false)]用以下内容替换:

[cpp] view plaincopy
  1. [assembly: ComVisible(true)]   
  2. [assembly: AssemblyDelaySign(false)]   
  3. [assembly: AssemblyKeyFile("MyKeyFile.snk")] //指向刚生成的文件(可用无汉字的绝对路径)  
   3)、重新编译,产生的程序集就是经过签名后的程序集了 

3.把生成的库文件加入全局程序集缓存(Global Assembly Cache, .NET [Compact]Framework支持一个工具,通常位于:C\Wndows\Assembly下)以便可以从任何 COM 客户端激活它,可以使用工具GACUtil.exe,指定/i命令开关将一个程序集安装到GAC中,同样可以使用/u命令开关将一个程序集从GAC中卸载。注意:安装的程序集必须是强命名程序集:         

[cpp] view plaincopy
  1. GACUTIL /i SmartDeviceDLL.dll   //可用SmartDeviceDLL.dll的绝对路径,Wince平台直接在命令窗口中用CGACUTIL(Compact)  

4.用下面的命令为COM注册刚才的程序集,生成COM库文件(程序集注册工具读取程序集中的元数据,并将所需的项添加到注册表中,注册表允许 COM 客户程序以透明方式创建 .NET Framework 类。类一经注册,任何 COM 客户程序都可以使用它,就好像该类是一个 COM 类。类仅在安装程序集时注册一次。程序集中的类实例直到被实际注册时,才能从 COM 中创建)

[cpp] view plaincopy
  1.  //下面命令注册 SmartDeviceDLL.dll 中包含的所有公共类,并生成和注册类型库 SmartDeviceDLL.tlb,该类型库包含 SmartDeviceDLL.dll 中定义的所有公共类型的定义   
  2.   REGASM SmartDeviceDLL.dll /tlb:SmartDeviceDLL.tlb  
  3. //或者可以选中:解决方案->Properties(右键)->Build->【Register for COM interop】(Wince平台的DLL此选项不可选)  

5.创建非托管的VC++调用程序(此处用Win32 Console Project为例): 

[cpp] view plaincopy
  1. #include "stdafx.h"  
  2.  #import "SmartDeviceDLL.tlb" named_guids  raw_interfaces_only  
  3.  using namespace SmartDeviceDLL;  
  4.  int _tmain(int argc, _TCHAR* argv[])  
  5.  {  
  6.   printf("1111111111111\n");   
  7.   //初始化COM以及产生智能指针  
  8.   HRESULT hr=CoInitializeEx(NULL,COINIT_MULTITHREADED);  
  9.   if(hr!=S_OK)  
  10.    printf("hr failed\n");  
  11.   else  
  12.    printf("hr ok\n");  
  13.    
  14.   printf("222222222222222222\n");  
  15.   SmartDeviceDLL::ICalculatorPtr pICalc;  
  16.   printf("2.1\n");  
  17.   HRESULT hRes=pICalc.CreateInstance(__uuidof(Class1),NULL,CLSCTX_ALL);  
  18.   //HRESULT hRes=pICalc.CreateInstance(SmartDeviceDLL::CLSID_Class1);  
  19.   printf("2.2\n");  
  20.   if(hRes==S_OK)  
  21.   {  
  22.        printf("hRes ok\n");  
  23.        long lResult =0;  
  24.        pICalc->Add(5,10, &lResult);  
  25.        wprintf(L"the result is %d\n",lResult);  
  26.   }  
  27.   else  
  28.        printf("hRes failure\n");  
  29.    
  30.   printf("333333333333\n");  
  31.   CoUninitialize();  
  32.   printf("4444444444444444444\n");  
  33.   char c;  
  34.   scanf("%c",&c);  
  35.   return 0;  
  36.  }  
0 0
原创粉丝点击