Guest User

Untitled

a guest
Nov 18th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. #pragma comment(lib, "user32.lib")
  6.  
  7.  
  8. void TrackChildProcess(LPVOID param)
  9. {
  10.     while (1)
  11.     {
  12.         STARTUPINFO si;
  13.         PROCESS_INFORMATION pi;
  14.         CHAR szExePath[MAX_PATH];
  15.         CHAR szCmdLine[MAX_PATH];
  16.         DWORD pid = GetCurrentProcessId();
  17.  
  18.         GetModuleFileName(GetModuleHandle(NULL), szExePath, MAX_PATH);
  19.         wsprintf(szCmdLine, "%s %d\0", szExePath, pid);
  20.  
  21.         ZeroMemory(&si, sizeof(STARTUPINFO));
  22.         si.cb = sizeof(STARTUPINFO);
  23.         CreateProcess(NULL,
  24.             szCmdLine,
  25.             NULL,
  26.             NULL,
  27.             FALSE,
  28.             CREATE_NEW_CONSOLE,
  29.             NULL,
  30.             NULL,
  31.             &si,
  32.             &pi);
  33.  
  34.         WaitForSingleObject(pi.hProcess, INFINITE);
  35.     }
  36. }
  37.  
  38. void TrackParentProcess(LPVOID hParentProcess)
  39. {
  40.     STARTUPINFO si;
  41.     PROCESS_INFORMATION pi;
  42.     CHAR szExePath[MAX_PATH];
  43.  
  44.     WaitForSingleObject((HANDLE)hParentProcess, INFINITE);
  45.  
  46.     GetModuleFileName(GetModuleHandle(NULL), szExePath, MAX_PATH);
  47.  
  48.     ZeroMemory(&si, sizeof(STARTUPINFO));
  49.     si.cb = sizeof(STARTUPINFO);
  50.     CreateProcess(NULL,
  51.         szExePath,
  52.         NULL,
  53.         NULL,
  54.         FALSE,
  55.         CREATE_NEW_CONSOLE,
  56.         NULL,
  57.         NULL,
  58.         &si,
  59.         &pi);
  60.     ExitProcess(0);
  61. }
  62.  
  63. void ShowMainWindow(void);
  64.  
  65. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  66. {
  67.     HANDLE hParentProcess = NULL;
  68.     DWORD  ProcessID = 0;
  69.  
  70.     if (lpCmdLine != NULL)
  71.         ProcessID = atoi(lpCmdLine);
  72.  
  73.     if (ProcessID == 0)
  74.     {
  75.         CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TrackChildProcess, NULL, 0, NULL);
  76.     }
  77.     else
  78.     {
  79.         hParentProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
  80.         if (hParentProcess)
  81.         {
  82.             CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TrackParentProcess, (LPVOID)hParentProcess, 0, NULL);
  83.             while (1)
  84.             {
  85.                 Sleep(500);
  86.             }
  87.         }
  88.     }
  89.  
  90.     // Main logic goes here...
  91.     ShowMainWindow();
  92. }
  93.  
  94. void RegisterWndClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground)
  95. {
  96.     WNDCLASSEX wc;
  97.  
  98.     wc.cbSize = sizeof(wc);
  99.     wc.style = CS_HREDRAW | CS_VREDRAW;
  100.     wc.lpfnWndProc = Proc;
  101.     wc.cbClsExtra = 0;
  102.     wc.cbWndExtra = 0;
  103.     wc.hInstance = GetModuleHandle(NULL);
  104.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  105.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  106.     wc.hbrBackground = (HBRUSH)brBackground;
  107.     wc.lpszMenuName = NULL;
  108.     wc.lpszClassName = szName;
  109.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  110.  
  111.     if (!RegisterClassEx(&wc))
  112.     {
  113.         MessageBox(NULL, TEXT("Cannot register class"), TEXT("Error"), MB_OK);
  114.     }
  115. }
  116.  
  117. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  118. {
  119.     HDC hDC;
  120.     PAINTSTRUCT ps;
  121.     RECT rect;
  122.  
  123.     switch (uMsg)
  124.     {
  125.     case WM_CREATE:
  126.         break;
  127.     case WM_PAINT:
  128.         hDC = BeginPaint(hWnd, &ps);
  129.         GetClientRect(hWnd, &rect);
  130.         EndPaint(hWnd, &ps);
  131.         break;
  132.     case WM_SIZE:
  133.         break;
  134.     case WM_CLOSE:
  135.         DestroyWindow(hWnd);
  136.         break;
  137.     case WM_DESTROY:
  138.         PostQuitMessage(0);
  139.         break;
  140.     default:
  141.         return DefWindowProc(hWnd, uMsg, wParam, lParam);
  142.     }
  143.     return 0;
  144. }
  145.  
  146. void ShowMainWindow(void)
  147. {
  148.     HWND hMainWnd;
  149.     MSG msg;
  150.  
  151.     RegisterWndClass(WndProc, TEXT("Info"), COLOR_WINDOW);
  152.     hMainWnd = CreateWindow(TEXT("Info"), TEXT("Info"),
  153.         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
  154.         CW_USEDEFAULT, 0, 250, 130, (HWND)NULL, (HMENU)NULL,
  155.         (HINSTANCE)GetModuleHandle(NULL), NULL);
  156.  
  157.     if (!hMainWnd)
  158.     {
  159.         MessageBox(NULL, TEXT("Can\'t create main window."), TEXT("Error"), MB_OK);
  160.         return GetLastError();
  161.     }
  162.  
  163.     ShowWindow(hMainWnd, SW_SHOW);
  164.  
  165.     while (GetMessage(&msg, NULL, 0, 0))
  166.     {
  167.         TranslateMessage(&msg);
  168.         DispatchMessage(&msg);
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment