2011-02-11 75 views
0

我寫這篇文章是要求你幫助我。 我的需求是在c#上下文中使用scard32.dll讀取和寫入智能卡(sle4428)。讀者是SCM微系統的SCR 3310Scard32.dll和類似的

首先,我有一個運行correctlty的vb.net代碼,我可以讀寫數據。 問題在於我嘗試使用c#來做同樣的事情。 我的第一個嘗試是在C#中翻譯本地調用「SCardCommand」,但失敗。 第二次嘗試是從VB.net代碼構建一個DLL並在c#上下文中使用它,但仍然失敗。

如果我寫這篇文章是因爲我沒有更多的想法。

後續我是在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); 

而對於istance如果我在C#中運行此命令int Ret = SCardComand(0, "Card,MemVerifyPin,FFFFF", 0,"",0, "", 0);獲取16384,這意味着沒有我的。

請,如果有人對如何進行一個想法......

+0

當你在VB中運行該命令時,你會得到什麼?你期望得到什麼,這對你來說意味着什麼? 16384有什麼問題?這是一個很好的數字! – 2011-02-11 08:19:14

+0

嗨丹,當我在vb.net中運行並且PIN碼是正確的時,我得到了0. – joksy82 2011-02-11 09:06:49

回答

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!=整數。你得到16384的原因是因爲你必須將一個IntPtr Marshall變成一個整數才能真正得到正確的答案。

如果我的建議不起作用,那麼發佈此命令的文檔,需要知道如何在C/C++代碼中聲明原型,以便更有可能提出替代方案。