Using Named Pipes to send variables from one process to another

Hi everybody I'm new to C++ and am trying to do a few things with this code.

First and foremost I'm using a Windows 7 OS.

I've got two processes started with the same executable program. I want to send two variables from one process to another using Named Pipes.

The first process is started by tmp1.cpp which is where I placed my Named Pipe server code and the second is started by tmp2.cpp where my Named Pipe client code is.

This tmp1.cpp file has to finish completely before tmp2.cpp can be executed so I have to place the Named Pipe server code that checks for the connection from client into a thread that runs in the background while the rest of my code executes.

Now I've been reading the MSDN Named Pipes info but I'm having trouble understanding it. I'd love it if somebody could look at my code and help me understand the mistakes I'm making and some of the concepts in there.

This is the code I have right now simplified:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
On the Server file (tmp1.cpp).
Populate the variables:
1
2
3
4
5
6
#include <iostream>
#include <windows.h>
using namespace std;

string x = "username"
string y = "password"

Create the pipe:
1
2
3
4
5
6
7
8
9
10
11
12
LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\u_p_pipe")
HANDLE pipe = CreateNamedPipe
    (
    lpszPipename,
    PIPE_ACCESS_OUTBOUND,
    PIPE_TYPE_BYTE,
    1,
    0,
    0,
    0,
    NULL
);

Then I wait for the client to connect:
 
BOOL result = ConnectNamedPipe(pipe, NULL);


This is where my first problem arises. tmp1.cpp hangs at this point until the client calls this created pipe but since the client (tmp2.cpp) can only be executed when tmp1.cpp finishes I'm stuck at this point.
--------------------------------------------------------------------------------
I'm not sure how to make this client run through a thread. The way I understand it so far is that I place my pipe into a function of some kind and then add that function to the CreateThread function but I'm not exactly sure how the create thread function should be made. Maybe something like this:
1
2
3
4
5
6
7
8
pipeFunction (){ //pipecode}
CreateThread(
NULL, //Don't need to inherit any of the attributes?
0, //Use the defualt size?
pipeFunction, //The function I want to execute
0, //Don't need any variables passed
NULL //Don't need to pass any flags
)

Does this make any sense?
--------------------------------------------------------------------------------
Then when the client connects I send the variables:
1
2
3
4
5
6
7
8
9
LPTSTR *data = &x; //Not sure how to convert std::String to LPTSTR
DWORD numBytesWritten = 0;
result = WriteFile(
    pipe, // handle to our outbound pipe
    data, // data to send
    lstrlen(data)+1)*sizeof(TCHAR), // length of data to send (bytes)
    &numBytesWritten, // will store actual amount of data sent
    NULL // not using overlapped IO
);

My second issue is that I still don't know how to send the variables. I can send some random text to test out the pipe connection but I'm still not sure how to force WriteFile to input a string variable.

And then I close the pipe:
 
            CloseHandle(pipe);

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
This is my client file (tmp2.cpp):
Create the pipe:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <windows.h>
using namespace std;

LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\u_p_pipe")
HANDLE pipe = CreateFile(
    lpszPipename,
    GENERIC_READ, // only need read access
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL
);

Wait until there is some data to be read. This is fine since at this point tmp1.cpp is running and is awaiting the connection call.
1
2
3
4
5
6
7
8
9
chart buffer[128];
DWORD numBytesRead = 0;
BOOL result = ReadFile(
    pipe,
    buffer, // the data from the pipe will be put here
    127 * sizeof(wchar_t), // number of bytes allocated
    &numBytesRead, // this will store number of bytes actually read
    NULL // not using overlapped IO
);

If the connection is true place the data in the variable "x"
1
2
3
4
5
6
7
if (result) {
    buffer[numBytesRead / sizeof(wchar_t)] = '\0'; // null terminate the string
    x = buffer

} else {
    cout << "Failed to read data from the pipe.";
}

Close the pipe
 
CloseHandle(pipe);

Print or use the newly created string variable.
 
cout << x;


So there you go all of my many many issues. I'd really appreciate it if somebody could explain the things I don't understand as simplistically as possible since I'm trying to learn how this works and not just get it to work.

Thanks to everybody that actually reads this huge wall of text.
Last edited on
This topic is far beyond the scope of this forum. Threading is an advanced topic and it should probably be in the windows section, seeing as you are using windows functions and flags.
Hey CodeGoggles thanks I didn't know where Exactly to post this so I'll move it to the windows forum.
Topic archived. No new replies allowed.