Solved

Accessing memory mapped file containing C++ structures from C#.NET or VB.NET

Posted on 2004-10-16
4
1,763 Views
Last Modified: 2008-03-03
Hello!

We have a group of C++ programs (unmanaged code) that communicate with by storing a large data structure in a memory mapped file.  This is an example of how we get the initial access in C++...

hSharedTEE = OpenFileMapping(FILE_MAP_ALL_ACCESS,0,sharedmem_TEE_name);
if (!hSharedTEE) return hSharedTEE:
else {
      pSharedTEE = MapViewOfFile(hSharedTEE,FILE_MAP_ALL_ACCESS,0,0,SHARED_MEMORY_SIZE_TEE);
      return pSharedTEE;
}

The returned (void *) is used to access the data structure...
((TYPE_SHARE_TEE *)pSharedTEE)->Cbufferorderin[iorderin].Cprimarydepth.primarydepth.side[SELL].pricepoint[i1].size = 700;

We would like to be able to access the same data structure from within VB.Net or C#.Net in order to simply the development of some GUIs to the data structure.

A sample structure is...

typedef struct {
       struct {
            struct {
                    double price;
                    long size;
                    char market[10];
                    bool valid;
                    char flag;
            } quote[SIZEOFQuotes];
      } side[SIZEOFSide];
} TYPE_QUOTE_MONTAGE;

This in turn may be embedded in other structures, etc...

All of the arrays are fixed length.

Getting the handle/pointer to the structure does not appear to be a problem within .Net using either "declare function" or "dllimport" for vb.net or c#.net.

My question is how do I create structures/classes/whatever in VB.Net or C#.Net to match up to the data structures that exist in C++ and access the data?  I don't care which language...just which ever makes the job easier.

Thanks,

John
0
Comment
Question by:John_Crumley
  • 2
4 Comments
 
LVL 12

Expert Comment

by:vascov
ID: 12344718
My advice would be to develop a component in Managed CPP that exposes those types.
It's going to hurt a little because you'll some overhead marshalling the data, but it should be easier than messing with the interop stuff (lot's of details about pining, what gets to one side or the other,etc...)

hth

Vasco
0
 
LVL 1

Author Comment

by:John_Crumley
ID: 12346419
Vasco,

We are not able to change the structure of shared memory.  How would we maintain the same shared mememory layout in a Manage CPP class to access these elements?

Thanks,

John
0
 
LVL 12

Accepted Solution

by:
vascov earned 500 total points
ID: 12354292
Using normal structures. Managed CPP would enable you to use whatever C/CPP structures/APIs you are already using and also provide a way to give that to Managed Code. The thing is that you would not be writing directly over those structures in managed code, but through some API exposed by your managed CPP layer. Something like, internally your managed cpp class holds the mem mapped thing, and exposes managed methods to be manipulated (read or write). (hence the marshalling overhead). It all depends on how significative the perf of this is, and how it impacts your overall target.

You could also just define PInvoke structs and APIs, but again, there are a lot of tweaks about the way PInvoke works, especially for bi-directional memory fixe stuff.

Vasco
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question