delphi下调用读写卡器的dll文件代码,帮忙翻译成C#!谢谢

hhhui2008 2006-10-25 02:33:45
var
Form1: TForm1;
Function MCS_InitComm (bPort:byte; dwCommBaudRate:integer):shortint;stdcall;
Function MCS_ExitComm ():shortint;stdcall;
Function MCS_GetVersion (bVersion:string):shortint;stdcall;
Function MCS_TestDevice ():shortint;stdcall;
Function MCS_TestDoor () :shortint;stdcall;
Function MCS_GetCapacity (bCapacity:byte) :shortint;stdcall;
Function MCS_AutoTest (bCardType:byte) :shortint;stdcall;
Function MCS_ErrorMsg (wErrNo:integer; bErrMsg:string) :shortint;stdcall;
Function MCS_PushCard ():shortint;stdcall;
Function MCS_LED (bOnOff:byte) :shortint;stdcall;
Function MCS_Buzzer (bOnOff:byte) :shortint;stdcall;
Function MCS_SetStringMode (bStringMode:byte) :shortint;stdcall;
Function MCS_GetStringMode (bStringMode:byte) :shortint;stdcall;
Function IsCommOpened () :shortint;stdcall;
Function SLE4442_OpenCard (bATR:string) :shortint;stdcall;
Function SLE4442_ReadChar(wAddr:integer;wLength:integer;bReadData:string):shortint;stdcall;
Function SLE4442_VerifyPWD(bPassWord1:byte;bPassWord2:byte;bPassWord3:byte):shortint;stdcall;
Function SLE4442_WriteChar(wAddr:integer;wLength:integer;bWriteData:string) :shortint;stdcall;
Function SLE4442_CloseCard():shortint;stdcall;
Function SLE4442_ReadPWD(bATR:string) :shortint;stdcall;
Function SLE4442_WritePWD(bATR:string) :shortint;stdcall;


implementation

{$R *.DFM}

Function MCS_InitComm;external 'MCS_SR.dll';
Function MCS_ExitComm;external 'MCS_SR.dll';
Function MCS_GetVersion;external 'MCS_SR.dll';
Function MCS_TestDevice;external 'MCS_SR.dll';
Function MCS_TestDoor;external 'MCS_SR.dll';
Function MCS_GetCapacity;external 'MCS_SR.dll';
Function MCS_AutoTest;external 'MCS_SR.dll';
Function MCS_ErrorMsg;external 'MCS_SR.dll';
Function MCS_PushCard;external 'MCS_SR.dll';
Function MCS_LED;external 'MCS_SR.dll';
Function MCS_Buzzer;external 'MCS_SR.dll';
Function MCS_SetStringMode;external 'MCS_SR.dll';
Function MCS_GetStringMode;external 'MCS_SR.dll';
Function IsCommOpened;external 'MCS_SR.dll';
Function SLE4442_OpenCard;external 'mcs_sr.dll';
Function SLE4442_ReadChar;external 'mcs_sr.dll';
Function SLE4442_VerifyPWD;external 'mcs_sr.dll';
Function SLE4442_WriteChar;external 'mcs_sr.dll';
Function SLE4442_CloseCard;external 'mcs_sr.dll';
Function SLE4442_ReadPWD;external 'mcs_sr.dll';
Function SLE4442_WritePWD;external 'mcs_sr.dll';

帮助翻译成C#,翻译几行吧!
...全文
309 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
Knight94 2006-10-26
  • 打赏
  • 举报
回复
注意函数名称的大小写
张小可 2006-10-26
  • 打赏
  • 举报
回复
public static extern Int16 sle4442_readchar(int wAddr,int wLength,[MarshalAs(UnmanagedType.LPTStr)]StringBuilder bReadData);

//Call
StringBuilder bReadData = new StringBuilder( 256 );//Init buffer
sle4442_readchar( yourAddr, yourLength, bReadData );调用它时出错“无法在 DLL MCS_SR.dll 中找到名为 sle4442_readchar 的入口点。”
hhhui2008 2006-10-26
  • 打赏
  • 举报
回复
调用它时出错“无法在 DLL MCS_SR.dll 中找到名为 sle4442_readchar 的入口点。”
出现以上错误信息
请指正!谢谢!
Knight94 2006-10-26
  • 打赏
  • 举报
回复
看函数,对于bReadData好像是传出

have a try!
public static extern Int16 sle4442_readchar(int wAddr,int wLength,[MarshalAs(UnmanagedType.LPTStr)]StringBuilder bReadData);

//Call
StringBuilder bReadData = new StringBuilder( 256 );//Init buffer
sle4442_readchar( yourAddr, yourLength, bReadData );
hhhui2008 2006-10-26
  • 打赏
  • 举报
回复
DELPHI代码如下:
Function SLE4442_ReadChar(wAddr:integer;wLength:integer;bReadData:PChar):short;stdcall;
external 'MCS_SR.dll' name 'SLE4442_ReadChar';

C#代码如下:
[DllImport("MCS_SR.dll", SetLastError=true)]
public static extern Int16 sle4442_readchar(int wAddr,int wLength,[MarshalAs(UnmanagedType.LPTStr)]string bReadData);

