Re: Pointers in vb6 by Mike
Mike
Wed Mar 02 08:22:06 CST 2005
> Hi i am building an application to read a PC/SC application in vb6. I am
> having problems with one of the functions SCardListReaders.Below is the C
> declaration of the SCardList Readers.
>
> LONG SCardListReaders(
> SCARDCONTEXT hContext,
> LPCTSTR mszGroups,
> LPTSTR mszReaders,
> LPDWORD pcchReaders
> );
>
> pcchReaders
> [in, out] Length of the mszReaders buffer in characters. This parameter
> receives the actual length of the multi-string structure, including all
> trailing null characters. If the buffer length is specified as
> SCARD_AUTOALLOCATE, then mszReaders is converted to a pointer to a byte
> pointer, and receives the address of a block of memory containing the
> multi-string structure. This block of memory must be deallocated with
> SCardFreeMemory.
>
> My problem is with mszReaders. i have to use SCARD_AUTOALLOCATE for the last
> variable.if this happens, mszReaders changers to a pointer to a byte pointer.
> How can vb handle this?
Due to the lack of detail specified a lot of assumptions have to be made about how the API works, however have a look at
something like this perhaps:
'*** Warning; air code..
Private Declare Function SCardListReaders Lib "YourLib.dll" ( _
ByVal hContext As Long, ByVal mszGroups As String, _
ByVal mszReaders As Long, ByRef pcchReaders As Long) As Long
Private Declare Function SCardFreeMemory Lib "YourLib.dll" ( _
ByVal pBuffer As Long) As Long
Private Declare Function lstrlen Lib "Kernel32.dll" Alias "lstrlenA" ( _
ByVal lpString As Long) As Long
Private Declare Function lstrcpy Lib "Kernel32.dll" Alias "lstrcpyA" ( _
ByVal lpString1 As String, ByVal lpString2 As Long) As Long
...
Dim hContext As Long
Dim Groups As String
Dim BufPtr As Long, BufLen As Long
Dim Strings() As String, NumStrings As Long
hContext = PopulateHandle()
Groups = PopulateGroups()
BufLen = SCARD_AUTOALLOCATE
If (SCardListReaders(hContext, Groups, BufPtr, ByVal BufLen) Then
NumStrings = MultiStringToStringArray(BufPtr, Strings())
Call SCardFreeMemory(BufPtr)
End If
...
Private Function MultiStringToStringArray(ByVal inPtr As Long, ByRef outString() As String) As Long
Dim RetStrs() As String, NumRet As Long ' Converts a multi-string buffer to an array of strings
Dim ThisString As String, StringLen As Long
Dim CurPtr As Long
CurPtr = inPtr
Do
StringLen = lstrlen(CurPtr)
If (StringLen > 0) Then
ThisString = Space$(StringLen)
Call lstrcpy(ThisString, CurPtr)
ReDim Preserve RetStrs(NumRet) As String
RetStrs(NumRet) = ThisString
NumRet = NumRet + 1
CurPtr = CurPtr + StringLen + 1
End If
Loop While StringLen > 0
MultiStringToStringArray = NumRet
outString() = RetStrs()
End Function
'***
Assumptions made are:
* SCardFreeMemory() takes a single input which is the pointer to the allocated buffer
* SCardListReaders() returns non-0 for success
* When pcchReaders is set to SCARD_AUTOALLOCATE, is takes the value directly and not a pointer to this value (so buffer
length can be overwritten.) - If it does explode then remove the "ByVal" from the SCardListReaders() call and see if
that works any better.
* A "multi-string structure" is simply a bunch of concatenated null-terminated strings terminated with a double null.
* My code actually works.. ;)
With all this, I would strongly recommend you save everything before running any of the above, if any of these are wrong
then it's likely to explode ;)
Hope this helps,
Mike
P.s. There's a Direct memory manipulation tutorial on my site which introduces working with memory in VB if you're
interested.
- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/