c++ - 将可执行文件添加到注册表以便在启动时运行

标签 c++ winapi

我想向 Windows 注册表的 Run 键添加一个值,以便我的程序在启动时运行。我编写了以下代码。它编译并链接成功。当我调试该项目时,它没有给出任何错误。但是,我的 key 没有添加到注册表中。

int wmain(int argc, wchar_t* argv[])
{
    HKEY key_handle;
    std::wstring start_name = L"MyApplication";
    std::wstring exe_path = L"C:\\Users\\user\\AppData\\Roaming\\Microsoft\\Windows\\MyApp.exe";

    LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &key_handle);

    if (ERROR_SUCCESS == result)
    {
        result = RegSetValueEx(key_handle, start_name.c_str(),
            0,
            REG_SZ,
            (unsigned char*)exe_path.c_str(),
            exe_path.length() * sizeof(wchar_t));
        
        if (result != ERROR_SUCCESS) 
        {
            printf("Error setting key %d\n", GetLastError());
        }
    }
    else
    {
        printf("Error opening key %d\n", GetLastError());
    }

    RegCloseKey(key_handle);

    return 0;
}

最佳答案

您应该首先在那里创建一个键并为其设置一个值。使用以下代码片段向运行注册表添加一个键和一个值。它是用 C++ 和类编写的。

#include <Windows.h>
#include <iostream>


class startup_management
{
private:
    HKEY m_handle_key = NULL;
    LONG m_result = 0;
    BOOL m_status = TRUE;
    DWORD m_registry_type = REG_SZ;
    wchar_t m_executable_path[MAX_PATH] = {};
    DWORD m_size = sizeof(m_executable_path);
    BOOL m_success = TRUE;

public:

    BOOL check(PCWSTR arg_application_name)
    {
        
        m_result = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_READ, &m_handle_key);

        m_status = (m_result == 0);

        if (m_status)
        {
            m_result = RegGetValueW(m_handle_key, NULL, arg_application_name, RRF_RT_REG_SZ, &m_registry_type, m_executable_path, &m_size);
            m_status = (m_result == 0);
        }

        if (m_status)
        {
            m_status = (wcslen(m_executable_path) > 0) ? TRUE : FALSE;
        }

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

        return m_status;
    }

    BOOL add(PCWSTR arg_application_name, PCWSTR arg_path_executable, PCWSTR arg_argument_to_exe)
    {
        const size_t count = MAX_PATH * 2;
        wchar_t registry_value[count] = {};


        wcscpy_s(registry_value, count, L"\"");
        wcscat_s(registry_value, count, arg_path_executable);
        wcscat_s(registry_value, count, L"\" ");

        if (arg_argument_to_exe != NULL)
        {
            wcscat_s(registry_value, count, arg_argument_to_exe);
        }

        m_result = RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, 0, (KEY_WRITE | KEY_READ), NULL, &m_handle_key, NULL);

        m_success = (m_result == 0);

        if (m_success)
        {
            m_size = (wcslen(registry_value) + 1) * 2;
            m_result = RegSetValueExW(m_handle_key, arg_application_name, 0, REG_SZ, (BYTE*)registry_value, m_size);
            m_success = (m_result == 0);
        }

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

        return m_success;
    }
};

int wmain(int argc, wchar_t* argv[])
{
    startup_management o_startup;
    wchar_t executable_path[MAX_PATH];

    GetModuleFileNameW(NULL, executable_path, MAX_PATH);
    o_startup.add(L"Milad", executable_path, L"-foobar");

    return 0;
}

然后检查以下路径:

Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

关于c++ - 将可执行文件添加到注册表以便在启动时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71938530/

相关文章:

windows - 如果窗口跨越多个显示器,我无法绘制它

c# - 包含 WinAPI (Win8) 组件的 WPF 应用程序

c++ - MFC 断言在全局析构函数中工作

c++ - 在哪里删除?

c++ - 如何将作品链接到您的 Visual Studio 项目?

c++ - 在 C++ 中使用私有(private)方法的属性

c++ - 跟踪打开特定文件的进程

c++ - 使用 C++ 在游戏循环中模拟时间

C++ 序列化性能

c++ - 最小化窗口时会发送什么消息?