sha*_*ain 0 c++ registry console winapi startup
我有一个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()));
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏
您的代码有三个问题.
你需要使用\而不是/.
您正在将8位Ansi数据传递给需要16位Unicode数据的函数.用std::wstring而不是std::string.
您传递的数据大小错误.它期望包含空终止符的字节计数.
试试这个:
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));
Run Code Online (Sandbox Code Playgroud)