0

我写这篇文章是为了请求你帮助我。我的要求是在 c# 上下文中使用 scard32.dll 读取和写入智能卡 (sle4428)。读卡器为单片机微系统的SCR 3310

首先,我有一个运行正确的 vb.net 代码,我能够读取和写入数据。问题是当我尝试使用 c# 做同样的事情时。我的第一次尝试是在 c# 中翻译本地调用“SCardComand”但失败了。第二次尝试是从 VB.net 代码构建一个 dll 并在 ac# 上下文中使用它,但仍然失败。

如果我写这篇文章是因为我没有更多的想法。

按照我在 VB.net 中为您提供本机调用

    <DllImport("SCARD32", EntryPoint:="SCardComand", SetLastError:=True, CallingConvention:=CallingConvention.Winapi, CharSet:=CharSet.Ansi, ExactSpelling:=True)> _
Public Shared Function SCardComand(ByRef handle As Integer, 
<MarshalAs(UnmanagedType.VBByRefStr)> ByRef cmd As String, 
ByRef cmdLen As Integer, 
<MarshalAs(UnmanagedType.VBByRefStr)> ByRef dataIn As String, 
ByRef dataInLen As Integer, 
<MarshalAs(UnmanagedType.VBByRefStr)> ByRef dataOut As String, 
ByRef dataOutLen As Integer) As Integer
End Function

这里是我对 c# 的翻译。

      [DllImport("SCARD32.DLL")]
  static extern UInt32 SCardComand(IntPtr handle,
  [MarshalAs(UnmanagedType.LPTStr)] ref String cmd,
  IntPtr cmdLen,
  [MarshalAs(UnmanagedType.LPTStr)] ref String dataIn,
  IntPtr dataInLen,
  [MarshalAs(UnmanagedType.LPTStr)] out String dataOut,
  IntPtr dataOutLen);

例如,如果我在 c# 中运行此命令,请int Ret = SCardComand(0, "Card,MemVerifyPin,FFFFF", 0,"",0, "", 0);获取 16384,这对我来说毫无意义。

请如果有人知道如何进行......

4

1 回答 1

0

我注意到的第一件事是你的 C# 是错误的

[DllImport("SCARD32.DLL")]    static extern UInt32 SCardComand(IntPtr handle,    [MarshalAs(UnmanagedType.LPTStr)] ref String cmd,    IntPtr cmdLen,    [MarshalAs(UnmanagedType.LPTStr)] ref String dataIn,    IntPtr dataInLen,    [MarshalAs(UnmanagedType.LPTStr)] out String dataOut,    IntPtr dataOutLen);  

应该:

[DllImport("SCARD32.DLL")]    static extern UInt32 SCardComand(int handle,    [MarshalAs(UnmanagedType.LPTStr)] ref String cmd,    IntPtr cmdLen,    [MarshalAs(UnmanagedType.LPTStr)] ref String dataIn,    int dataInLen,    [MarshalAs(UnmanagedType.LPTStr)] out String dataOut,    int dataOutLen);  

原因是工作中的 VB.net 代码不使用 IntPtr 作为句柄、dataInLen 和 dataOutLen,显然 IntPtr != integer。你得到 16384 的原因是你必须将 IntPtr 编组为一个整数才能真正得到正确的答案。

如果我的建议不起作用然后发布此命令的文档,则需要知道原型是如何在 C/C++ 代码中声明的,以便更有可能提出替代方案。

于 2011-02-11T13:49:28.700 回答