Home > Back-end >  Add an executable to registry in order to run at startup
Add an executable to registry in order to run at startup

Time:04-21

I want to add a value to the Run key of the Windows Registry in order for my program to run at startup. I have written the following code. It compiles and links successfully. And when I debug the project, it doesn't give any errors. But, my key doesn't get added to the Registry.

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;
}

CodePudding user response:

You should first create a key there and set a value for it. Use the following code snipts which add a key and a value to the run registry. It has been written with C and classes.

#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;
}

Then check the following path:

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

  • Related