C#调用C++编写的DLL函数各种参数传递问题

时间:2025-03-15 07:52:25

1. 不返回值的参数

C++ 原型:

 

[cpp] view plain copy

  1. bool    SendNewSms(char *szTel, char *szMessage);  

 

 

C#引用;

 

[csharp] view plain copy

  1. [DllImport( "",EntryPoint="SendNewSms")]  
  2. public     static     extern    bool SendNewSms(string phone,string msg);  


2. 带返回值(char *)

 

C++原型:

[cpp] view plain copy

  1. BOOL GetCardErrorMessage(char *szErrorMessage , int errorCode);  

C#引用

 

 

[csharp] view plain copy

  1. [DllImport( "",EntryPoint="GetCardErrorMessage")]  
  2.      public     static     extern    int GetCardErrorMessage(StringBuilder msg,int errorCode);  
  3.   
  4. StringBuilder buf = new StringBuilder(1024);//指定的Buf大小必须大于可能的最大长度  
  5.        GetCardErrorMessage(buf,1);  


3. 带返回值(其他类型)

 

C++原型:

 

[cpp] view plain copy

  1. BOOL GetSmsSaveStation (int *nSmsStation);  


C#引用

 

 

[csharp] view plain copy

  1. [DllImport( "",EntryPoint="GetSmsSaveStation")]  
  2.  public    static    extern   bool GetSmsSaveStation(ref int nStation);  


4. 传递结构体指针(C++填充)

 

C++原型:

[cpp] view plain copy

  1. struct NET_INFO_STRUCT  
  2. {  
  3.    DWORD nDurationTime; //持续时间   
  4.    double nReceiveByte; //接收字节  
  5.    double nSendByte;   //发送字节  
  6. };    
  7. BOOL NetGetConnectDetail(NET_INFO_STRUCT *lpNetInfo);  


C#引用
  

[csharp] view plain copy

  1. public struct NET_INFO_STRUCT  
  2. {  
  3.    public uint nDurationTime; //持续时间   
  4.    public double nReceiveByte; //接收字节  
  5.    public double nSendByte;   //发送字节  
  6. }  
  7. [DllImport( "",EntryPoint="NetGetConnectDetail")]  
  8.          public    static    extern   int NetGetConnectDetail(ref NET_INFO_STRUCT pNetInfo);  
  9.           
  10.          NET_INFO_STRUCT netInfo = new NET_INFO_STRUCT();  
  11.          NetGetConnectDetail(ref netInfo);   

     
5. 传递结构体数组(C++来填充)
C++原型:

[cpp] view plain copy

  1. struct UIM_BOOK_STRUCT  
  2. {  
  3.    int UimIndex;  
  4.    char szName[15];  
  5.    char szPhone[21];  
  6. };  
  7. int ReadUimAllBook(UIM_BOOK_STRUCT lpUimBookItem[],int nMaxArraySize);  


C#引用

[csharp] view plain copy

  1. [StructLayout(, CharSet = )]//可以指定编码类型  
  2. public struct UIM_BOOK_STRUCT  
  3. {  
  4.    public int UimIndex;  
  5.    [MarshalAs(, SizeConst= 15)]  
  6.    public string szName;  
  7.    [MarshalAs(, SizeConst= 21)]  
  8.    public string szPhone;  
  9. };  
  10. [DllImport( "",EntryPoint="ReadUimAllBook")]  
  11. public    static    extern   int ReadUimAllBook([Out] UIM_BOOK_STRUCT [] lpUimBookItem,int nMaxArraySize);  
  12. UIM_BOOK_STRUCT[] p = new UIM_BOOK_STRUCT[20];  
  13. int ret = ReadUimAllBook(p,);  

6. 注意问题

 

类型不一致,会导致调用失败,
(1) long 类型,在C++中是4字节的整数,在C#中是8字节的整数;
(2) 字符串类型的设置不正确;

 

以下是几个简单的window调用

 

[csharp] view plain copy

  1. [] // We won't use this maliciously  
  2. [DllImport("", CharSet=)]  
  3. public static extern bool ScreenToClient(IntPtr hWnd, ref  rect);  
  4.   
  5. [] // We won't use this maliciously  
  6. [DllImport("", CharSet=)]  
  7. public static extern bool GetWindowRect(IntPtr hWnd, out  rect);  
  8.   
  9. [] // We won't use this maliciously  
  10. [DllImport("", CharSet=)]  
  11. public static extern bool UnregisterClass([MarshalAs()] string className, IntPtr instanceHandle);  


转自/wen158809179/article/details/5704701