How can I make my program run on startup by adding it to the registry?
Solution 1
There are three problems with your code.
You need to use
\
instead of/
.You are passing 8bit Ansi data to a function that expects 16bit Unicode data instead. Use
std::wstring
instead ofstd::string
.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));
Solution 2
you can do this:
HKEY hKey;
const char* czStartName = "MyApplication";
const char* czExePath = "C:\\Users\\user\\AppData\\Roaming\\Microsoft\\Windows\\MyApp.exe";
LONG lnRes = RegOpenKeyEx( HKEY_CURRENT_USER,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
0 , KEY_WRITE,
&hKey);
if( ERROR_SUCCESS == lnRes )
{
lnRes = RegSetValueEx( hKey,
czStartName,
0,
REG_SZ,
(unsigned char*)czExePath,
strlen(czExePath) );
}
RegCloseKey(hKey);
the czStartName is the name in registry of your application. czExePath is the full path of the executable application to run at startup. and the last is the length of the full path of your executable program.
- if you are on windows 7 then you have to run the application as administrator to be able edit registry. remember windows 7 uses UAC.
or open MSVC as administrator then it will have the privilege to edit registry.
Related videos on Youtube
sharpchain
Updated on May 25, 2022Comments
-
sharpchain almost 2 years
I have a VC++ console application that I want to make run on startup. I want to do this by adding it to the registry I have already tried what I found on another post about this but it didnt work, I logged out and then signed back in but the program didnt start. Here is the code I used
string progPath = "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(), sizeof(progPath.c_str()));
Any help is appreciated
-
sharpchain over 7 yearsThat didnt work at all this time nothing even showed up in the registry
-
Raindrop7 over 7 yearsare you on windows 7 as I guess??
-
RbMm over 7 years@sharpchain - "still doesnt work" - need say what error code you got and on which call
-
Raindrop7 over 7 years@RbMm: the OP should run the application as administrator or run the MSVC as administrator. for old OSs like winxp he needn't to do so.
-
RbMm over 7 years@Raindrop7 - for write under
HKEY_CURRENT_USER
not need elevate -
Raindrop7 over 7 years@RbMm: aah ok! thanx. does it matter if the type of user
user
islimited
? -
RbMm over 7 years@Raindrop7 - usually any users can write under
HKEY_CURRENT_USER
, except Low Intergity apps -
RbMm over 7 yearsfor
HKEY_LOCAL_MACHINE
we need admin group enabled in token, when forHKEY_CURRENT_USER
not need -
IInspectable over 7 yearsThere is zero reason to use MBCS character encoding in 2016. With all supported versions of Windows using UTF-16 encoded characters, using MBCS is both wasteful on resources, as well as opens an avenue to (more or less) subtle bugs. Besides, the call to
RegSetValueEx
passes the wrong cbData argument. Sorry, -1.