Created
February 19, 2018 22:48
-
-
Save udaken/5cc50ca8a223808bf586113336af7424 to your computer and use it in GitHub Desktop.
名前付きパイプを利用して、 TransactNamedPipe() を行うサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "stdafx.h" | |
| #define NOMINMAX | |
| #define WIN32_LEAN_AND_MEAN | |
| #define STRICT | |
| #include <windows.h> | |
| #include <thread> | |
| int main() | |
| { | |
| fprintf(stdout, "[S]Start Server\r\n"); | |
| auto hNamedPipe = CreateNamedPipe(_T("\\\\.\\pipe\\TEST"), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 2, 0, 0, 3000, NULL); | |
| if (hNamedPipe == INVALID_HANDLE_VALUE) | |
| { | |
| fprintf(stderr, "CreateNamedPipe() Error: 0x%08X\r\n", GetLastError()); | |
| return 1; | |
| } | |
| std::thread t([]() { | |
| fprintf(stdout, "[C]Start Client\r\n"); | |
| auto hNamedPipe = CreateFile(L"\\\\.\\pipe\\TEST", GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); | |
| if (hNamedPipe == INVALID_HANDLE_VALUE) | |
| { | |
| fprintf(stderr, "CreateFile() Error: 0x%08X\r\n", GetLastError()); | |
| return; | |
| } | |
| DWORD dwError = GetLastError(); | |
| // Exit if an error other than ERROR_PIPE_BUSY occurs. | |
| if (dwError != ERROR_SUCCESS && dwError != ERROR_PIPE_BUSY) | |
| { | |
| fprintf(stderr, "Could not open pipe: 0x%08X\r\n", dwError); | |
| return; | |
| } | |
| DWORD dwMode = PIPE_READMODE_MESSAGE; | |
| if(!SetNamedPipeHandleState( | |
| hNamedPipe, // pipe handle | |
| &dwMode, // new pipe mode | |
| NULL, // don't set maximum bytes | |
| NULL) // don't set maximum time | |
| ) | |
| { | |
| fprintf(stderr, "SetNamedPipeHandleState() failed. 0x%08X\r\n", GetLastError()); | |
| } | |
| BYTE buf[64]; | |
| DWORD dwBytesRead; | |
| BYTE inBytes[] = "brabrabra"; | |
| fprintf(stdout, "[C]Start Client\r\n"); | |
| //if (!CallNamedPipe(_T("\\\\.\\pipe\\TEST"), inBytes, 4, buf, _countof(buf), &dwBytesRead, 1000)) | |
| if (!TransactNamedPipe(hNamedPipe, inBytes, _countof(inBytes), buf, _countof(buf), &dwBytesRead, nullptr)) | |
| { | |
| fprintf(stderr, "TransactNamedPipe() failed. 0x%08X\r\n", GetLastError()); | |
| } | |
| CloseHandle(hNamedPipe); | |
| fprintf(stdout, "[C]End Client\r\n"); | |
| }); | |
| if (!ConnectNamedPipe(hNamedPipe, nullptr)) | |
| { | |
| fprintf(stderr, "[S]ConnectNamedPipe() Error: 0x%08X\r\n", GetLastError()); | |
| CloseHandle(hNamedPipe); | |
| return 1; | |
| } | |
| fprintf(stdout, "[S]Read from pipe\r\n"); | |
| BYTE buf[64] = {}; | |
| DWORD dwBytesRead; | |
| if (!ReadFile(hNamedPipe, buf, _countof(buf), &dwBytesRead, nullptr)) | |
| { | |
| fprintf(stderr, "[S]ReadFile() Error: 0x%08X\r\n", GetLastError()); | |
| } | |
| fprintf(stdout, "[S]Write to pipe\r\n"); | |
| DWORD dwBytesWrite; | |
| if (!WriteFile(hNamedPipe, buf, _countof(buf), &dwBytesWrite, nullptr)) | |
| { | |
| fprintf(stderr, "[S]ReadFile() Error: 0x%08X\r\n", GetLastError()); | |
| } | |
| t.join(); | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment