Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int _tmain(int argc, LPTSTR argv[]) {
- BOOL fSuccess;
- int num;
- hPipe = CreateFile(GATEWAY_PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0 | FILE_FLAG_OVERLAPPED, NULL);
- if (hPipe == INVALID_HANDLE_VALUE) {
- _tprintf(TEXT("[ERRO] Nao foi possivel abrir o named pipe => %d\n"), GetLastError());
- return -1;
- }
- if (!WaitNamedPipe(GATEWAY_PIPE_NAME, 20000)) {
- printf("[ERRO] Nao foi possivel abrir o named pipe passados 20 segundos\n");
- return -1;
- }
- dwMode = PIPE_READMODE_MESSAGE;
- fSuccess = SetNamedPipeHandleState(hPipe, &dwMode, NULL, NULL);
- if (!fSuccess) {
- _tprintf(TEXT("[ERRO] SetNamedPipeHandleState falhou => %d\n"), GetLastError());
- return -1;
- }
- OVERLAPPED oOverlap = { 0 };
- HANDLE hWrite = CreateEvent(NULL, TRUE, FALSE, NULL);
- while (1) {
- printf("Write: ");
- scanf_s("%d", &num);
- ZeroMemory(&oOverlap, sizeof(oOverlap));
- oOverlap.hEvent = hWrite;
- ResetEvent(hWrite);
- fSuccess = WriteFile(hPipe, &num, sizeof(int), &cbWritten, &oOverlap);
- WaitForSingleObject(hWrite, INFINITE);
- GetOverlappedResult(hPipe, &oOverlap, &cbWritten, FALSE);
- if (!fSuccess || cbWritten < sizeof(int)) {
- _tprintf(TEXT("[ERRO] Escrever no ficheiro do servidor => %d\n"), GetLastError());
- return -1;
- }
- if (!fSuccess) {
- _tprintf(TEXT("[ERRO] Funcao WriteFile falhou... (%d)\n", GetLastError()));
- return -1;
- }
- printf("[INFO] Escrevi no %d no pipe.\n", num);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement