Advertisement
Savelyev_Vyacheslav

OS_laba2_slave

May 5th, 2021
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. //Программа Slave
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <windows.h>
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <string.h>
  7. using namespace std;
  8.  
  9. LPWSTR winApiFormat(char text[], wchar_t wtext2[128]) {
  10.     //wchar_t wtext2[128];
  11.     mbstowcs(wtext2, text, strlen(text) + 1);//Plus null
  12.     LPWSTR ptr = wtext2;
  13.     return ptr;
  14. }
  15. void titleSet(char a[]) {
  16.     wchar_t wtext2[128];
  17.     LPWSTR ptr2 = winApiFormat(a, wtext2);
  18.     SetConsoleTitle(ptr2);
  19. }
  20.  
  21. void setCharArg(char* cmd, char* arg) {
  22.     sprintf(cmd + strlen(cmd), " %s", arg);
  23. }
  24. void controlProcess(int argc, char* argv[]) {
  25.     char title[128] = "slave";
  26.     if (argc != 4) {
  27.         cerr << "Slave: Snachalo slave zapustite MASTER.EXE.\n";
  28.         exit(1);
  29.     }
  30.    
  31.     int pid = atoi(argv[1]);
  32.     cout << "----------------" << argv[3] << "\n";
  33.     cout << "----------------" << argv[2] << "\n";
  34.     cout << "----------------" << argv[1] << "\n";
  35.     cout << "----------------" << argv[0] << "\n";
  36.     setCharArg(title, argv[2]);
  37.     setCharArg(title, argv[3]);
  38.     titleSet(title);
  39.  
  40.     HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE,
  41.         FALSE, pid);
  42.     if (!process) cout << "Slave: Oshibka otkrytiay protsessa\n";
  43.     cout << "Slave:Ozhidanie zakrytiay protsessa Master\n";
  44.     cout.flush();
  45.     if (WaitForSingleObject(process, INFINITE) == STATUS_WAIT_0)
  46.     {
  47.         cout << "Slave: Master zavershil rabotu\n";
  48.         cin.get();
  49.     }
  50.     else
  51.         cout << "Slave: Не известная ошибка\n";
  52.     exit(0);
  53.     cout << "Slave: EXIT\n";
  54.     cin.get();
  55. }
  56.  
  57.  
  58. int main(int argc, char* argv[]) {
  59.  
  60.     cout << "Slave: start ...\n";
  61.     //cin.get();
  62.     controlProcess(argc, argv);
  63.  
  64.  
  65.     //cin.get();
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement