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

推荐订阅源

S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
博客园 - 聂微东
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
L
LangChain Blog
I
InfoQ
T
Tailwind CSS Blog
博客园 - 【当耐特】
V
V2EX
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Vercel News
Vercel News
雷峰网
雷峰网
量子位
A
About on SuperTechFans
Martin Fowler
Martin Fowler
H
Help Net Security

博客园 - 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;  
                  }