1

I have an API for a game, which calls methods in a C++ dll, you can write bots for the game by modifying the DLL and calling certain methods. This is fine, except I'm not a big fan of C++, so I decided to use named pipes so I can send game events down the pipe to a client program, and send commands back - then the C++ side is just a simple framework for sending a listening on a named pipe.

I have some methods like this on the C# side of things:

private string Read()
{
 byte[] buffer = new byte[4096];
 int bytesRead = pipeStream.Read(buffer, 0, (int)4096);
 ASCIIEncoding encoder = new ASCIIEncoding();
 return encoder.GetString(buffer, 0, bytesRead);
}
private void Write(string message)
{
 ASCIIEncoding encoder = new ASCIIEncoding();
 byte[] sendBuffer = encoder.GetBytes(message);
 pipeStream.Write(sendBuffer, 0, sendBuffer.Length);
 pipeStream.Flush();
}

What would be the equivalent methods on the C++ side of things?

asked Jan 14, 2010 at 10:39

1 Answer 1

1

After you create the pipe and have pipe handles, you read and write using the ReadFile and WriteFile APIs: see Named Pipe Client in MSDN for a code example.


However, I'm at loss exactly how to use them.

The "Named Pipe Client" section which I quoted above gives an example of how to use them.

For example, what are the types of all the arguments

The types of all the arguments are defined in MSDN: see ReadFile and WriteFile

how do I convert from a buffer of bytes which I presumably receive from the ReadFile method into a string and vice/versa?

You're sending the string using ASCIIEncoding, so you'll receive a string of non-Unicode characters.

You can convert that to a string, using the overload of the std::string constructor which takes a pointer to a character buffer plus a sencond parameter which specifies how many characters are in the buffer:

//chBuf and cbRead are as defined in the
//MSDN "Named Pipe Client" example code fragment
std::string received((const char*)chBuf, cbRead);
answered Jan 14, 2010 at 10:49
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I understand that ReadFile and WriteFile are equivalent to pipestrea.Write/Read in my example. However, I'm at loss exactly how to use them. For example, what are the types of all the arguments, how do I convert from a buffer of bytes which I presumably receive from the ReadFile method into a string and vice/versa?
I edited my answer to answer the additional questions in your comment.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.