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

  • SEARCH
  • Home
  • 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 7972323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:50:08+00:00 2026-06-04T07:50:08+00:00

I was given a block of c++ code that looks like it was from

  • 0

I was given a block of c++ code that looks like it was from a c++ app that makes use of
Shared Memory for sending messages to other programs.

The c++ code has no #include or anything yet. I was given the code to use in my C# application and I am pretty stuck. I somewhat understand what the code does, but I don’t know it well enough to translate it to C# as I am pretty new to coding.

My question is, what is the easiest way to be able to use the functionality of the code in my project? The end result is to send messages to another program, that will in turn do something that I’m not worried about.

I have tried creating different c++ projects and file types in my solution to link them using a reference later on, but I can never get it to compile properly.

Please let me know if you have some advice or a good place to look. I can always provide more information.

Code (I had to remove comments, sorry):

UINT WM_HELO_ZOOM_XYZ = RegisterWindowMessage("WM_HELO_ZOOM_XYZ");


int HELO_Broadcast_Zoom_Message(
  double dbX,
  double dbY,
  double dbZ,
  UINT uMessage=WM_HELO_ZOOM_XYZ) {

  #ifndef HELO_ 
    typedef struct { 
      UINT uMajVersion; 
      UINT uMinVersion; 
      DWORD dwReserved;
      double dbX;  
      double dbY;
      double dbZ;
    } HELOCoordsStruct;
  #endif


  char *szSharedMemory = "HELO-_Coords"; 
  char szErr[_MAX_PATH*3];
  HANDLE hMem = OpenFileMapping(FILE_MAP_WRITE, FALSE, szSharedMemory); 
  if (NULL == hMem) {
    return(0);
  }

  void *pvHead = MapViewOfFile(hMem, FILE_MAP_WRITE, 0,0,0);
  if (NULL == pvHead)  {
    CloseHandle(hMem);
    sprintf(szErr, "Unable to view", szSharedMemory);
    AfxMessageBox(szErr, MB_OK|MB_ICONSTOP);
    return(0);
  }

  HELOCoordsStruct *pHELOCoords = (HELOCoordsStruct *)pvHead;

  BOOL bVersionOk=FALSE; 

  if (1 == pHELOCoords->uMajorVersion) {

    if (WM_HELO_ZOOM_XYZ==uMessage) { 
      pHELOCoords->dbX = dbX;
      pHELOCoords->dbY = dbY;
      pHELOCoords->dbZ = dbZ;
    }
    bVersionOk=TRUE;
  }
  else {

    sprintf(szErr, "Unrecognized HELO- shared memory version: %d.%d", pHELOCoords->uMajVersion, pHELOCoords->uMinVersion);
    AfxMessageBox(szErr, MB_OK);
  }

  if (NULL != hMem) CloseHandle(hMem);
  UnmapViewOfFile(pvHead);

  if (bVersionOk) {
    PostMessage(HWND_BROADCAST,uMessage,0,0); 
    return(1); 
  }
  else return(0);
}

EDIT: The feedback has been completely unreal. I must say that the community sure spoils folks around here.

Thanks,
Kevin

  • 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-04T07:50:09+00:00Added an answer on June 4, 2026 at 7:50 am

    I think you have three options:

    1. Create a Managed C++ project of type Class Library, put the code in it, make a reference from your main app to this project.
    2. Create an unmanaged C++ DLL project, put the code in a function (or functions), export the function(s) (using .def file), and build the project. Use the functions from that dll using [DllImport] attribute. (See here and here)
    3. Convert the code to C#. This will require some knowledge of unmanaged code, Win32 and P/Invoke (See here and here). And as I see your code, it is takes a little time!

    Here is your converted code (option 3):

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace UnmanagedBlock
    {
        public class ConvertedClass
        {
            public uint WM_HELO_ZOOM_XYZ = RegisterWindowMessageA("WM_HELO_ZOOM_XYZ"); // Your code uses the ANSI string
    
            int HELO_Broadcast_Zoom_Message(
                double dbX, double dbY, double dbZ, uint uMessage) // Porting the default value for 'uMessage' is not possible
            {
                string szSharedMemory = "HELO-_Coords";
                IntPtr hMem = OpenFileMapping(FileMapAccessRights.Write, FALSE, szSharedMemory);
                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 " + szSharedMemory, // Your code does not concat these two strings.
                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return 0;
                }
    
                HELOCoordsStruct pHELOCoords = new HELOCoordsStruct();
                Marshal.PtrToStructure(pvHead, pHELOCoords);
    
                int bVersionOk = FALSE;
    
                if (1 == pHELOCoords.uMajVersion) // I think it had a typo (it was uMajorVersion)
                {
                    if (WM_HELO_ZOOM_XYZ == uMessage)
                    {
                        pHELOCoords.dbX = dbX;
                        pHELOCoords.dbY = dbY;
                        pHELOCoords.dbZ = dbZ;
                    }
                    Marshal.StructureToPtr(pHELOCoords, pvHead, false);
                    bVersionOk = TRUE;
                }
                else
                {
                    MessageBox.Show(
                        "Unrecognized HELO- shared memory version: " +
                        pHELOCoords.uMajVersion.ToString() + "." + pHELOCoords.uMinVersion.ToString());
                }
    
                if (IntPtr.Zero != hMem)
                    CloseHandle(hMem);
                UnmapViewOfFile(pvHead);
    
                if (bVersionOk == TRUE)
                {
                    PostMessage(HWND_BROADCAST, uMessage, 0, 0);
                    return 1;
                }
                else
                    return 0;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private class HELOCoordsStruct
            {
                public uint uMajVersion;
                public uint uMinVersion;
                public uint dwReserved;
                public double dbX;
                public double dbY;
                public double dbZ;
            }
    
            [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);
    
            public const int FALSE = 0, TRUE = 1;
    
            public enum FileMapAccessRights : uint
            {
                Write = 0x2,
                Read = 0x4,
                Execute = 0x20,
            }
    
            public const IntPtr HWND_BROADCAST = (IntPtr)0xffff;
        }
    }
    

    I’ve done an exact conversion and I think that it should work fine, however I have not tested it.

    Let me know if it works.

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

Sidebar

Related Questions

Given a large text block from a WYSIWYG like: Lorem ipsum dolor sit amet,
given i have the following block of code (function(){ var mb = { abc:function(){
I'm working on an android app that has to send and receive information from
I have a WPF app that from time to time needs to perform a
I tried to use forall to allocate dynamic arrays, but gfortran didn't like that.
Sometimes when I run my application it gives me an error that looks like:
I'd like to test a Grails controller that is sending out emails using the
I want to give a static javascript block of code to a html template
I need to obtain the number of parameters a given block takes. For example:
Is there a way to block a given change based on the user role?

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.