是读卡部分,现在是读卡出现问题!
请指正,愚翁!(这个希望能详细一点,包括怎样定义STRING)
十分感谢!
hhhui2008 2006-10-26
  • 打赏
  • 举报
回复
DELPHI代码如下:
function MCS_InitComm(port: byte;baud: dword): short;stdcall;
external 'MCS_SR.dll' name 'MCS_InitComm';

我转换成C#如下 :
public class NativeWIN32
{
[DllImport("MCS_SR.dll", SetLastError=true)]
public static extern int MCS_InitComm(byte bPort,int dwCommBaudRate
);
}

private void button1_Click(object sender, System.EventArgs e)
{
i=NativeWIN32.MCS_InitComm(0,115200);
}
但是错误!请愚翁给指正一下!十分感谢!
hhhui2008 2006-10-26
  • 打赏
  • 举报
回复
好的,我看到了!谢谢你!
不懂的地方,再请教你!
Knight94 2006-10-26
  • 打赏
  • 举报
回复
你先试着自己写,我已经给出了类型转换,如果还不行的话,我可以帮你写个别的。
hhhui2008 2006-10-26
  • 打赏
  • 举报
回复
var
Form1: TForm1;

implementation
function MCS_InitComm(port: byte;baud: dword): short;stdcall;
external 'MCS_SR.dll' name 'MCS_InitComm';
function MCS_ExitComm: short;stdcall;
external 'MCS_SR.dll' name 'MCS_ExitComm';
function MCS_ErrorMsg(wErrNo:word;bErrMsg:PChar):short;stdcall;
external 'MCS_SR.dll' name 'MCS_ErrorMsg';
function MCS_TestDoor:short;stdcall;
external 'MCS_SR.dll' name 'MCS_TestDoor';
Function MCS_SetStringMode (bStringMode:byte) :short;stdcall;
external 'MCS_SR.dll' name 'MCS_SetStringMode';
Function MCS_GetStringMode (bStringMode:byte) :short;stdcall;
external 'MCS_SR.dll' name 'MCS_GetStringMode';
Function IsCommOpened () :short;stdcall;
external 'MCS_SR.dll' name 'IsCommOpened';
Function SLE4442_OpenCard (bATR:PChar) :short;stdcall;
external 'MCS_SR.dll' name 'SLE4442_OpenCard';
Function SLE4442_ReadChar(wAddr:integer;wLength:integer;bReadData:PChar):short;stdcall;
external 'MCS_SR.dll' name 'SLE4442_ReadChar';
Function SLE4442_VerifyPWD(bPassWord1:byte;bPassWord2:byte;bPassWord3:byte):short;stdcall;
external 'MCS_SR.dll' name 'SLE4442_VerifyPWD';
Function SLE4442_WriteChar(wAddr:integer;wLength:integer;bWriteData:PChar) :short;stdcall;
external 'MCS_SR.dll' name 'SLE4442_WriteChar';
Function SLE4442_ReadPAC(bPAC:PByte):short;stdcall
external 'MCS_SR.dll' name'SLE4442_ReadPAC';
Function SLE4442_CloseCard():short;stdcall;
external 'MCS_SR.dll' name 'SLE4442_CloseCard';
愚翁,进来看看!
十分感谢!
hhhui2008 2006-10-26
  • 打赏
  • 举报
回复
自己再顶一下!
hhhui2008 2006-10-26
  • 打赏
  • 举报
回复
愚翁,搞定!
真的十分感谢你!
结贴!
张小可 2006-10-26
  • 打赏
  • 举报
回复
up
hhhui2008 2006-10-26
  • 打赏
  • 举报
回复
DELPHI代码中:
  Function MCS_SetStringMode (bStringMode:byte) :short;stdcall;
external 'MCS_SR.dll' name 'MCS_SetStringMode';

//call
MCS_SetStringMode(1);//设置成字符模式
i:= SLE4442_OpenCard(pChar(str1));
在C#中读出来的全是“口”
愚翁,再指正一下,快成功了!
张小可 2006-10-26
  • 打赏
  • 举报
回复
郁闷,还是不行呀
VirtualDesktop 2006-10-26
  • 打赏
  • 举报
回复
[DllImport( yourDll, CharSet=CharSet.Unicode)]
张小可 2006-10-26
  • 打赏
  • 举报
回复
愚翁呀,不行呀还是乱码
Knight94 2006-10-26
  • 打赏
  • 举报
回复
to 卡读出来了,怎么都是些乱码呀

在dllimport的时候加上charset标示
例如:
[DllImport( yourDll, CharSet=CharSet.Auto )]

当然你可以试试其他
张小可 2006-10-26
  • 打赏
  • 举报
回复
怎么读出来的都是些乱码呀,是不是 还得经过字符转换呀
张小可 2006-10-26
  • 打赏
  • 举报
回复
卡读出来了,怎么都是些乱码呀
hhhui2008 2006-10-25
  • 打赏
  • 举报
回复
自己顶顶吧!

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