C++ 守护进程-并且添加开机自启动

#include 
#include 
#include 
#include 

bool SetStartupRegistry(const TCHAR* appName, const TCHAR* appPath) {
    HKEY hKey;
    LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_SET_VALUE, &hKey);
    if (result != ERROR_SUCCESS) {
        _tprintf(_T("Error opening registry key. Error code: %d\n"), result);
        return false;
    }

    result = RegSetValueEx(hKey, appName, 0, REG_SZ, (BYTE*)appPath, (_tcslen(appPath) + 1) * sizeof(TCHAR));
    RegCloseKey(hKey);

    if (result != ERROR_SUCCESS) {
        _tprintf(_T("Error setting registry value. Error code: %d\n"), result);
        return false;
    }

    return true;
}

bool isProcessRunning(const TCHAR* processName) {
    DWORD processes[1024], cbNeeded, cProcesses;
    if (!EnumProcesses(processes, sizeof(processes), &cbNeeded)) {
        std::cerr << "Error enumerating processes." << std::endl;
        return false;
    }

    cProcesses = cbNeeded / sizeof(DWORD);

    for (DWORD i = 0; i < cProcesses; i++) {
        if (processes[i] != 0) {
            TCHAR szProcessName[MAX_PATH] = TEXT("");
            HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processes[i]);
            if (hProcess != NULL) {
                HMODULE hMod;
                DWORD cbNeeded;

                if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                    GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName) / sizeof(TCHAR));
                }
                CloseHandle(hProcess);

                if (_tcscmp(szProcessName, processName) == 0) {
                    return true;
                }
            }
        }
    }

    return false;
}

bool StartExecutable(const TCHAR* exePath) {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    if (!CreateProcess(NULL, const_cast<TCHAR*>(exePath), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
        _tprintf(_T("CreateProcess failed (%d).\n"), GetLastError());
        return false;
    }

    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    return true;
}


int main(int argc, char* argv[]) {
    // Hide the console window
    ShowWindow(GetConsoleWindow(), SW_HIDE);
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " " << std::endl;
        return 1;
    }
    const TCHAR* processName = _T(argv[1]);
    const TCHAR* processPath = _T(argv[2]);
    SetStartupRegistry(processName, processPath);
    while (true)
    {
        Sleep(1000);
        if (isProcessRunning(processName)) {
            std::cout << "Process is already running." << processName << std::endl;
        }
        else {
            std::cout << "Process is not running. Starting it..." << processName << std::endl;
            StartExecutable(_T(argv[2]));
            exit(0);
            // Add code here to start the process using CreateProcess() or other methods
        }
    }
    

    return 0;
}
调用 pr.exe 进程名 执行文件完整目录

你可能感兴趣的:(c++,开发语言,windows)