copymemory basic question



I'm trying to speed up a sort routine used on large text files by using CopyMemory to copy or swap array elements. Never having used CopyMemory before, I'm wondering whether it can be used to simply copy one string to another, without doing a swap. If I understand the web page below correctly, it isn't possible. Maybe I'm trying to use the wrong API?

TIA,
Mike

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnovba00/html/LightningStrings.asp

'Swap strings s and t.
temp = s
s = t
t = temp

....the quick method uses the swapping code:

CopyMemory lng, ByVal VarPtr(s), 4
CopyMemory ByVal VarPtr(s),  ByVal VarPtr(t), 4
CopyMemory ByVal VarPtr(t), lng, 4

This code simply swaps the contents of the BSTR's s and t. That is, it swaps the addresses of the corresponding Unicode arrays. In this way, we only need to swap 4-byte addresses, no matter how long the Unicode arrays may be.

In the first line of code, the long variable lng will receive the address of the first Unicode array. Because this address is stored in the BSTR s, we pass the address of s by value.

Actually, you might think that the code:

CopyMemory lng, s, 4

would also work, but it doesn't. In brief, the reason is that when VB sees that a string is being passed to an API function, it makes a copy of the array in ANSI format (rather than Unicode) and passes the ANSI version to the function. (For a more detailed discussion of this issue, please see my book.)

.



Relevant Pages

  • Re: Visual Basic source for Garmin track download (altitude only)?
    ... > I can get the binary track data from a Garmin gps and extract lat/long ... The function is then called as CopyMemory (the CopyMemory alias is as ... and for an array you just pass the first element (AFAIK arrays are always ...
    (sci.geo.satellite-nav)
  • Re: Visual Basic source for Garmin track download (altitude only)?
    ... I think (from memory) that what I did was save the info. to a file(probably ... > The function is then called as CopyMemory (the CopyMemory alias is as ... > So read your data into a Byte array of size 4 (the Byte type is safer than ... > Dim sgl As Single ...
    (sci.geo.satellite-nav)
  • Re: Constant array
    ... But I can't quite figure out how to make an array of constants without ... DOES> SWAP CELLS + @ EXECUTE; ...
    (comp.lang.forth)
  • Re: "Sorting" assignment
    ... you think using memcpy a block at a time is not the best way to swap ... trouble now or later. ... It does not bother to zero the array (you'd need a clever optimiser ... void memswap(char *ptrToA, char *ptrToB, int intLength) ...
    (comp.programming)
  • Re: Copying Similar Data Structures
    ... >> I tried using a straight CopyMemory call and it seemed to work great. ... >> When finished I have array of new structure to write back to disk. ... >> Tim Rude ... >> The other way would be to use CopyMemory, incrementing the source and ...
    (microsoft.public.vb.general.discussion)

Loading