wrote a Linked list , but failed why??
Hello
i have used the following code to write a linked list.
but the code crashes at CopyMemoryRead in ReadDataToStructure.
may i know the reason for the failure of my code!?!?
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetProcessHeap Lib "kernel32" () As Long
Public Declare Function HeapAlloc Lib "kernel32" _
(ByVal hHeap As Long, ByVal dwFlags As Long, _
ByVal dwBytes As Long) As Long
Public Declare Function HeapFree Lib "kernel32" _
(ByVal hHeap As Long, ByVal dwFlags As Long, _
lpMem As Any) As Long
Public Declare Sub CopyMemoryPut Lib "kernel32" Alias _
"RtlMoveMemory" (ByVal Destination As Long, _
Source As Any, ByVal Length As Long)
Public Declare Sub CopyMemoryRead Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, _
ByVal Source As Long, ByVal Length As Long)
Public pHead As Long
Public Type struc_ewordptr
sUserName As String
sDir As String
sFileName As String
pNext As Long
End Type
Public Sub PutDataIntoStructure(ByVal ptr As Long, _
szdata As String, ByVal ptrNext As Long)
Dim stLL As struc_ewordptr
stLL.sUserName = szdata
stLL.pNext = ptrNext
CopyMemoryPut ptr, stLL, 504
End Sub
Public Sub ReadDataToStructure(ByVal ptr As Long, _
struct As struc_ewordptr)
Dim stLL As struc_ewordptr
CopyMemoryRead stLL, ptr, 504
struct.sUserName = stLL.sUserName
struct.pNext = stLL.pNext
End Sub
Public Sub ReadLinkedListDataAndFreeMemory()
Dim pLocal As Long
Dim hHeap As Long
Dim stLL As struc_ewordptr
Dim strData As String
pLocal = pHead
hHeap = GetProcessHeap()
Do While pLocal <> 0
ReadDataToStructure pLocal, stLL
strData = strData & vbCrLf & stLL.sUserName
HeapFree hHeap, 0, pLocal
pLocal = stLL.pNext
Loop
MsgBox strData
End Sub
Public Sub CreateLinkedList()
'add three items to list
' get the heap first
Dim pFirst As Long, pSecond As Long 'local pointers
Dim hHeap As Long
Dim str As String
hHeap = GetProcessHeap()
'allocate memory for the first and second element
pFirst = HeapAlloc(hHeap, 0, 504)
pSecond = HeapAlloc(hHeap, 0, 504)
If pFirst <> 0 And pSecond <> 0 Then
'memory is allocated
str = InputBox("enter the user name")
PutDataIntoStructure pFirst, str, pSecond
PutDataIntoStructure pSecond, "Pointers", 0
pHead = pFirst
End If
'put he second element in the list
End Sub
thanks in advance
Sruti
|