Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <string.h>
- #include <stdio.h>
- #pragma comment(lib, "user32.lib")
- void TrackChildProcess(LPVOID param)
- {
- while (1)
- {
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- CHAR szExePath[MAX_PATH];
- CHAR szCmdLine[MAX_PATH];
- DWORD pid = GetCurrentProcessId();
- GetModuleFileName(GetModuleHandle(NULL), szExePath, MAX_PATH);
- wsprintf(szCmdLine, "%s %d\0", szExePath, pid);
- ZeroMemory(&si, sizeof(STARTUPINFO));
- si.cb = sizeof(STARTUPINFO);
- CreateProcess(NULL,
- szCmdLine,
- NULL,
- NULL,
- FALSE,
- CREATE_NEW_CONSOLE,
- NULL,
- NULL,
- &si,
- &pi);
- WaitForSingleObject(pi.hProcess, INFINITE);
- }
- }
- void TrackParentProcess(LPVOID hParentProcess)
- {
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- CHAR szExePath[MAX_PATH];
- WaitForSingleObject((HANDLE)hParentProcess, INFINITE);
- GetModuleFileName(GetModuleHandle(NULL), szExePath, MAX_PATH);
- ZeroMemory(&si, sizeof(STARTUPINFO));
- si.cb = sizeof(STARTUPINFO);
- CreateProcess(NULL,
- szExePath,
- NULL,
- NULL,
- FALSE,
- CREATE_NEW_CONSOLE,
- NULL,
- NULL,
- &si,
- &pi);
- ExitProcess(0);
- }
- void ShowMainWindow(void);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- HANDLE hParentProcess = NULL;
- DWORD ProcessID = 0;
- if (lpCmdLine != NULL)
- ProcessID = atoi(lpCmdLine);
- if (ProcessID == 0)
- {
- CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TrackChildProcess, NULL, 0, NULL);
- }
- else
- {
- hParentProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
- if (hParentProcess)
- {
- CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TrackParentProcess, (LPVOID)hParentProcess, 0, NULL);
- while (1)
- {
- Sleep(500);
- }
- }
- }
- // Main logic goes here...
- ShowMainWindow();
- }
- void RegisterWndClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground)
- {
- WNDCLASSEX wc;
- wc.cbSize = sizeof(wc);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = Proc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = GetModuleHandle(NULL);
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)brBackground;
- wc.lpszMenuName = NULL;
- wc.lpszClassName = szName;
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if (!RegisterClassEx(&wc))
- {
- MessageBox(NULL, TEXT("Cannot register class"), TEXT("Error"), MB_OK);
- }
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- HDC hDC;
- PAINTSTRUCT ps;
- RECT rect;
- switch (uMsg)
- {
- case WM_CREATE:
- break;
- case WM_PAINT:
- hDC = BeginPaint(hWnd, &ps);
- GetClientRect(hWnd, &rect);
- EndPaint(hWnd, &ps);
- break;
- case WM_SIZE:
- break;
- case WM_CLOSE:
- DestroyWindow(hWnd);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- return 0;
- }
- void ShowMainWindow(void)
- {
- HWND hMainWnd;
- MSG msg;
- RegisterWndClass(WndProc, TEXT("Info"), COLOR_WINDOW);
- hMainWnd = CreateWindow(TEXT("Info"), TEXT("Info"),
- WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
- CW_USEDEFAULT, 0, 250, 130, (HWND)NULL, (HMENU)NULL,
- (HINSTANCE)GetModuleHandle(NULL), NULL);
- if (!hMainWnd)
- {
- MessageBox(NULL, TEXT("Can\'t create main window."), TEXT("Error"), MB_OK);
- return GetLastError();
- }
- ShowWindow(hMainWnd, SW_SHOW);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment