Solved

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

Posted on 2004-10-16
4
1,822 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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