【发布时间】:2021-07-09 11:07:49
【问题描述】:
我有 C++ 代码通过管道发送一些字符串(在标准主函数中):
HANDLE pipe = CreateNamedPipe(L"\\\\.\\pipe\\example",
PIPE_ACCESS_OUTBOUND, PIPE_TYPE_BYTE, 1, 0, 0, 0, NULL);
if (pipe == NULL || pipe == INVALID_HANDLE_VALUE) {
cout << "Failed to create outbound pipe instance.";
system("pause");
return 1;
}
cout << "Waiting for a client to connect to the pipe..." << endl;
// This call blocks until a client process connects to the pipe
BOOL result = ConnectNamedPipe(pipe, NULL);
if (!result) {
cout << "Failed to make connection on named pipe." << endl;
CloseHandle(pipe); // close the pipe
system("pause");
return 1;
}
cout << "Sending data to pipe..." << endl;
// This call blocks until a client process reads all the data
const wchar_t *data = L"*** Hello Pipe World ***";
DWORD numBytesWritten = 0;
result = WriteFile(pipe, data, wcslen(data) * sizeof(wchar_t), &numBytesWritten, NULL);
if (result) {
cout << "Number of bytes sent: " << numBytesWritten << endl;
} else {
cout << "Failed to send data." << endl;
}
CloseHandle(pipe);
我也有 Python 代码来接收这个:
quit = False
while not quit:
try:
handle = win32file.CreateFile(
"\\\\.\\pipe\\example",
win32file.GENERIC_READ,
0,
None,
win32file.OPEN_EXISTING,
0,
None
)
res = win32pipe.SetNamedPipeHandleState(handle, win32pipe.PIPE_READMODE_MESSAGE, None, None)
if res == 0:
print(f"SetNamedPipeHandleState return code: {res}")
while True:
resp = win32file.ReadFile(handle, 64*1024)
print(f"message: {resp}")
except pywintypes.error as e:
print(e.args[0])
quit = True
我首先运行 C++ 代码,然后启动我的 Python 脚本。 C++ 的最后一个输出是“将数据发送到管道...”。所以我收到错误 231,这意味着“所有管道实例都忙”。
我需要什么来解决这个问题?
P。 S. Python 3.6、Windows 7、C++ 11。
【问题讨论】:
标签: python c++ pipe ipc pywin32