|
|
#1 |
|
Member
Join Date: Dec 05
Posts: 43
![]() |
What's all the fuss about COPYMEMORY ?
Hi there,
Can anybody show a practical real-world use for the CopyMemory API in VB apart from simply copying memory bits to an array ? I still don't really understand all the fuss about this function . Thanks in advance. |
|
|
|
|
|
#2 |
|
Old Member
Join Date: Nov 04
Location: In Hiding.... Weather: sizzzzlin'........ Code: Secret
Posts: 2,681
![]() ![]() ![]() ![]() ![]() |
Re: What's all the fuss about COPYMEMORY ?
where is the fuss besides here?
|
|
|
|
|
|
#3 |
|
The Devil
Join Date: Aug 00
Location: Quetzalshacatenango
Posts: 9,091
![]() ![]() ![]() |
Re: What's all the fuss about COPYMEMORY ?
You use CopyMemory to convert a pointer into a UDT for many APIs.
__________________
Laugh, and the world laughs with you. Cry, and you just water down your vodka. Take credit, not responsibility |
|
|
|
|
|
#4 |
|
Fanatic Member
Join Date: Dec 04
Location: Sector 000
Posts: 556
![]() |
Re: What's all the fuss about COPYMEMORY ?
CopyMemory is used to create basic structures such as stacks, queues, and linked lists.
__________________
VBAhack (Links: Cubic Spline Tutorial, Parametric Cubic Spline Tutorial, VB Numerical Routines)
|
|
|
|
|
|
#5 |
|
Frenzied Member
Join Date: Aug 01
Location: England
Posts: 1,237
![]() |
Re: What's all the fuss about COPYMEMORY ?
VB, generally, does not allow direct access to memory. This can be prohibitively slow - in some cases - so some programmers prefer to get dirty and get down to the details to provide a (sometimes) huge increase in performance.
In many cases CopyMemory is used to dereference a pointer (as described above) but this is mostly unecessary. For instance you can use IDL (to create a typelibrary) and squeeze implied semantic functionality from a few other 'unknown' functions: VB Code:
This allows the following: VB Code:
. . . which allows for more natural dereferencing code (and it's much MUCH) faster. I think that CopyMemory is so popular is because it's simply that it's so well known.
__________________
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein It's turtles! And it's all the way down Thoughts Last edited by yrwyddfa; Mar 22nd, 2006 at 06:12 AM. |
|
|
|
|
|
#6 |
|
Frenzied Member
Join Date: Aug 01
Location: England
Posts: 1,237
![]() |
Re: What's all the fuss about COPYMEMORY ?
For instance, consider the following VB code (it won't run - if you want to play I can provide a link to the project)
VB Code:
This avoids many VB things that could be considered slow. To be fair, you'd probably want to write this lot in C and implement it in a DLL that VB can access: I only wrote this to play with the MemLong function (and to test it's speed) This fragment avoids SAFEARRAY overheads, and other VB safety features by directly allocating some memory, and then manipulating it according to various matrix rules. SAFEARRAYS are not great for dealing across different threads in a process with such wonderful features such as cbLocks in it's descriptor, and the automatic teardown code that goes with SAFEARRAYS (which, btw, makes VB marvellously simple and safe for beginners and experts alike) Also, ideally, the memory needs to be allocated on the stack, rather than the heap, but - hey: one step at a time. It also creates a completly arbitrary descriptor (I invented it, if you like) to describe the matrix structure. Which means the thread only needs to have a copy of the descriptor, and not the body of data. You can effectively pass around the descriptor instead of the data. You can replace the vast majority of MemLong, and MemSingle calls in this fragment with CopyMemory calls - so hopefully you can see the advantage in using it. Normally, though, you'd be using CopyMemory to directly access parts of VB's internal system as in this example: VB Code:
which returns the highest ordinal allocated in an a single dimensional array, and ARRAY_NOT_INITIALISED if the array is not allocated yet; this is incredibly useful in production code, as well as being extremely fast.
__________________
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein It's turtles! And it's all the way down Thoughts Last edited by yrwyddfa; Mar 22nd, 2006 at 06:19 AM. |
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|