UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Converting Code To Vba    
 
   
Zepha
post Jun 12 2019, 08:32 AM
Post#1



Posts: 182
Joined: 30-November 02



Dear Members

I am trying to write functions to read data from and write data to a Smart Card. I searched for VBA codes all over the Internet and failed to find anything I can use. However, i came across some codes which I believe I could modify for my purpose. However, while walking through the codes I have found some statements that require different syntax in VBA, but I cannot translate them. I request anybody in here with knowledge of both languages to help me translate these selected codes to VBA:

ASCII.GetBytes(lpszSendData, 0, Len(lpszSendData), aSend, 5)

ASCII.GetString(aRecv).Substring(0, pbLen)

pszRdr = New String(vbNullChar, 255)

aRdrLst(i) = aRdrLst(i).Replace(vbNullChar, vbNullString)

Dim sATR As String = String.Empty

Dim aATR() As Byte = Array.CreateInstance(GetType(Byte), 0)

pszATR = pszATR.TrimEnd(vbNullChar)

For i = 0 To (Len(pszATR) - 1)
pbAttr += Hex(Asc(pszATR.Chars(i))).PadLeft(2, "0") & Space(1)
Next i

Thank you very much






Go to the top of the page
 
ADezii
post Jun 12 2019, 11:24 AM
Post#2



Posts: 2,376
Joined: 4-February 07
From: USA, Florida, Delray Beach


This will be a somewhat feeble attempt to convert the Code into VBA, especially given the fact that I'm not even sure what Language the Code is in. In any event, here is what I came up with. I CANNOT attest to it's accuracy or interpretation, I can only hope that it will at least point you in the right direction.
CODE
'pszRdr = New String(vbNullChar, 255)
'vbNullChar - a character having a Value of 0 (translated)

Dim pszRdr As String
pszRdr = String(255, vbNullChar)
'OR
pszRdr = String(255, Chr$(0))

CODE
'aRdrLst(i) = aRdrLst(i).Replace(vbNullChar, vbNullString)
'If a NULL Character exists in the nth Element of an
'Array, Replace it with a NULL String

aRdrLst(i) = Replace(aRdrLst(i), vbNullChar, vbNullString)

CODE
'Dim sATR As String = String.Empty
'String.Empty - an Empty String (translated)

Dim sATR As String
sATR = ""

CODE
'Dim aATR() As Byte = Array.CreateInstance(GetType(Byte), 0)
'Creates a 1-dimensional Byte Array of Zero Length

Dim aATR() As Byte

CODE
'pszATR = pszATR.TrimEnd(vbNullChar)
'Remove a Trailing NULL Character from a String

Dim pszATR As String    'with a Trailing NULL
pszATR = Left$(pszATR, Len(pszATR) - 1)

CODE
'For i = 0 To (Len(pszATR) - 1)
  'pbAttr += Hex(Asc(pszATR.Chars(i))).PadLeft(2, "0") & Space(1)
'Next i
'Converts each Character of a String to a Hex Value padded
'on the left with 2 zeros (0's) and an additional Space

Dim pszATR As String
Dim i As Integer

pszATR = "Hello Dolly!"

Debug.Print pszATR

For i = 1 To Len(pszATR)
  Debug.Print Mid$(pszATR, i, 1) & ": 00" & _
              Hex(Asc(Mid$(pszATR, i, 1))) & Space(1)
Next i

CODE
OUTPUT from above:
Hello Dolly!
H: 0048
e: 0065
l: 006C
l: 006C
o: 006F
: 0020
D: 0044
o: 006F
l: 006C
l: 006C
y: 0079
!: 0021

CODE
'ASCII.GetBytes(lpszSendData, 0, Len(lpszSendData), aSend, 5)
'ASCII.GetString(aRecv).Substring(0, pbLen)

'Not sure on these two, it looks like ASCII Encoding/Decoding
'whereas a String is converted into an Array of Bytes and
'vice versa
Go to the top of the page
 
