copymemory basic question
- From: mscir <mscir@xxxxxxxxx>
- Date: Wed, 24 Aug 2005 17:59:53 -0700
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.)
.
- Follow-Ups:
- Re: copymemory basic question
- From: Sam Hobbs
- Re: copymemory basic question
- From: Karl E. Peterson
- Re: copymemory basic question
- Prev by Date: Re: What is the fastest way to determine if folder has subfolders or not!?
- Next by Date: Re: gettickcount returns a negative number???
- Previous by thread: How to use InternetErrorDlg in VB
- Next by thread: Re: copymemory basic question
- Index(es):
Relevant Pages
|
Loading