 |

04-23-2017, 09:41 PM
|
Ultimate Contributor
|
|
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,859
|
|
Help needed with Pointers
In this test code below, I am simply trying to copy a Public variable (mHdr) to a Private variable (mHdr2) using CopyMemory.
When I print the results, I get what I expect for the first two Debug.Print statements (50 and 100),
same memory address for the third (mHdr2 and mHdr),
but I get 0 for the 4th line, instead of the 2 I expect.
50
100
8292624 | 8292624
bArray2(0) = 0
Could someone please show me what I am doing wrong?
Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)
Private Type MIDIHDR
lpData As Long ' pointer to MIDI data
dwBufferLength As Long
dwBytesRecorded As Long
dwUser As Long
dwFlags As Long
lpNext As Long
Reserved As Long
dwOffset As Long
dwReserved(0 To 3) As Long
End Type
Dim mHdr As MIDIHDR
Private Sub Form_Load()
'Populate byte array
Dim bArray(11) As Byte
bArray(0) = 2
bArray(1) = 0
bArray(2) = 0
bArray(3) = 0
bArray(4) = 0
bArray(5) = 0
bArray(6) = 0
bArray(7) = 0
bArray(8) = 0
bArray(9) = 0
bArray(10) = 0
bArray(11) = 0
'Populate MidiHeader
mHdr.lpData = VarPtr(bArray(0))
mHdr.dwBufferLength = 100
mHdr.dwBytesRecorded = 50
End Sub
Private Sub Command1_Click()
Dim mHdr2 As MIDIHDR
CopyMemory mHdr2, mHdr, Len(mHdr2)
Debug.Print mHdr2.dwBytesRecorded
Debug.Print mHdr2.dwBufferLength
Debug.Print mHdr2.lpData & " | " & mHdr.lpData
Dim bArray2(11) As Byte
CopyMemory bArray2(0), ByVal mHdr2.lpData, 12
Debug.Print "bArray2(0) = " & bArray2(0)
End Sub
|
Last edited by mms; 04-23-2017 at 09:51 PM.
|

04-24-2017, 09:09 AM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,775
|
|
bArray() is private to the form_load and no longer exists after the sub exits. Move it to public scope.
|
__________________
Quis custodiet ipsos custodues.
|

04-28-2017, 02:47 PM
|
Ultimate Contributor
|
|
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,859
|
|
I've been away for work for a few days and just getting back to this now.
Thanks OnErrOr !!
That fixes it (and explains it) for me.
|
|
Tags
|
private, debug.print, dim, copymemory, mhdr2, byte, mhdr, midihdr, barray20, barray11, byval, expect, populate, mhdr2.lpdata, type, variable, form_load, dwbufferlength, dwbytesrecorded, dwuser, lpnext, dwoffset, lpdata, dwreserved0, reserved  |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|