惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
WordPress大学
WordPress大学
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
罗磊的独立博客
The Cloudflare Blog
V
V2EX
月光博客
月光博客
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
博客园 - 【当耐特】
T
Tailwind CSS Blog

博客园 - icwin

在LINUX下进行C语言编程所需要的基础知识 路由器的“心脏”技术 软件费用的组成 如何利用C#创建和调用DLL收藏 C#中创建和使用资源动态链接库收藏 Nobi's StatusChart - 野比的状态波形图控件 C#调用API的一些基础(希望有人用的上) 如何合理创建c#对象 MD5加密简介以及C#下的操作实现 【转】C#项目打包,并自动安装SQL数据库 手把手教你用C#打包应用程序(安装程序)【卸载模块已添加】 C#.NET中存取数据库入门贴(ADO.NET) http://bbs.bccn.net/ .NET开发中的一些小技巧收藏 C#读取Excel数据收藏 C#文件夹和文件的常见操作收藏 C#实现重启应用程序收藏 C#实现SQL数据库备份与恢复 C#备份和还原SQL数据库
如何利用C#创建和调用DLL |
icwin · 2008-10-01 · via 博客园 - icwin

c#中如何调用vc++写的动态链接库收藏

新一篇: 如何利用C#创建和调用DLL | 

      当VC等调用C#写的COM时,用regasm   /tlb生成TLB文件,也可用tlbexp.exe,在VC等中加载TLB文件,当用C#调用VC等写的COM时,用tlbimp.exe,你可以写一个程序调试一下

      添加System.Runtime.InteropServices命名空间

     如是COM就直接用静态函数调用:                  
  public   static   int   GetNum(  
                          int   lFileSeqNo,  
                          string   sExtType,  
                          string   sExtNumber,  
                          string   sFormID,  
                          string   sOperationDate,  
                          string   sSystemRegistDate,  
                          out   int   lCount,  
                          out   int   lErrorType,  
                          out   int   lErrorCode)  
                  {  
                          int   iRet;  
   
                          WOBCom.ObjClass   obj   =   new   WOBCom.ObjClass();  
                           
                          iRet   =   obj.GetNum(  
                                  lFileSeqNo,  
                                  sExtType,  
                                  sExtNumber,  
                                  sFormID,  
                                  sOperationDate,  
                                  sSystemRegistDate,  
                                  out   lCount,  
                                  out   lErrorType,  
                                  out   lErrorCode);  
   
                          return   iRet;  
                  }  
  如不使COM是普通的DLL  
  不能直接用  
  只能在C++中加一个对外的接口:  
  extern   "C"   __declspec(dllexport)   WOExtConRegObj*   OutGetObjConstructor();  
  extern   "C"   __declspec(dllexport)   void   OutGetObjDestructor(WOExtConRegObj*   outGetObj);  
   
  extern   "C"   __declspec(dllexport)   long   SelectDummyRecord(long   *lErrorType,  
        long   *lErrorCode,  
        WOExtConRegObj*   outGetObj);  
  //  
  extern   "C"   __declspec(dllexport)   WOExtConRegObj*   OutGetObjConstructor()  
  {  
          WOExtConRegObj*   outGetObj   =   new   WOExtConRegObj();  
   
          return   outGetObj;  
  }  
   
  extern   "C"   __declspec(dllexport)   void   OutGetObjDestructor(WOExtConRegObj*   outGetObj)  
  {  
          delete   outGetObj;  
  }  
   
  extern   "C"   __declspec(dllexport)   long   SelectDummyRecord(long   *lErrorType,  
        long   *lErrorCode,  
        WOExtConRegObj*   outGetObj)  
  {  
  return   outGetObj->SelectDummyRecord(lErrorType,  
  lErrorCode);  
   
  }  
  C#就可直接调用了  
                  [DllImport("XXX.dll",  
                            EntryPoint="SelectDummyRecord",  
                            ExactSpelling=false,  
                            CallingConvention=CallingConvention.Cdecl)]  
                  private   static   extern   int   SelectDummyRecord(      
                          out   int   lErrorType,  
                          out   int   lErrorCode,  
                          int   outGetObj);  
   
                  ///   <summary>  
                  ///   </summary>  
                  ///   <remarks>  
                  ///   </remarks>                  
                  ///   <param   name="lErrorType"></param>  
                  ///   <param   name="lErrorCode"></param>  
                  ///   <returns></returns>  
                  public   int   SelectDummyRecord(  
                          out   int   lErrorType,  
                          out   int   lErrorCode)  
                  {  
                          int   intRtn;  
   
                          intRtn   =   SelectDummyRecord(  
                                  out   lErrorType,  
                                  out   lErrorCode,  
                                  m_OutGetObj);  
   
                          return   intRtn;  
                  }