I found this StackOverflow question:
And created this class:
#pragma once
#define PIPE_FD TEXT("\\\\.\\pipe\\somepipe")
#define BUFFER_SIZE 1024
// CPipe Class
class CPipe
{
private:
//
// Variables
//
HANDLE hPipe;
char buffer[BUFFER_SIZE];
DWORD dwRead;
DWORD dwWritten;
public:
bool CreatePipe()
{
hPipe = CreateNamedPipe(PIPE_FD, PIPE_ACCESS_DUPLEX | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, PIPE_WAIT, 1, BUFFER_SIZE * 16, BUFFER_SIZE * 16, NMPWAIT_USE_DEFAULT_WAIT, NULL);
return (hPipe == NULL) ? false : true;
}
bool CreatePipeFile()
{
hPipe = CreateFile(PIPE_FD, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
return (hPipe == NULL) ? false : true;
}
void Destroy()
{
DisconnectNamedPipe(hPipe);
}
bool IsPipe()
{
return (hPipe == NULL) ? false : true;
}
bool IsConnected()
{
return (ConnectNamedPipe(hPipe, NULL) != FALSE) ? true : false;
}
void Read()
{
while (ReadFile(hPipe, buffer, sizeof(buffer), &dwRead, NULL) != FALSE)
{
/* do something with data in buffer */
printf("%s", buffer);
}
}
void Write()
{
WriteFile(hPipe, "Hello Pipe\n", 12, &dwWritten, NULL);
CloseHandle(hPipe);
}
};
extern CPipe gPipe;
Main Process:
gPipe.CreatePipe();
while (gPipe.IsPipe())
{
if (gPipe.IsConnected())
{
gPipe.Read();
}
gPipe.Destroy();
}
Remote:
gPipe.CreatePipeFile();
if (gPipe.IsPipe())
{
gPipe.Write();
}
Which works great. I can send "Hello Pipe\n" between two applications. However, I am trying to modify it to send data structures rather than strings.
For example, this structure:
struct Test_t
{
int x;
int y;
float J[3];
bool Yes;
};
That way, the client can send the structure over the pipe, and the server can read the structure off the pipe and update local server structs accordingly.
I have tried:
reinterpret_cast<char*>(&test);
But I haven't been able to get it to work. Any ideas?
Any help is appreciated.