Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

 


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);
[/Code]

 

 

Then, at the receiving end the C# server does the following:

[Code]
IntPtr chRequest;
bool fSuccess = ReadFile(hPipeInst, chRequest, uSize, cbRead, OverlappedPtr);
if (fSuccess)
byte[] temp = Encoding.ASCII.GetBytes(Marshal.PtrToStringAnsi(chRequest));
[/Code]

 

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,

  • Leaders
Posted

It looks like you are going from a pointer to a string to a byte array. I'm not sure if that is intentional. If the pointer points to the first char in the cCommand field this should be reasonably simple.

 

Since command is always 8 bytes, you could do some pointer math to get a pointer to the "parameter". I don't foresee any padding/alignment issues here. How you encode the length is up to you (prefixed or zero terminated). But once you have a pointer you can use the Marshal class to get the parameter string.

// Specify 8 chars, no length prefix or null terminator
string command = 
   System.Runtime.InteropServices.Marshal.PtrToStringAnsi(chRequest, 8);

IntPtr lpszParameter = new IntPtr(chRequest.ToInt64() + 8);
// With no len specified, this will work with a null ("\0") terminated string 
string parameter = 
   System.Runtime.InteropServices.Marshal.PtrToStringAnsi(lpszParameter);

ToInt64 (in theory) will work on both 32-bit and 64-bit, but I haven't tested this code on either platform.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...