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 7988453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:27:28+00:00 2026-06-04T12:27:28+00:00

I am using sharedmemory in my c# app with c++ interop. Currently I am

  • 0

I am using sharedmemory in my c# app with c++ interop. Currently I am marshalling a struct to a pointer and broadcasting the message. The program I am broadcasting to, opens up correctly with the debug message, but doesn’t show/bring-in the data that I had in use within my struct.

Thanks!

The app I am trying to talk to was written in c++ and I am coding in c#. I am using all the DLLImports correctly (I think) and it compiles and runs error free.

using System.Runtime.InteropServices;

[DllImport("user32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
public static extern uint RegisterWindowMessageW([In]string lpString);

[DllImport("user32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern uint RegisterWindowMessageA([In]string lpString);

[DllImport("kernel32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern IntPtr OpenFileMapping(FileMapAccessRights dwDesiredAccess, int bInheritHandle, [In]String lpName);

[DllImport("kernel32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, FileMapAccessRights dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap);

[DllImport("kernel32", CallingConvention = CallingConvention.StdCall)]
public static extern int UnmapViewOfFile(IntPtr lpBaseAddress);

[DllImport("kernel32", CallingConvention = CallingConvention.StdCall)]
public static extern int CloseHandle(IntPtr hObject);

[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

uint WM_ZOOM_XYZ = RegisterWindowMessageA("WM_ZOOM_XYZ");

int i = Broadcast_Zoom_Message(10000, 10000, 0, WM_ZOOM_XYZ);

public int Broadcast_Zoom_Message(double dbX, double dbY, double dbZ, uint uMessage) 
{
    string smSharedMemory = "COORDINATES";

    IntPtr hMem = OpenFileMapping(FileMapAccessRights.Write, FALSE, smSharedMemory);

    if (IntPtr.Zero == hMem)
    {
        return 0;
    }

    IntPtr pvHead = MapViewOfFile(hMem, FileMapAccessRights.Write, 0, 0, UIntPtr.Zero);


    if (IntPtr.Zero == pvHead)
    {
        CloseHandle(hMem);
        MessageBox.Show(
            "Unable to view " + smSharedMemory, 
            "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        return 0;
    }

    CoordinatesStruct structCoords = new CoordinatesStruct();


    Marshal.PtrToStructure(pvHead, structCoords);

    int bVersionOk = FALSE;

    if (1 == structCoords.uMajorVersion)
    {
        if (WM_ZOOM_XYZ == uMessage)
        {
            structCoords.dbDesiredX = dbX;
            structCoords.dbDesiredY = dbY;
            structCoords.dbDesiredZ = dbZ;
        }
        bVersionOk = TRUE;
    }
    else
    {
        MessageBox.Show(
            "Unrecognized shared memory: " +
            structCoords.uMajorVersion.ToString() + "." + structCoords.uMinorVersion.ToString());
    }
    if (IntPtr.Zero != hMem)
    {
        CloseHandle(hMem);
    }
    UnmapViewOfFile(pvHead);

    IntPtr HWND_BROADCAST = (IntPtr)0xffff;

    if (bVersionOk == TRUE)
    {
        PostMessage(HWND_BROADCAST, uMessage, 0, 0);
        return 1;
    }
    else
        return 0;
}
  • 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-06-04T12:27:29+00:00Added an answer on June 4, 2026 at 12:27 pm

    I think your intention was to put the changed structCoords back to the mapped file. When we use Marshal.PtrToStructure() we receive a copy of the content of the unmanaged memory. The changes of the received object will not reflect in the unmanaged memory. When we are done with the data, we should put the changes back to the memory using Marshal.StructureToPtr.

    Here is what I think it should be:

    if (1 == structCoords.uMajorVersion)
    {
        if (WM_ZOOM_XYZ == uMessage)
        {
            structCoords.dbDesiredX = dbX;
            structCoords.dbDesiredY = dbY;
            structCoords.dbDesiredZ = dbZ;
        }
        bVersionOk = TRUE;
        Marshal.StructureToPtr(structCoords , pvHead, false); // <-- this is what you (I) forgot!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My unix/windows C++ app is already parallelized using MPI: the job is splitted in
I am trying to setup a 'shared' memory location using caddr_t mmap_ptr; But am
Good evening guys! I'm currently designing a desktop interface with various features using Firemonkey/FMX.
I built a client server application using posix shared memory and posix unnamed semaphores
I am trying to share two different using one shared memory block using the
I'm trying to build a client server application using POSIX shared memory and POSIX
On Linux I'm using shmget and shmat to setup a shared memory segment that
Using LINQ on collections, what is the difference between the following lines of code?
using (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();
Using Nunit, I want to be able to write a test fixture that will

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.