|
-
Dec 27th, 2014, 01:34 PM
#7
Thread Starter
New Member
Re: [VB6] Problem Using CopyMemory API to Get Collection Keys
Thanks everyone! I took LaVolpe's suggestion to zero out the tColElem when the loop ends. I also refactored some code based on Bonnie's suggestions. However, I don't think I implemented Bonnie's suggestion correctly, because it seemed to strip the keys from the collection.
As for credit to whomever came up with the structure declarations, I have no idea. I'm glad my question benefited others and wish I could give credit where it's due, but there are no comments in the code I'm working with to explain where it came from. I imagine the person who implemented the code in my application copied it from elsewhere, because the code style was inconsistent with the majority of the application.
I'm ok with the extra step to extract the key, so here's the working solution that I've implemented:
Code:
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Any) As Long
Private Type CollectionData
Unk0(0 To 2) As Long
Unk1 As Long
ElementCount As Long
Unk2 As Long
PtrFirstElement As Long
PtrLastElement As Long
Unk3 As Long
Unk4 As Long
Unk5 As Long
End Type
Private Type CollectionElement
Data As Variant
Key As String
PtrPrev As Long
PtrNext As Long
Unk0 As Long
Unk1 As Long
Unk2 As Long
End Type
Public Function GetKeys(ByVal oCollection As Collection) As String()
Dim asKeys() As String
Dim lIdx As Long
Dim tColl As CollectionData
Dim tColElem As CollectionElement
Dim tNulldata() As Byte
Dim sKey As String
If oCollection.Count > 0 Then
ReDim tNulldata(1 To LenB(tColElem))
ReDim asKeys(0 To oCollection.Count - 1)
CopyMemory tColl, ByVal ObjPtr(oCollection), LenB(tColl)
tColElem.PtrNext = tColl.PtrFirstElement
For lIdx = 0 To tColl.ElementCount - 1
CopyMemory tColElem, ByVal tColElem.PtrNext, LenB(tColElem)
sKey = String$(lstrlenW(tColElem.Key), 0)
CopyMemory ByVal StrPtr(sKey), ByVal tColElem.Key, lstrlenW(tColElem.Key) * 2
asKeys(lIdx) = sKey
Next
CopyMemory tColElem, tNulldata(1), UBound(tNulldata)
End If
GetKeys = asKeys
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|