0
Windows가 시작되어 백그라운드에서 실행될 때 자동으로 C++ 프로그램을 시작하도록하고 싶습니다. 나는 그것에 대해 무언가를 검색하여 C++ 프로그램을 등록하여 Windows 서비스가되도록 할 수 있으므로 Windows가 시작될 때 프로그램이 자동으로 실행될 수 있습니다. 이 코드를 Add Application to Startup (Registry)에 복사하고 코드를 실행하지만 컴퓨터 관리 -> 서비스의 레코드를 볼 수 없습니다. 이미 댓글에서 언급Windows가 시작될 때 자동으로 시작하도록 C++ 프로그램을 설정하는 방법
#include "stdafx.h"
#include<Windows.h>
#include <Winbase.h>
BOOL RegisterMyProgramForStartup(PCWSTR pszAppName, PCWSTR pathToExe, PCWSTR args)
{
HKEY hKey = NULL;
LONG lResult = 0;
BOOL fSuccess = TRUE;
DWORD dwSize;
const size_t count = MAX_PATH * 2;
wchar_t szValue[count] = {};
wcscpy_s(szValue, count, L"\"");
wcscat_s(szValue, count, pathToExe);
wcscat_s(szValue, count, L"\" ");
if (args != NULL)
{
wcscat_s(szValue, count, args);
}
lResult = RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, 0, (KEY_WRITE | KEY_READ), NULL, &hKey, NULL);
fSuccess = (lResult == 0);
if (fSuccess)
{
dwSize = (wcslen(szValue) + 1) * 2;
lResult = RegSetValueExW(hKey, pszAppName, 0, REG_SZ, (BYTE*)szValue, dwSize);
fSuccess = (lResult == 0);
}
if (hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
return fSuccess;
}
void RegisterProgram()
{
wchar_t szPathToExe[MAX_PATH];
GetModuleFileNameW(NULL, szPathToExe, MAX_PATH);
RegisterMyProgramForStartup(L"ConsoleApplication7", szPathToExe, L"-foobar");
}
int _tmain(int argc, _TCHAR* argv[])
{
RegisterProgram();
return 0;
}
서비스를 시작하지 않습니다. 응용 프로그램을 실행하고 있습니다. 그들은 같은 것이 아닙니다. 응용 프로그램 또는 프로세스 탭의 작업 관리자에서 찾아보십시오. –
또한이 방법을 사용하면 컴퓨터가 시작될 때 프로그램이 실행되지 않고 사용자가 로그온 할 때 프로그램이 실행됩니다. Run 키가 많이 남용되기 때문에 일부 시스템 관리자는 그렇게하지 않는 것이 좋습니다. –