gpt4 book ai didi

c++ - 将应用程序添加到启动(注册表)

转载 作者:行者123 更新时间:2023-11-30 20:41:16 26 4
gpt4 key购买 nike

我正在尝试将我的软件添加到注册表中,我找到了一些我可以使用的代码,但不是完整的工作代码 C/C++ 对我来说是新的,无法自己创建它。但基本思想是:检查 reg key 是否已设置(如果未创建)。

我能够使用以下代码获取我的程序位置:

TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL,szPath,MAX_PATH);

并且能够使用以下方式创建 key :(不确定这是否是正确的方式)

HKEY newValue;
RegOpenKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&newValue);
RegSetValueEx(newValue,"myprogram",0,REG_SZ,(LPBYTE)szPath,sizeof(szPath));
RegCloseKey(newValue);
return 0;

缺少什么,稍微检查一下 key 是否不存在......

谢谢!

最佳答案

这里有一些代码可能可以满足您的需求。为 EXE 调用 RegisterProgram 进行 self 注册,以便在用户登录时自动启动。此函数调用 GetModuleFileName,然后调用另一个名为 RegisterMyProgramForStartup< 的辅助函数 写入注册表。

调用 IsMyProgramRegisteredForStartup(L"My_Program") 来检测注册是否确实存在且有效。

简单说明一下。在实际再次写出 key 之前检查 key 是否存在对性能的影响可以忽略不计。您可以盲目地调用 RegisterProgram,如果 key 已经存在,它将覆盖该 key 。检测注册是否存在对于初始化启用或禁用自动启动的 UI 复选框很有用。 (您正在给您的用户一个选择,对吗?因为我讨厌自动安装并自动运行而不给我选择的应用程序。)

BOOL IsMyProgramRegisteredForStartup(PCWSTR pszAppName)
{
HKEY hKey = NULL;
LONG lResult = 0;
BOOL fSuccess = TRUE;
DWORD dwRegType = REG_SZ;
wchar_t szPathToExe[MAX_PATH] = {};
DWORD dwSize = sizeof(szPathToExe);

lResult = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_READ, &hKey);

fSuccess = (lResult == 0);

if (fSuccess)
{
lResult = RegGetValueW(hKey, NULL, pszAppName, RRF_RT_REG_SZ, &dwRegType, szPathToExe, &dwSize);
fSuccess = (lResult == 0);
}

if (fSuccess)
{
fSuccess = (wcslen(szPathToExe) > 0) ? TRUE : FALSE;
}

if (hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}

return fSuccess;
}

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)
{
// caller should make sure "args" is quoted if any single argument has a space
// e.g. (L"-name \"Mark Voidale\"");
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"My_Program", szPathToExe, L"-foobar");
}

int _tmain(int argc, _TCHAR* argv[])
{
RegisterProgram();
IsMyProgramRegisteredForStartup(L"My_Program");
return 0;
}

关于c++ - 将应用程序添加到启动(注册表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15935564/

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