Add Application to Startup (Registry)

Running an application at startup: registry entry vs. startup folder

There's no fundamental difference. The folder tends to be favorited by users, easy for them to create a shortcut there. The registry key tends to be favorited by installers, lower odds that the user disables the program.

That's not exactly much of a guarantee anymore, plenty of utilities around that help editing the keys without having to use Regedit.exe. Including Windows' own msconfig.exe

how to add an item to registry to run at startup without UAC

You can always start it for a single user, this command can be run by a normal user, and will enable the application on startup for just that user.

REG ADD "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /V "My App" /t REG_SZ /F /D "C:\MyAppPath\MyApp.exe"

You cannot add something to the local machine run registry key without at some point running something as Administrator.

Even task scheduler option would require something to run as admin to add the task in.

How can I make my program run on startup by adding it to the registry?

There are three problems with your code.

  1. You need to use \ instead of /.

  2. You are passing 8bit Ansi data to a function that expects 16bit Unicode data instead. Use std::wstring instead of std::string.

  3. You are passing the wrong value for the data size. It expects a byte count that includes the null terminator.

Try this instead:

std::wstring progPath = L"C:\\Users\\user\\AppData\\Roaming\\Microsoft\\Windows\\MyApp.exe";
HKEY hkey = NULL;
LONG createStatus = RegCreateKey(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", &hkey); //Creates a key
LONG status = RegSetValueEx(hkey, L"MyApp", 0, REG_SZ, (BYTE *)progPath.c_str(), (progPath.size()+1) * sizeof(wchar_t));

Which ways are there to start a program at windows startup from regedit?

Here's some other ways to autostart programs at startup

Startup folders

You have two folders that will autorun executable in Windows. This one will start a program only for a specific user:

C:\Users\YOUR SESSION\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

And this one for all users:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

Or you can press Windows Key + R and enter

shell:startup

for the current user, or:

shell:common startup

for all users

Task Scheduler

You can plan a task to run a program at startup (or periodically!) on your computer. You have plenty tutorials on the internet if you want it's pretty easy to setup.

Windows Service

Last option is to create a Windows Service in C#. I don't really know for this option as I try it once a while back and in the end I ended doing something else.
You can look it up too on Google

Add an executable to registry in order to run at startup

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 Topics



Leave a reply



Submit