Help needed with Pointers
Help needed with Pointers
Help needed with Pointers
Help needed with Pointers
Help needed with Pointers
Help needed with Pointers Help needed with Pointers Help needed with Pointers Help needed with Pointers Help needed with Pointers Help needed with Pointers Help needed with Pointers Help needed with Pointers
Help needed with Pointers Help needed with Pointers
Help needed with Pointers
Go Back  Xtreme Visual Basic Talk > > > Help needed with Pointers


Reply
 
Thread Tools Display Modes
  #1  
Old 04-23-2017, 10:41 PM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,859
Default 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 10:51 PM.
Reply With Quote
  #2  
Old 04-24-2017, 10:09 AM
OnErr0r's Avatar
OnErr0rHelp needed with Pointers OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

bArray() is private to the form_load and no longer exists after the sub exits. Move it to public scope.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #3  
Old 04-28-2017, 03:47 PM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,859
Default

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.
Reply With Quote
Reply

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Help needed with Pointers
Help needed with Pointers
Help needed with Pointers Help needed with Pointers
Help needed with Pointers
Help needed with Pointers
Help needed with Pointers Help needed with Pointers Help needed with Pointers Help needed with Pointers Help needed with Pointers Help needed with Pointers Help needed with Pointers
Help needed with Pointers
Help needed with Pointers
 
Help needed with Pointers
Help needed with Pointers
 
-->