1. 程式人生 > >C#呼叫C++編寫的DLL函式引數傳遞

C#呼叫C++編寫的DLL函式引數傳遞

                                          

1. 不返回值的引數

C++ 原型:

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

C#引用;


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

2. 帶返回值(char *)

C++原型:

BOOL GetCardErrorMessage(char *szErrorMessage , int errorCode);
    
C#引用


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

3. 帶返回值(其他型別)

C++原型:

   BOOL GetSmsSaveStation (int *nSmsStation);
    

C#引用


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

4. 傳遞結構體指標(C++填充)

C++原型:


    
  1. struct NET_INFO_STRUCT
  2. {
  3.    DWORD nDurationTime; //持續時間
  4.    double nReceiveByte; //接收位元組
  5.    double nSendByte;   //傳送位元組
  6. }; 
  7. BOOL NetGetConnectDetail(NET_INFO_STRUCT *lpNetInfo);

C#引用
  

    
  1. public struct NET_INFO_STRUCT
  2. {
  3.    public uint nDurationTime; //持續時間
  4.    public double nReceiveByte; //接收位元組
  5.    public double nSendByte;   //傳送位元組
  6. }
  7. [ DllImport( "CdmaCard.dll",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++原型:

    
  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#引用

    
  1. [ StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] //可以指定編碼型別
  2. public struct UIM_BOOK_STRUCT
  3. {
  4.    public int UimIndex;
  5.    [ MarshalAs(UnmanagedType.ByValTStr, SizeConst= 15)]
  6.    public string szName;
  7.    [ MarshalAs(UnmanagedType.ByValTStr, SizeConst= 21)]
  8.    public string szPhone;
  9. };
  10. [ DllImport( "CdmaCard.dll",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,p.Length);
6. 注意問題

型別不一致,會導致呼叫失敗,
(1) long 型別,在C++中是4位元組的整數,在C#中是8位元組的整數;
(2) 字串型別的設定不正確;

 

以下是幾個簡單的window呼叫


    
  1.         [ System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
  2.         [ DllImport("User32.dll", CharSet=CharSet.Auto)]
  3.         public static extern bool ScreenToClient(IntPtr hWnd, ref System.Drawing.Point rect);
  4.         [ System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
  5.         [ DllImport("User32.dll", CharSet=CharSet.Auto)]
  6.         public static extern bool GetWindowRect(IntPtr hWnd, out System.Drawing.Rectangle rect);
  7.         [ System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
  8.         [ DllImport("User32.dll", CharSet=CharSet.Auto)]
  9.         public static extern bool UnregisterClass([MarshalAs(UnmanagedType.LPTStr)] string className, IntPtr instanceHandle);

轉自 http://blog.csdn.net/wen158809179/article/details/5704701

                                                                      

1. 不返回值的引數

C++ 原型:

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

C#引用;


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

2. 帶返回值(char *)

C++原型:

BOOL GetCardErrorMessage(char *szErrorMessage , int errorCode);
  
C#引用


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

3. 帶返回值(其他型別)

C++原型:

   BOOL GetSmsSaveStation (int *nSmsStation);
  

C#引用


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

4. 傳遞結構體指標(C++填充)

C++原型:


  
  1. struct NET_INFO_STRUCT
  2. {
  3.    DWORD nDurationTime; //持續時間
  4.    double nReceiveByte; //接收位元組
  5.    double nSendByte;   //傳送位元組
  6. }; 
  7. BOOL NetGetConnectDetail(NET_INFO_STRUCT *lpNetInfo);

C#引用
  

  
  1. public struct NET_INFO_STRUCT
  2. {
  3.    public uint nDurationTime; //持續時間
  4.    public double nReceiveByte; //接收位元組
  5.    public double nSendByte;   //傳送位元組
  6. }
  7. [ DllImport( "CdmaCard.dll",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++原型:

  
  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#引用

  
  1. [ StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] //可以指定編碼型別
  2. public struct UIM_BOOK_STRUCT
  3. {
  4.    public int UimIndex;
  5.    [ MarshalAs(UnmanagedType.ByValTStr, SizeConst= 15)]
  6.    public string szName;
  7.    [ MarshalAs(UnmanagedType.ByValTStr, SizeConst= 21)]
  8.    public string szPhone;
  9. };
  10. [ DllImport( "CdmaCard.dll",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,p.Length);
6. 注意問題

型別不一致,會導致呼叫失敗,
(1) long 型別,在C++中是4位元組的整數,在C#中是8位元組的整數;
(2) 字串型別的設定不正確;

 

以下是幾個簡單的window呼叫


  
  1.         [ System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
  2.         [ DllImport("User32.dll", CharSet=CharSet.Auto)]
  3.         public static extern bool ScreenToClient(IntPtr hWnd, ref System.Drawing.Point rect);
  4.         [ System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
  5.         [ DllImport("User32.dll", CharSet=CharSet.Auto)]
  6.         public static extern bool GetWindowRect(IntPtr hWnd, out System.Drawing.Rectangle rect);
  7.         [ System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
  8.         [ DllImport("User32.dll", CharSet=CharSet.Auto)]
  9.         public static extern bool UnregisterClass([MarshalAs(UnmanagedType.LPTStr)] string className, IntPtr instanceHandle);

轉自 http://blog.csdn.net/wen158809179/article/details/5704701