首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将可执行文件添加到注册表,以便在启动时运行。

将可执行文件添加到注册表,以便在启动时运行。
EN

Stack Overflow用户
提问于 2022-04-20 11:03:42
回答 1查看 253关注 0票数 2

我想为Windows的Run键添加一个值,以便我的程序在启动时运行。我写了以下代码。它成功地编译和链接。当我调试这个项目时,它不会出现任何错误。但是,我的钥匙没有被添加到注册表中。

代码语言:javascript
运行
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-20 11:22:21

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

代码语言:javascript
运行
复制
#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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71938530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档