Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设置C++;windows启动时自动启动的程序?(由windows服务解决方案提供) 我想让我的C++程序在Windows启动和后台运行时自动启动。我搜索了一些关于它的内容,发现我们可以登记C++程序作为Windows服务,这样当Windows启动时,程序可以自动运行。 所以我复制了这段代码,并运行了代码,但在我的计算机管理->服务中看不到任何记录。 代码如下: #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; } #包括“stdafx.h” #包括 #包括 BOOL RegisterMyProgramForStartup(PCWSTR pszAppName、PCWSTR pathToExe、PCWSTR args) { HKEY HKEY=NULL; 长lResult=0; BOOL fSuccess=TRUE; DWORD dwSize; 常量大小\u t计数=最大路径*2; wchar_t szValue[count]={}; wcscpy\u s(szValue,count,L“\”); wcscat_s(szValue、count、pathToExe); wcscat\u s(szValue,count,L“\”); 如果(args!=NULL) { wcscat_s(szValue、count、args); } lResult=RegCreateKeyExW(HKEY_当前用户,L“软件\\Microsoft\\Windows\\CurrentVersion\\Run”,0,NULL,0,(键写|键读),NULL,&HKEY,NULL); fSuccess=(lResult==0); 如果(成功) { dwSize=(wcslen(szValue)+1)*2; lResult=RegSetValueExW(hKey,pszAppName,0,REG_SZ,(字节*)szValue,dwSize); fSuccess=(lResult==0); } 如果(hKey!=NULL) { 雷克洛斯基(香港中学),; hKey=NULL; } 返回成功; } 无效注册表程序() { wchar_t szPathToExe[MAX_PATH]; GetModuleFileNameW(NULL,szPathToExe,MAX_PATH); 启动注册程序(L“控制台应用程序7”,szPathToExe,L“-foobar”); } int _tmain(int argc,_TCHAR*argv[] { 寄存器程序(); 返回0; }_C++_Windows Services - Fatal编程技术网

如何设置C++;windows启动时自动启动的程序?(由windows服务解决方案提供) 我想让我的C++程序在Windows启动和后台运行时自动启动。我搜索了一些关于它的内容,发现我们可以登记C++程序作为Windows服务,这样当Windows启动时,程序可以自动运行。 所以我复制了这段代码,并运行了代码,但在我的计算机管理->服务中看不到任何记录。 代码如下: #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; } #包括“stdafx.h” #包括 #包括 BOOL RegisterMyProgramForStartup(PCWSTR pszAppName、PCWSTR pathToExe、PCWSTR args) { HKEY HKEY=NULL; 长lResult=0; BOOL fSuccess=TRUE; DWORD dwSize; 常量大小\u t计数=最大路径*2; wchar_t szValue[count]={}; wcscpy\u s(szValue,count,L“\”); wcscat_s(szValue、count、pathToExe); wcscat\u s(szValue,count,L“\”); 如果(args!=NULL) { wcscat_s(szValue、count、args); } lResult=RegCreateKeyExW(HKEY_当前用户,L“软件\\Microsoft\\Windows\\CurrentVersion\\Run”,0,NULL,0,(键写|键读),NULL,&HKEY,NULL); fSuccess=(lResult==0); 如果(成功) { dwSize=(wcslen(szValue)+1)*2; lResult=RegSetValueExW(hKey,pszAppName,0,REG_SZ,(字节*)szValue,dwSize); fSuccess=(lResult==0); } 如果(hKey!=NULL) { 雷克洛斯基(香港中学),; hKey=NULL; } 返回成功; } 无效注册表程序() { wchar_t szPathToExe[MAX_PATH]; GetModuleFileNameW(NULL,szPathToExe,MAX_PATH); 启动注册程序(L“控制台应用程序7”,szPathToExe,L“-foobar”); } int _tmain(int argc,_TCHAR*argv[] { 寄存器程序(); 返回0; }

如何设置C++;windows启动时自动启动的程序?(由windows服务解决方案提供) 我想让我的C++程序在Windows启动和后台运行时自动启动。我搜索了一些关于它的内容,发现我们可以登记C++程序作为Windows服务,这样当Windows启动时,程序可以自动运行。 所以我复制了这段代码,并运行了代码,但在我的计算机管理->服务中看不到任何记录。 代码如下: #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; } #包括“stdafx.h” #包括 #包括 BOOL RegisterMyProgramForStartup(PCWSTR pszAppName、PCWSTR pathToExe、PCWSTR args) { HKEY HKEY=NULL; 长lResult=0; BOOL fSuccess=TRUE; DWORD dwSize; 常量大小\u t计数=最大路径*2; wchar_t szValue[count]={}; wcscpy\u s(szValue,count,L“\”); wcscat_s(szValue、count、pathToExe); wcscat\u s(szValue,count,L“\”); 如果(args!=NULL) { wcscat_s(szValue、count、args); } lResult=RegCreateKeyExW(HKEY_当前用户,L“软件\\Microsoft\\Windows\\CurrentVersion\\Run”,0,NULL,0,(键写|键读),NULL,&HKEY,NULL); fSuccess=(lResult==0); 如果(成功) { dwSize=(wcslen(szValue)+1)*2; lResult=RegSetValueExW(hKey,pszAppName,0,REG_SZ,(字节*)szValue,dwSize); fSuccess=(lResult==0); } 如果(hKey!=NULL) { 雷克洛斯基(香港中学),; hKey=NULL; } 返回成功; } 无效注册表程序() { wchar_t szPathToExe[MAX_PATH]; GetModuleFileNameW(NULL,szPathToExe,MAX_PATH); 启动注册程序(L“控制台应用程序7”,szPathToExe,L“-foobar”); } int _tmain(int argc,_TCHAR*argv[] { 寄存器程序(); 返回0; },c++,windows-services,C++,Windows Services,如评论中所述,您不是在注册服务,而是在创建一个自动运行条目。您的应用程序必须实现各种各样的功能,才能符合服务的要求 code.msdn.microsoft.com上有一个示例项目,可以帮助您开始编写自己的服务。您不是在启动服务。您正在运行一个应用程序。他们不是一回事。在任务管理器的“应用程序”或“进程”选项卡中查找。还要注意,使用这种方法,程序不会在计算机启动时运行,而是在用户登录时运行。(并且可能会被完全禁用,一些系统管理员更喜欢这样做,因为Run键经常被滥用。)

如评论中所述,您不是在注册服务,而是在创建一个自动运行条目。您的应用程序必须实现各种各样的功能,才能符合服务的要求


code.msdn.microsoft.com上有一个示例项目,可以帮助您开始编写自己的服务。

您不是在启动服务。您正在运行一个应用程序。他们不是一回事。在任务管理器的“应用程序”或“进程”选项卡中查找。还要注意,使用这种方法,程序不会在计算机启动时运行,而是在用户登录时运行。(并且可能会被完全禁用,一些系统管理员更喜欢这样做,因为Run键经常被滥用。)