C#学习之路--DLL参数转换

CHAR* 可以先实例化一个StringBuilder然后可以传给char*类型

 关于其他的请参考msdn中的c++与c#的类型转换

对应关系如下:

C++ ---- C#

传入的char* ----string

传出的char* ---- StringBuilder(预分配空间)

short ----short

char ---- byte

char[n] ---- fixed byte[n] \string 注意指定大小

结构指针 ----结构指针

函数指针 ---- 委托

 

建议参考http://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.unmanagedtype(v=VS.90).aspx这里描述最详尽。

样例:

c++ 中: typedef char OrderRefType[13];
c# 中:

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
string OrderRef;

这两天工作当中需要用到c++写的DLL文件,使用过程当中遇到些注意事项,记录下来

1 :正常情况下C++写好的文件当中有.dll,.lib,.h,.exp   VS 打开.h c++头文件以后会看到DLL文件当中提供的function 如下所示

 int __cdecl verifyTransResponse(char MerId[15], char OrdId[16], char TransAmt[12], char CuryId[3], char TransDate[8], char TransType[4], char OrdStat[4], char ChkValue[256]);
int __cdecl verifySignData(char *SignData, char ChkValue[256]);
int __cdecl signOrder(char MerId[15], char OrdId[16], char TransAmt[12], char CuryId[3], char TransDate[8], char TransType[4], char ChkValue[256]);
int __cdecl signData(char MerId[15], char *SignData, char ChkValue[256]);
void __cdecl setMerKeyFile(char keyFile[256]);
void __cdecl unsetMerKeyFile();
void __cdecl setPubKeyFile(char keyFile[256]);
void __cdecl unsetPubKeyFile();
int __cdecl digitalSign(char *MerId, char *SignData, char *keyFile, char *ChkValue)
int __cdecl validateSign(char *MerId, char *PlainData, char *ChkValue, char *keyFile);

2 : 新建C# 项目,将 C++ DLL文件放到BIN目录下边,如果用DLLImport方法会按照先BIN目录,然后系统文件夹顺序找DLL文件,当然也可自己放置到指定文件夹,然后DLLImport的时候指文件所在路径也可以,c:\test.DLL,DLLImport的时候写上该路径即可,如[DllImport("c:\test.DLL")]

3 : C#使用DLL文件示例如下

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Web;
using System.IO;
using System.Net;
using System.Collections;
using System.Configuration;
using System.Data;

namespace zyc.NetPay
{
    
public class NetPay
    {
        [DllImport(
"netpay.dll", EntryPoint = "?digitalSign@@YGHQADPAD00@Z")]
        
public static extern int digitalSign(string MerId, string SignData, string keyFile, StringBuilder chkValue);

        [DllImport(
"netpay.dll", EntryPoint = "?validateSign@@YGHQADPAD00@Z")]
        
public static extern int validateSign(string MerId, string PlainData, StringBuilder chkValue, string keyFile);
       
        
public int GetSign(string MerId, string SignData, string keyFile, ref StringBuilder ChkValue)
        { 
            
//string Result = "";
            ////  digitalSign
            //string pa1 = "123";
            
//string pa2 = "123";
            
//string pa3 = "123";
            
//string pa4 = "123";
            
//Byte[] chkValue = null;
            
//chkValue = new Byte[100];
            
//chkValue = System.Text.Encoding.Default.GetBytes("808080290000001");
            
//  string i = digitalSign(pa1, pa1, pa1, pa1);
            int returValue = digitalSign(MerId, SignData, keyFile, ChkValue);
            
return returValue; 
        }
        
public int GetValidateSign(string MerId, string PlainData,ref StringBuilder chkValue, string keyFile)
        {
            
int Result = validateSign(MerId, PlainData, chkValue, keyFile);
            
return Result;
        }
    }
}

4 : 注意事项

   4.1 必须引用 System.Runtime.InteropServices 命名空间

   4.2  [DllImport("netpay.dll", EntryPoint = "?digitalSign@@YGHQADPAD00@Z")]

        public static extern int digitalSign(string MerId, string SignData, string keyFile, StringBuilder chkValue);

        原先C++ DLL文件当中方法名是 digitalSign ,之前我用这个方法,程序会提示找不到入口点的错误,用dependercy walker软件打开DLL文件以后看到的方法名变成了?digitalSign@@YGHQADPAD00@Z

   所以EntryPoint属性写成这样,他表示要调用C++的哪个方法

 4.3 :关于传递参数问题,有时候C++ DLL方法当中是指针参数,且需要返回值的,类似于C#当中传址调用 这时候可以用StringBuilderw类型传递,

[DllImport("netpay.dll", EntryPoint = "?digitalSign@@YGHQADPAD00@Z")]

        public static extern int digitalSign(string MerId, string SignData, string keyFile, StringBuilder chkValue);这是DLLImport部份

C#中使用该方法的示例程序如下

int returValue = digitalSign(MerId, SignData, keyFile, ChkValue);

这样当在C++ DLL方法当中如果改变了ChkValue变量的值,那么原先C#当中的ChkValue值也改变了 更多的的关于参数传递部份学习自网址http://blog.csdn.net/jame_peng/archive/2009/07/28/4387906.aspx的一篇文章

    

int  类型
[DllImport(“MyDLL.dll
" )]
// 返回个int 类型
public   static   extern   int  mySum ( int  a1, int  b1);
// DLL中申明
extern  “C” __declspec(dllexport)  int  WINAPI mySum( int  a2, int  b2)
{
// a2 b2不能改变a1 b1
// a2=..
// b2=...
return  a + b;
}

// 参数传递int 类型
public   static   extern   int  mySum ( ref   int  a1, ref   int  b1);
// DLL中申明
extern  “C” __declspec(dllexport)  int  WINAPI mySum( int   * a2, int   * b2)
{
// 可以改变 a1, b1
* a2 = ...
* b2 = ...
return  a + b;
}

DLL 需传入char 
* 类型
[DllImport(“MyDLL.dll
" )]
// 传入值
public   static   extern   int  mySum ( string  astr1, string  bstr1);
// DLL中申明
extern  “C” __declspec(dllexport)  int  WINAPI mySum( char   *  astr2, char   *  bstr2)
{
// 改变astr2 bstr 2 ,astr1 bstr1不会被改变
return  a + b;
}

DLL 需传出char 
* 类型
[DllImport(“MyDLL.dll
" )]
//  传出值
public   static   extern   int  mySum (StringBuilder abuf, StringBuilder bbuf );
// DLL中申明
extern  “C” __declspec(dllexport)  int  WINAPI mySum( char   *  astr, char   *  bstr)
{
// 传出char * 改变astr bstr -->abuf, bbuf可以被改变
return  a + b;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值