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

Bor*_*aka 4 c c++ windows registry

我正在尝试将我的软件添加到注册表中,我发现了一些我可以使用的代码,但是没有完整的代码C/C++对我来说是新的,不能自己创建.但这里有一个基本思路:检查是否设置了reg键,如果没有创建它.

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

TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL,szPath,MAX_PATH);
Run Code Online (Sandbox Code Playgroud)

并且能够创建密钥:(不确定它是否正确)

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;
Run Code Online (Sandbox Code Playgroud)

遗漏了什么,小小的检查钥匙是否已经存在......

谢谢!

sel*_*bie 12

这里有一些代码可能会做你想要的.调用RegisterProgram您的EXE自行注册,以便在用户登录时自动启动.此函数调用GetModuleFileName然后调用另一个调用RegisterMyProgramForStartup注册表的辅助函数.

调用IsMyProgramRegisteredForStartup(L"My_Program")以检测注册是否实际存在且显示有效.

一个快速说明.在实际再次写出密钥之前检查密钥是否存在对性能的影响可以忽略不计.您可以盲目地调用RegisterProgram,如果密钥已经存在,它将覆盖密钥.检测是否存在注册对于初始化启用或禁用自动启动的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;
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*nan 4

要检查该值是否存在,请调用RegQueryValueEx.

LONG retval = RegQueryValueEx(hKey, "myprogram", NULL, NULL, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)

注意,你所说的newValue实际上是一个键而不是一个值。为了避免混淆,您应该这样命名。我用过这个名字hKey

然后要检查该值是否存在,请按照文档中的描述进行retval比较。ERROR_SUCCESS

您的代码的另一个问题是绝对没有错误检查。我会把这个问题留给你来解决。