Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. int _tmain(int argc, LPTSTR argv[]) {
  2. BOOL fSuccess;
  3. int num;
  4.  
  5. hPipe = CreateFile(GATEWAY_PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0 | FILE_FLAG_OVERLAPPED, NULL);
  6.  
  7. if (hPipe == INVALID_HANDLE_VALUE) {
  8. _tprintf(TEXT("[ERRO] Nao foi possivel abrir o named pipe => %d\n"), GetLastError());
  9. return -1;
  10. }
  11.  
  12. if (!WaitNamedPipe(GATEWAY_PIPE_NAME, 20000)) {
  13. printf("[ERRO] Nao foi possivel abrir o named pipe passados 20 segundos\n");
  14. return -1;
  15. }
  16.  
  17. dwMode = PIPE_READMODE_MESSAGE;
  18. fSuccess = SetNamedPipeHandleState(hPipe, &dwMode, NULL, NULL);
  19.  
  20. if (!fSuccess) {
  21. _tprintf(TEXT("[ERRO] SetNamedPipeHandleState falhou => %d\n"), GetLastError());
  22. return -1;
  23. }
  24.  
  25. OVERLAPPED oOverlap = { 0 };
  26. HANDLE hWrite = CreateEvent(NULL, TRUE, FALSE, NULL);
  27.  
  28. while (1) {
  29. printf("Write: ");
  30. scanf_s("%d", &num);
  31.  
  32. ZeroMemory(&oOverlap, sizeof(oOverlap));
  33. oOverlap.hEvent = hWrite;
  34. ResetEvent(hWrite);
  35.  
  36. fSuccess = WriteFile(hPipe, &num, sizeof(int), &cbWritten, &oOverlap);
  37.  
  38. WaitForSingleObject(hWrite, INFINITE);
  39.  
  40. GetOverlappedResult(hPipe, &oOverlap, &cbWritten, FALSE);
  41.  
  42. if (!fSuccess || cbWritten < sizeof(int)) {
  43. _tprintf(TEXT("[ERRO] Escrever no ficheiro do servidor => %d\n"), GetLastError());
  44. return -1;
  45. }
  46.  
  47. if (!fSuccess) {
  48. _tprintf(TEXT("[ERRO] Funcao WriteFile falhou... (%d)\n", GetLastError()));
  49. return -1;
  50. }
  51.  
  52. printf("[INFO] Escrevi no %d no pipe.\n", num);
  53. }
  54.  
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement