-
How to reference Memory Mapped File data directly
Is it possible in VB6 or VB.NET to reference a view of a memory-mapped file
directly without copying the data into another variable all the time?
The main application I want to interface to uses a Memory Mapped File to
communicate between any client application and the main application. The
client application puts a command and it's arguments at the start of the
memory in the MMF, the main app watches for a command and processes it, and
puts the response into the MMF. The client gets the response data from the
MMF.
I want to get a pointer to the MMF view in memory during initialisation.
After that, I just want to reference fields in the shared memory structure
by reference, and I do not want to copy the memory when I want to set/get
the current values into/from the shared memory.
Is it possible to do this in VB6 or VB.NET, and could you please give me
the syntax for declaring the shared memory structure in VB so that it is
a direct reference, or should I change the development language to C# to
get the performance I want?
Many thanks in anticipation.
-
Re: How to reference Memory Mapped File data directly
"Geoff Lingham" <glingham@yahoo.com> wrote in message
news:3cc448a1$1@10.1.10.29...
>
> Is it possible in VB6 or VB.NET to reference a view of a memory-mapped
file
> directly without copying the data into another variable all the time?
>
> The main application I want to interface to uses a Memory Mapped File to
> communicate between any client application and the main application. The
> client application puts a command and it's arguments at the start of the
> memory in the MMF, the main app watches for a command and processes it,
and
> puts the response into the MMF. The client gets the response data from the
> MMF.
>
> I want to get a pointer to the MMF view in memory during initialisation.
> After that, I just want to reference fields in the shared memory structure
> by reference, and I do not want to copy the memory when I want to set/get
> the current values into/from the shared memory.
>
> Is it possible to do this in VB6 or VB.NET, and could you please give me
> the syntax for declaring the shared memory structure in VB so that it is
> a direct reference, or should I change the development language to C# to
> get the performance I want?
>
> Many thanks in anticipation.
>
>
Geoff -
My guess is that VB.NET is out, since you can't write unmanaged code in
VB.NET. Of course, this doesn't preclude you from writing a wrapper class
in C#or C++ w/Managed Extensions.
As for VB6 - there is a pretty neat "trick" you can use to do this, which I
got from Matt Curland's "Advanced Visual Basic 6" book. To summarise: you
take advantage of the way that VB array's work. Behind the scenes, VB array
variables are actually pointers to a structure called SAFEARRAY. Using the
inevitable CopyMemory() call, you can replace the default structure with
your own SAFEARRAY, one of whose members is actually a pointer to the start
of the memory which is used by your memory mapped file. I think this
technique was also described in an article for Visual Basic Programmer's
Journal a year or so agao.
--
Mark Alexander Bertenshaw
Programmer/Analyst
Chordiant Software, Inc.
Brentford
UK
-
Re: How to reference Memory Mapped File data directly
> I want to get a pointer to the MMF view in memory during initialisation.
> After that, I just want to reference fields in the shared memory structure
> by reference, and I do not want to copy the memory when I want to set/get
> the current values into/from the shared memory.
Using a UDT and
'# Create Mapping object
h1 = CreateFileMapping(-1, 0, PAGE_READWRITE, 0, Len(UDT), "Name")
'# Get pointer to mapping
p1 = MapViewOfFile(h1, FILE_MAP_WRITE, 0, 0, 0)
gives you a pointer to the memory where UDT is located, and using
CopyMemory() you can extract the members of UDT to variables.
I've written a little sample project to read the LV_ITEM structure
of the desktop items; if interested, please mail.
But for your purpose it would be simpler and better to use
SendMessage(hwnd, WM_COPYDATA, ...). For a sample see
www.allapi.net -> VB Tools -> CALLBACK
BTW: WM_COPYDATA uses file mapping in the background ...
Juergen.
-
Re: How to reference Memory Mapped File data directly
Hi Juergen
I am copying the whole of my User Defined Type (UDT) from the Memory Mapped
File (MMF) to my local VB variable UDT on every request, at the moment, and
this seems inefficient to me. What I was wondering was, is there a way in
VB of *not* having to get/set a copy of my UDT from/to the MMF mapped view
all the time using Win32 memory copy API's. If my UDT is really large then
copying the whole UDT all the time is very expensive in processing time.
I want to be able to just reference any field within my UDT and *directly*
access the corresponding offset within the MMF shared memory. Is this possible
in VB6 or VB.NET, and if so what is the VB syntax please?
Thanks for your help.
-
Re: How to reference Memory Mapped File data directly
Hi Geoff,
> I want to be able to just reference any field within my UDT and *directly*
> access the corresponding offset within the MMF shared memory. Is this
possible
> in VB6 or VB.NET, and if so what is the VB syntax please?
I fear, you cannot. All you use in VB must be located in any variable.
VB doesn't support pointer except of such pointing to the own things
(StrPtr(), VarPtr(), ObjPtr()).
But copying memory is the fastest thing of all - you shouldn't worry about
this ...
Juergen.
-
Re: How to reference Memory Mapped File data directly
Geoff,
You can do this in VB6 by crafting a SAFEARRAY structure to create a single
element array of the UDT.
<Air Code>
Public Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ptr()
As Any) As Long
Type SafeArray1d
cDims As Integer
fFeatures as Integer
cbElements as Long
cLocks as Long
pvData as Long
cElements as Long
lLbound as Long
End Type
Private atMy() as MyUDT
Private tSA1D as SafeArray1d
Sub MapUDTToArray()
With tSA1D
.cDims = 1
.cbElements = LenB(atMy(0))
.cElements = 1
End With
CopyMemory ByVal VarPtrArray(atMy), VarPtr(tSA1D), 4
End Sub
</Air Code>
With this done you can simpy assign the MMF pointer to tSA1D.pvData. You
will be able to retrieve it's contents as atMy(0).whatever.
Make sure you do following _before_ atMy() passes out of scope
ZeroMemory ByVal VarPtrArray(atMy), 4
If you like this I would recommend Matthew Curland's book Advanced Visual
Basic 6 to you.
--
Anthony Jones
Nuesoft Ltd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks