Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to read XML file attributes... 17 42
Problem to Popup 2 52
.net VBA word safemode 1 24
Trouble with References... 5 25
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

24 Experts available now in Live!

Get 1:1 Help Now