gpt4 book ai didi

c++ - 如何通过将程序添加到注册表来使我的程序在启动时运行?

转载 作者:太空宇宙 更新时间:2023-11-04 15:01:12 25 4
gpt4 key购买 nike

我有一个 VC++ 控制台应用程序,我想让它在启动时运行。我想通过将它添加到注册表中来做到这一点 我已经尝试过我在另一篇关于此的帖子中找到的方法但是它没有用,我注销然后重新登录但程序没有启动。这是我使用的代码

string progPath = "C:/Users/user/AppData/Roaming/Microsoft/Windows/MyApp.exe";
HKEY hkey = NULL;
long createStatus = RegCreateKey(HKEY_CURRENT_USER, L"/SOFTWARE/Microsoft/Windows/CurrentVersion/Run", &hkey);//Creates a key


long status = RegSetValueEx(hkey, L"MyApp", 0, REG_SZ, (BYTE *)progPath.c_str(), sizeof(progPath.c_str()));

感谢任何帮助

最佳答案

您的代码存在三个问题。

  1. 您需要使用 \ 而不是 /

  2. 您正在将 8 位 Ansi 数据传递给需要 16 位 Unicode 数据的函数。使用 std::wstring 而不是 std::string

  3. 您为数据大小传递了错误的值。它需要一个包含空终止符的字节数

试试这个:

std::wstring progPath = L"C:\\Users\\user\\AppData\\Roaming\\Microsoft\\Windows\\MyApp.exe";
HKEY hkey = NULL;
LONG createStatus = RegCreateKey(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", &hkey); //Creates a key
LONG status = RegSetValueEx(hkey, L"MyApp", 0, REG_SZ, (BYTE *)progPath.c_str(), (progPath.size()+1) * sizeof(wchar_t));

关于c++ - 如何通过将程序添加到注册表来使我的程序在启动时运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41317224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com