首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scard32.dll和类似文件

问Scard32.dll和类似文件
EN

Stack Overflow用户
提问于 2011-02-11 15:54:02
回答 1查看 1.3K关注 0票数 0

我写这篇文章是为了请你帮助我。我的需求是在c#上下文中使用scard32.dll对智能卡(sle4428)进行读写。阅读器为SCM Microsystem公司的SCR3310

问题是我在哪里尝试使用c#做同样的事情。我的第一个尝试是在c#中翻译原生调用"SCardComand“,但失败了。

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

下面是我为你提供的VB.net原生调用

代码语言:javascript
复制
    <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#的翻译。

代码语言:javascript
复制
      [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);

如果有人知道如何继续下去,请……

EN

回答 1

Stack Overflow用户

发布于 2011-02-11 21:49:29

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

代码语言:javascript
复制
[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);  

应该是:

代码语言:javascript
复制
[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来处理handle、dataInLen和dataOutLen,而且很明显IntPtr != integer。得到16384的原因是因为您必须将IntPtr编组为一个整数才能真正获得正确的答案。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4966746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档