Jump to content

Recommended Posts

Posted

I am trying to get a list of smartcard readers on my machine using winscard.dll's scardlistreaders (http://msdn2.microsoft.com/en-us/library/aa379793.aspx)

LONG SCardListReaders(

__in SCARDCONTEXT hContext,

__in LPCTSTR mszGroups,

__out LPTSTR mszReaders,

__inout LPDWORD pcchReaders

);

with mszReaders being a "Multi-string that lists the card readers within the supplied reader groups. If this value is NULL, SCardListReaders ignores the buffer length supplied in pcchReaders, writes the length of the buffer that would have been returned if this parameter had not been NULL to pcchReaders, and returns a success code."

In the result to my call I do have the first reader in $result[3] but I can't get the rest of the list... In my arguments to the dllcall I set the type for mszReaders to "str".

I tried "str*" or "ptr" but those don't give anything back.

In VB this would be defined as "String * 256" for example (256 may be a little exaggerated for a count of readers on my machine :-) ).

Thanks for helping my find my lost data.

Posted

Here is my code. You can ignore the declarations at the top since dllstruct seems to ignore them:

Global Const $SCARD_SCOPE_USER = 0
Dim $hContext
Dim $pcchReaders = 227
Dim $szReaderList = DllStructCreate ( "char var1[256];char var2[256];char var3[256];char var4[256]")
Dim $mzGroup = ""
$dll = DllOpen("WinScard.dll")
if @error Then MsgBox(0,"1","Fail Load")
$result = DllCall($dll, "long", "SCardEstablishContext","long",$SCARD_SCOPE_USER,"long",0,"long",0,"long*",$hContext)
if @error Then 
    MsgBox(0,"2","Fail" & @error & $hContext & $result)
    DllClose($dll)
    Exit
EndIf
;MsgBox(0,"2","Success " & $result[4] & $hContext)
_ArrayDisplay($result)
$result = DllCall($dll, "lparam", "SCardListReaders","long",$result[4],"str",$mzGroup,"str",$szReaderList,"long*",$pcchReaders)
if @error Then 
    MsgBox(0,"3","Fail" & $pcchReaders & $result)
    DllClose($dll)
    Exit
EndIf
;MsgBox(0,"3","Success " & $result[3] & $result[4] & $pcchReaders & $szReaderList[1])
_ArrayDisplay($result)
DllClose($dll)
  • 16 years later...
Posted

I'm have use autoit call the winscard to connect contact and contact less device to use,

autoit is perfact .

Func Cardapi_getreader($nonui =  False)

if $mszGroups = "" Then
$mszGroups = "SCard$AllReaders"
EndIf

Dim $aReaderList[0][2]
$Readerslenth = 0
;SCardListReaders
;1獲取Readers list lenth
$result = DllCall($WinScard_DLL, "long", "SCardListReaders", "ptr", DllStructGetData($hContext,1) , "str", "", "str", "", "long*", 0)
;_ArrayDisplay($result)
$Readerslenth = $result[4]
Local $szReadername2 = DllStructCreate("byte a["&$Readerslenth&"];")
;2帶入Readers list lenth 獲取List
$result = DllCall($WinScard_DLL, "long", "SCardListReaders", "ptr", DllStructgetdata($hContext,1) , "str", $mszGroups, "ptr", DllStructGetPtr($szReadername2) , "long*", $Readerslenth) ;有in out 才要加* 原本就有out 定義就不用


Local $Meger_reader_name = ""
for $i = 1 to $Readerslenth
if hex(StringToBinary(StringMid(BinaryToString($szReadername2.a),$i,1))) <> "00" Then
$Meger_reader_name &=StringMid(BinaryToString($szReadername2.a),$i,1)
;msgbox(0,"",StringMid(BinaryToString($szReadername2.a),$i,1))
Else
_ArrayAdd($aReaderList, $Meger_reader_name)
$Meger_reader_name = ""
EndIf
Next

;_ArrayDisplay($aReaderList)
;msgbox(0,"",UBound($aReaderList))




if UBound($aReaderList) > 2 Then

While 1
$iReader = Input_GUI("Reader list",$aReaderList,0,1)
if $iReader <> -1 Then
ExitLoop
EndIf
WEnd

$szReadername = $aReaderList[$iReader][0]
;msgbox(0,"",$szReadername)

Else
if $Readerslenth <> 0 Then
$szReadername = BinaryToString($szReadername2.a)
;msgbox(0,"",$szReadername)
EndIf
EndIf
;_ArrayDisplay($result,"getreader")
;ConsoleWrite($result[0]&@CRLF)

if $nonui =  False Then
    if $result[0] = -2146435026 Then
        msgbox(0,"Getreader","沒有可用的智慧卡讀取裝置。")
        ;Exit
    elseif $result[0] = -2146435043 Then
        msgbox(0,"Getreader","智慧卡資源管理員並未執行。")
        Cardapi_unini()
        $steprec -= 1
        Return True
        ;Exit
    elseif $result[0] = -2146435042 Then
        msgbox(0,"Getreader","智慧卡資源管理員已關閉。")
        Cardapi_unini()
        $steprec -= 1
        Return True
        ;Exit
    EndIf

    if $result[0] <> 0 Then
        Return False
    EndIf
Else
    if $result[0] = -2146435026 Then
        Return False
        ;msgbox(0,"getreader","沒有可用的智慧卡讀取裝置。")
        ;Exit
    elseif $result[0] = -2146435043 Then
        Cardapi_unini()
        $steprec -= 1
        Return True
        ;msgbox(0,"getreader","智慧卡資源管理員並未執行。")
        ;Exit
    elseif $result[0] = -2146435042 Then
        ;msgbox(0,"getreader","智慧卡資源管理員已關閉。")
        Cardapi_unini()
        $steprec -= 1
        Return True
        ;Exit
    EndIf

    if $result[0] <> 0 Then
    Return False
    EndIf
EndIf
$steprec +=1
Return True

EndFunc

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...