Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6170705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:05:10+00:00 2026-05-23T23:05:10+00:00

What is the best practice to share memory of a struct from a C#

  • 0

What is the best practice to share memory of a struct from a C# program to a C++ win32 DLL?

I’ve used structs in managed shared memory using Boost between two C++ programs and it worked great. I’m lost on the best way to accomplish this between where the struct gets populated in the C# program and the C++ DLL that is an SNMP subagent.

Here’s the C++ DLL:

//====================           Code Excerpt from the main cpp file              ====================== 
#include "stdafx.h" 
//=================           Here we are setting up the shared memory area   =====================
#pragma data_seg (".SHAREDMEMORY")
    struct sharedData {
     int sharedA;
     int sharedB;
    };
    static sharedData A;

#pragma data_seg() 
#pragma comment(linker,"/SECTION:.SHAREDMEMORY,RWS")

BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) 
{ 
     return TRUE; 
} 
//=============================================================================================
//====================== Here we are writing wrappers to the shared memory area ===========================
//=You must declare it as an Extern "C" to prevent name mangling. This is absolutely necessary in order to import it into c#  =
//=============================================================================================
extern "C" __declspec(dllexport) sharedData __stdcall getMyData() 
{
    A.sharedA = 1237;
    A.sharedB = 31337;
     //return gshared_nTest; 
    return A;
} 
extern "C" __declspec(dllexport) void __stdcall setMyData( sharedData buff ) 
{ 
     A = buff; 
}

Here’s the calling C# function:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace sharedMemTestCS
{
    public partial class frmSharedMemTestCS : Form
    {
    struct sharedData {
     int sharedA;
     int sharedB;
    };
    static sharedData A;

    //==============  here we are importing the methods from the win32 dll into the c# console application =================
    [DllImport(@"C:\Documents and Settings\My Documents\Visual Studio 2010\Projects\sharedMemTestCPP\Debug\sharedMemTestCPP.dll")]  
    public static extern sharedData getMyData();  
    [DllImport(@"C:\Documents and Settings\My Documents\Visual Studio 2010\Projects\sharedMemTestCPP\Debug\sharedMemTestCPP.dll")]  
    public static extern void setMyData(int data);

        public frmSharedMemTestCS()
        {
            InitializeComponent();
            //==============  here i am incrementing the value =================

            //== i use a message box so that i can have multiple console applications running at once and it will pause at the messagebox (if i don't click ok)
            //== i do this so i can see the values changing in the shared memory.
            //MessageBox.Show( getMyData().ToString() );
            getMyData();
            //txtBoxA.Text = (getMyData().ToString() );
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //setMyData( getMyData() + 100 );
            //txtBoxA.Text = (getMyData().ToString() );
        }
    }
}

The error message I get is:

Error 1   Inconsistent accessibility: return type

‘sharedMemTestCS.frmSharedMemTestCS.sharedData’ is less accessible
than method
‘sharedMemTestCS.frmSharedMemTestCS.getMyData()’ c:\documents and
settings\mconrad\my documents\visual studio
2010\Projects\sharedMemTestCS\sharedMemTestCS\Form1.cs 23 37 sharedMemTestCS

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:05:11+00:00Added an answer on May 23, 2026 at 11:05 pm

    The best practice for sharing memory would be to use the MemoryMappedFile class in C# and CreateFileMapping/MapViewOfFile in C++.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there best practice for managing Object shared by 2 or more others Object.
What is the best practice to accessing a single running mnesia node from another
What is the best practice if I wanted to download the latest binaries from
Just want to get some advice on what is the best practice to share
I am looking for some suggestion over best practice (considering Memory and CPU time)
say I want to load an array of short from global memory to shared
Is it always a best practice to use - Select E.Id,D.DeptName from Employee E
I was wondering if people would share their best practices / strategies on handling
Best practice is to use unique ivs, but what is unique? Is it unique
Short best practice question: If an object A is injected into another object B,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.