Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-10-16
4
Medium Priority
?
1,923 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 1500 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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