0
我想通過管道將數據從C++ DLL文件發送到C#pip服務器。服務器已經編程,並且使用C#客戶端可以正常獲取數據。C++ NamedPipeClientStream發送數據
我簡單的C#的客戶端代碼:
System.IO.Pipes.NamedPipeClientStream pipeClient = new System.IO.Pipes.NamedPipeClientStream(".", "testpipe", System.IO.Pipes.PipeDirection.InOut, System.IO.Pipes.PipeOptions.None);
if (pipeClient.IsConnected != true) { pipeClient.Connect(); }
StreamReader sr = new StreamReader(pipeClient);
StreamWriter sw = new StreamWriter(pipeClient);
try
{
sw.WriteLine("Test Message");
sw.Flush();
pipeClient.Close();
}
catch (Exception ex) { throw ex; }
}
不過,我不來用C實現這一客戶端++。我需要哪些頭文件? 你能給我一個簡單的例子嗎?謝謝!編輯: 感謝您的回覆! 爲了測試它,我創建一個C++程序和編譯現在以下:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE pipe = CreateFile(
L"testpipe",
GENERIC_READ, // only need read access
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (pipe == INVALID_HANDLE_VALUE) {
// look up error code here using GetLastError()
DWORD err = GetLastError();
system("pause");
return 1;
}
// The read operation will block until there is data to read
wchar_t 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 (result) {
buffer[numBytesRead/sizeof(wchar_t)] = '?'; // null terminate the string
// wcout << "Number of bytes read: " << numBytesRead << endl;
// wcout << "Message: " << buffer << endl;
} else {
// wcout << "Failed to read data from the pipe." << endl;
}
// Close our pipe handle
CloseHandle(pipe);
system("pause");
return 0;
return 0;
}
然而,當我運行它,所述(管== INVALID_HANDLE_VALUE)爲真。 調試顯示DWORD err = GetLastError();值爲2,儘管服務器正在運行。 有沒有人有想法?
嗨jmihalicza,謝謝你的回覆!我現在編輯了我的入門文章。你知道我現在必須做什麼嗎? – user1956123 2013-05-07 23:48:57