Results 1 to 6 of 6

Thread: CopyMemory confusion !!!!

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    43

    CopyMemory confusion !!!!

    Hi,

    the code below uses the CopyMemory API to copy the address of the variable X to a variable Y.

    after carrying out the copying of memory, the variable Y should contain the value in X.

    Then , if I change the value in X, the variable Y SHOULD now contain the new X value BUT that's not the case as the following test code shows :


    VB Code:
    1. Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    2. (pDst As Long, pSrc As Long, ByVal ByteLen As Long)
    3.  
    4.  
    5. Sub test()
    6.  
    7.     Dim x As Long
    8.     Dim y As Long
    9.    
    10.     x = 123
    11.     CopyMemory y, x, LenB(x)
    12.    
    13.     MsgBox y ' this show 123 as expected
    14.     x = 888
    15.     MsgBox y ' this should show 888 but shows 123 instead !!!!
    16.  
    17. End Sub


    Why is Y - in the second MsgBox - still showing (123) instead of showing the new value in X ie(888)?!

    Am I missing something ?

    Any help much appreciated .
    Last edited by BLUE_SEA; Jan 22nd, 2006 at 07:24 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: CopyMemory confusion !!!!

    Quote Originally Posted by BLUE_SEA
    Then , if I change the value in X, the variable Y SHOULD now contain the new X value
    No it shouldn't - you are copying the value.

    Your code is essentially the same as this:
    VB Code:
    1. Dim x As Long
    2.     Dim y As Long
    3.    
    4.     x = 123
    5.     y = x   '(y = 123)
    6.    
    7.     MsgBox y ' this show 123 as expected
    8.     x = 888
    9.     MsgBox y  'you have only changed X, so y is still 123

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    43

    Re: CopyMemory confusion !!!!

    Thanks Si_The_Geek,

    So what's the use of CopyMemory ?

    How would I use CopyMemory in this context to have two different pointers pointing to the same variable ??

    Regards.

  4. #4
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: CopyMemory confusion !!!!

    You can't change the pointer of a variable
    You can get the pointer of a variable using the VarPtr function
    what are you trying to do?

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    43

    Re: CopyMemory confusion !!!!

    Quote Originally Posted by moeur
    You can't change the pointer of a variable
    You can get the pointer of a variable using the VarPtr function
    what are you trying to do?
    I am trying to understand the workings of the CopyMemory API. I thought one of its uses was to be able to copy a memory address from one Pointer to another so we can have two different Pointers poiting to the same variable kind of like using the Set VB statement to point to an object .


    Regards.

  6. #6
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: CopyMemory confusion !!!!

    As Si pointed out copy memory copies what is pointed to to a new location not the pointer.

    Uses in VB are limited. It is usually used when working with API function that return a UDT. Do a search for copymemory in these forums and you'll see examples of when it is used.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width