I have an old client side application that is writing to named pipes using a VC 6.0 dll, and I have been asked to write an C# application to read the named pipe and process the request. I am able to receive the data in a byte array, but can’t get the data in the pipe to match with the structures i have defined in C#.
old C struct
typedef struct
{
WORD WtYPE;
AB objEmbededStruct1;
BB objEmbededStruct2;
char szString[13];
union
{
char szString1[25];
char szSTring2[45];
char szString3[134];
}
BOOL bExist;
} myStruct1;
typedef struct
{
char szThisString1[2];
int iFlag1;
char szThisString2[11];
}AB;
typedef struct
{
HANDLE hEvents[2];
DWORD dw;
int ithisFlag;
}BB;
I have tried parsing the byte array, but the data is not where I expect it to be. For instance, the first string in the first embedded structure (AB) starts at byte[4] as opposed to byte[2] since a word maps to an unsigned int16. Then the first integer in the AB struct starts at byte[8] as opposed to byte[6]. So, is there a more efficient way to retrieve the data from the pipe and put it into the structure, or is parsing by bytes the correct way? If parsing the bytes is how it should be done, then what am I missing when trying to map where the data should be?
Thanks
after a suggestion from LU RD, i was able to put this solution together:
definition of VC 6 structs:
I needed to the using statement to the c# code:
The struct definition in c# looks like this:
the code to pull the stream from the named pipe into the struct looks like this:
to use the char[]’s in the structure, I used:
string myWorkString = new string( objData.szString);
to return the data to the pipe — I reversed the process:
the following link was a big help, thanks to that author as well!
Mastering c# structs