Zepha
post Jun 13 2019, 02:25 AM
Post#3



Posts: 182
Joined: 30-November 02



Thank you ADzeiI for your prompt reply. I will try to fix the codes you have given me and see if they can work. However, I have already tried to run the following simple VBA code to connect to the my Card Reader, but it returns a value of 6, which according to the constants refers to ERROR_INVALID_HANDLE

CODE
' API Functions

Public Declare Function SCardEstablishContext Lib "winscard.dll" (ByVal dwScope As Long, _
    ByVal pvReserved1 As Long, ByVal pvReserved2 As Long, ByRef phContext As Long) As Long

Public Declare Function SCardListReaders Lib "winscard.dll" Alias "SCardListReadersA" _
    (ByVal hContext As Long, ByVal mszGroups As String, ByVal mszReaders As String, _
    ByRef pcchReaders As Long) As Long



CODE
Sub GetContext()

    Dim lReturn As Long
    Dim RSVD1 As Long, RSVD2 As Long
    Dim myContext As Long

    lReturn = SCardEstablishContext(SCARD_SCOPE_USER, RSVD1, RSVD2, myContext)
    
    Debug.Print "SCardEstablishContext: Return =" & lReturn & _
                " myContext.CardContext1 = " & myContext & _
                " myContext.ReaderName = " & Chr(34) & myContext & Chr(34)

    Dim ListOfReaders As String, lenListOfReaders As Long

    lReturn = SCardListReaders(myContext, SCARD_DEFAULT_READERS, ListOfReaders, lenListOfReaders)

    Debug.Print "SCardListReaders: Return =" & lReturn & _
                " ListOfReaders = " & Chr(34) & ListOfReaders & Chr(34) & _
                " lenListOfReaders = " & lenListOfReaders

    lReturn = SCardReleaseContext(myContext)
    Debug.Print "SCardReleaseContext: Return =" & lReturn

End Sub


This has left me wondering what I can do. I am designing an application in Ms Access and that is why I am searching for VBA codes to read from and write to a Smart Card. The Internet is replete with codes in .Net or C#. My only problem is that I cannot translate the .Net codes like the ones I found on this website SMART CARD FRAMEWORK to VBA. I highly appreciate any further help in this regard.



Go to the top of the page
 
cheekybuddha
post Jun 13 2019, 04:12 AM
Post#4


UtterAccess VIP
Posts: 11,294
Joined: 6-December 03
From: Telegraph Hill


Just skimming your code ...

Where have you defined SCARD_SCOPE_USER? What's the constant's value?

Do you know whether the code is 32bit/64bit ?

--------------------


Regards,

David Marten
Go to the top of the page
 
Zepha
post Jun 13 2019, 04:34 AM
Post#5



Posts: 182
Joined: 30-November 02



The constant is defined here below

CODE
Public Const SCARD_SCOPE_USER As Long = &H0


I believe the code is 32bit


Thanks
Go to the top of the page
 
cheekybuddha
post Jun 13 2019, 06:55 AM
Post#6


UtterAccess VIP
Posts: 11,294
Joined: 6-December 03
From: Telegraph Hill


I'm afraid I don't have a lot of time to look in to this now.

I think you have maybe already seen a lot of the links here

The ones that caught my eye that might help you are:
https://stackoverflow.com/questions/3958536...stablishcontext
https://www.excelforum.com/excel-programmin...owing-code.html
https://bytes.com/topic/access/answers/9497...ace-smart-cards
http://www.UtterAccess.com/forum/Acccess-C...s-t1986575.html
https://docs.microsoft.com/en-us/windows/de...stablishcontext

I hope someone else can jump in and offer assistance.

d

--------------------


Regards,

David Marten
Go to the top of the page
 
Zepha
post Jun 13 2019, 07:08 AM
Post#7



Posts: 182
Joined: 30-November 02




Thanks for the links you have provided. I will go through them and hopefully get useful guidance to help me achieve my purpose

Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    20th June 2019 - 06:53 AM