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

1. 不返回值的参数

C++ 原型:

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

C#引用;

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

2. 带返回值(char *)

C++原型:

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

[DllImport( "CdmaCard.dll",EntryPoint="GetCardErrorMessage")]
     public     static     extern    int GetCardErrorMessage(StringBuilder msg,int errorCode);

StringBuilder buf = new StringBuilder(1024);//指定的Buf大小必须大于可能的最大长度
       GetCardErrorMessage(buf,1);

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

C++原型:

   BOOL GetSmsSaveStation (int *nSmsStation);

C#引用

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

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

C++原型:

struct NET_INFO_STRUCT
{
   DWORD nDurationTime; //持续时间 
   double nReceiveByte; //接收字节
   double nSendByte;   //发送字节
};  
BOOL NetGetConnectDetail(NET_INFO_STRUCT *lpNetInfo);

C#引用
  
public struct NET_INFO_STRUCT
{
   public uint nDurationTime; //持续时间 
   public double nReceiveByte; //接收字节
   public double nSendByte;   //发送字节
}
[DllImport( "CdmaCard.dll",EntryPoint="NetGetConnectDetail")]
         public    static    extern   int NetGetConnectDetail(ref NET_INFO_STRUCT pNetInfo);
        
         NET_INFO_STRUCT netInfo = new NET_INFO_STRUCT();
         NetGetConnectDetail(ref netInfo); 
     
5. 传递结构体数组(C++来填充)
C++原型:
struct UIM_BOOK_STRUCT
{
   int UimIndex;
   char szName[15];
   char szPhone[21];
};
int ReadUimAllBook(UIM_BOOK_STRUCT lpUimBookItem[],int nMaxArraySize);

C#引用
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]//可以指定编码类型
public struct UIM_BOOK_STRUCT
{
   public int UimIndex;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst= 15)]
   public string szName;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst= 21)]
   public string szPhone;
};
[DllImport( "CdmaCard.dll",EntryPoint="ReadUimAllBook")]
public    static    extern   int ReadUimAllBook([Out] UIM_BOOK_STRUCT [] lpUimBookItem,int nMaxArraySize);
UIM_BOOK_STRUCT[] p = new UIM_BOOK_STRUCT[20];
int ret = ReadUimAllBook(p,p.Length);
6. 注意问题

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

 

以下是几个简单的window调用

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

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

  • 2
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSharp 调用C++ DLL; 参数为指针类型导出函数 c# Csharp调用 c++库 参数为导入和导出指针两种 包含C++ DLL源码 如fun(cont char* A,char*B) A为输入参数,B为输出参数-C# CSharp call C++ DLL lib dll function param use export and import eg: fun(cont char* A,char*B) A IN,B OUT TestDll\Debug\TestCallDll.exe .......\.....\TestCallDll.vshost.exe .......\.....\TestCallDll.vshost.exe.manifest .......\.....\TestDll.dll .......\.....\TestDll.lib .......\TestCallDll\Form1.cs .......\...........\Form1.Designer.cs .......\...........\Form1.resx .......\...........\obj\Debug\TestCallDll.csproj.FileListAbsolute.txt .......\...........\...\.....\TestCallDll.csproj.GenerateResource.Cache .......\...........\...\.....\TestCallDll.exe .......\...........\...\.....\TestCallDll.Form1.resources .......\...........\...\.....\TestCallDll.pdb .......\...........\...\.....\TestCallDll.Properties.Resources.resources .......\...........\Program.cs .......\...........\...perties\AssemblyInfo.cs .......\...........\..........\Resources.Designer.cs .......\...........\..........\Resources.resx .......\...........\..........\Settings.Designer.cs .......\...........\..........\Settings.settings .......\...........\TestCallDll.csproj .......\....Dll\dllmain.cpp .......\.......\ReadMe.txt .......\.......\stdafx.cpp .......\.......\stdafx.h .......\.......\targetver.h .......\.......\TestDll.cpp .......\.......\TestDll.def .......\.......\TestDll.h .......\.......\TestDll.vcproj .......\.......\TestDll.vcproj.PC-201008261742.Administrator.user .......\TestDll.sln .......\TestDll.suo .......\....CallDll\obj\Debug\TempPE .......\...........\...\Debug .......\...........\obj .......\...........\Properties .......\Debug .......\TestCallDll .......\TestDll TestDll

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值