C++ Named Pipes and Looping

Viewed 40

I am creating two apps in windows, one is c# and another one is c++. I need to transfer data between them, So I used Named Pipes. The problem is C# sends the code like commands.

For example:

Cmd1: C++ Open window
Cmd2: C++ Close window

My Current C++ Code is:

// Function Readstring
void ReadString(char* output)
{
    ULONG read = 0;
    int index = 0;
    do
    {
        ReadFile(fileHandle, output + index++, 1, &read, NULL);
    } while (read > 0 && *(output + index - 1) != 0);
}

// Connect with the pipe
fileHandle = CreateFileW(TEXT("\\\\.\\pipe\\Commander"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

// Read Buffer
char* buffer = new char[100];
memset(buffer, 0, 100);
ReadString(buffer);

This is executed in separate thread to avoid blocking other processes. But the reading stops once it received just one command (Ex: Cmd:1 C++ Open window)

How to write C++ code to listen for all commands instead of stopping in just one command. (I have rough idea that, I can do it with while loop.) But I don't know how to do it in good way and when to do execute flush() command.

0 Answers
Related