添加应用程序,以启动(注册)

问题描述 投票:4回答:3

我想我的软件添加到注册表中,我发现我可以使用代码的一些作品,但不完整的工作代码C / C ++是新的给我,我自己不能创建它。但这里的基本思想是:检查是否REG键设置,如果不创建它。

我能够使用这个代码让我的程序位置:

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

并能够创建的关键在于:(不知道这是正确的方式)

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;

缺什么,一个小检查,如果关键已不存在...

谢谢!

c++ c windows registry
3个回答
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;
}

4
投票

要检查值是否存在,请致电RegQueryValueEx

LONG retval = RegQueryValueEx(hKey, "myprogram", NULL, NULL, NULL, NULL);

需要注意的是什么,你叫newValue实际上是一个关键,而不是一个值。为了避免混乱,你应该命名为这样。我使用的名称hKey

然后检查值是否存在,如retval描述比较ERROR_SUCCESSdocumentation

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


0
投票

您忘了写有关安全访问的参数

© www.soinside.com 2019 - 2024. All rights reserved.