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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:24:00+00:00 2026-05-12T09:24:00+00:00

I am using Named Pipes to transfer data from a client (C++) to a

  • 0

I am using Named Pipes to transfer data from a client (C++) to a server (C#), the client does the following:

// C++ Client sending message
struct MESSAGE
   {
   char    cCommand[8];
   string  sParameter;
   };

MESSAGE msg;
strcpy(msg.cCommand, "COMMAND");
strcpy(msg.sParameter, "DO SOMETHING");

DWORD dwWrote = 0;
WriteFile (hpipe, &msg, sizeof(msg), dwWrote, NULL);


// Then, at the receiving end the C# server recieve message:
IntPtr chRequest;
bool fSuccess = ReadFile(hPipeInst, chRequest, uSize, cbRead, OverlappedPtr);
if (fSuccess)
        byte[] temp = Encoding.ASCII.GetBytes(Marshal.PtrToStringAnsi(chRequest));

Now, at the receiving end, I need to transform the temp (byte[]) back into the STRUCT or something equivalent so I can access the members cCommand and sPatameter – but at this point I have no clue how to proceed… In reality doesn’t need to be a struct, I just need to extract the data itself.

Note – the STRUCT MESSAGE is something I came up with, meaning that it can be changed if a different format would be helpful in the reconstruction (add the length of sParameter for example?), I just need a COMMAND and PARAMETER to be transfered in a single block (if possible).

Requirements are simple:
– COMMAND is a fixed-length 8-characters long string that indicates what action needs to be performed
– PARAMETER is a variable-length (unless this causes issues) parameter dependant on each COMMAND

For example:
COMMAND = TRANS

PARAMETER = C:\FILE.txt C:\NewFolder\FILE.TXT

(this is just to illustrate, there are a lot more applications)

If possible I would like to extract it as a chunk of data (byte[]) and then pass it along to my application where it could be decomposed, not a fan of reading in the size, then a field, then a size, then a field – that requires that my Communication be overly linked with my implementation.

If there is a more suitable way to implement this transfer please let me know… advice would be welcome…
Any help would be much appreciated.
Thanks,

  • 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-12T09:24:00+00:00Added an answer on May 12, 2026 at 9:24 am

    If you have the definitions of the struct in c++ you can define it in c# (it may require a little finagling). Then just read the size off of the socket and use Marshal.PtrToStructure to marshal it to the c# defined structure.

    /* Define you structure like this (possibly) */
    struct MESSAGE
    {
      [MarshalAs(UnmanageType.ByValArray, SizeConst=8)]
      byte[]    cCommand;
      [MarshalAs(UnmanagedType.LPStr)]
      string  sParameter;
    };
    
    /* Read data into an instance of MESSAGE like this */
    byte[] bytes = new byte[Marshal.SizeOf(MESSAGE)];
    
    socket.Receive(bytes, 0, bytes.Length);
    IntPtr ptr = Marshal.AllocHGlobal(bytes.Length);
    
    try
    {
        Marshal.Copy(bytes, 0, ptr, bytes.Length);
        m = (MESSAGE)Marshal.PtrToStructure(ptr, typeof(MESSAGE));
    }
    finally
    {
        Marshal.FreeHGlobal(ptr);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.