Solved

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

Posted on 2004-10-16
4
1,747 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now