3

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

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
sharpchain
  • 355
  • 2
  • 7
  • 15

3 Answers3

10

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));
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
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.

Raindrop7
  • 3,889
  • 3
  • 16
  • 27
  • That didnt work at all this time nothing even showed up in the registry – sharpchain Dec 24 '16 at 23:01
  • are you on windows 7 as I guess?? – Raindrop7 Dec 24 '16 at 23:16
  • 1
    @sharpchain - "still doesnt work" - need say what error code you got and on which call – RbMm Dec 24 '16 at 23:20
  • @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. – Raindrop7 Dec 24 '16 at 23:22
  • 1
    @Raindrop7 - for write under `HKEY_CURRENT_USER` not need elevate – RbMm Dec 24 '16 at 23:23
  • @RbMm: aah ok! thanx. does it matter if the type of user `user` is `limited`? – Raindrop7 Dec 24 '16 at 23:24
  • @Raindrop7 - usually any users can write under `HKEY_CURRENT_USER`, except Low Intergity apps – RbMm Dec 24 '16 at 23:26
  • 1
    for `HKEY_LOCAL_MACHINE` we need admin group enabled in token, when for `HKEY_CURRENT_USER` not need – RbMm Dec 24 '16 at 23:30
  • There 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. – IInspectable Dec 27 '16 at 15:39
0

A program that requires elevation (i.e. running as Administrator) can't be set to automatically run at startup. See: How does one run a program at startup that requires UAC elevation? Since you app is placed in "\AppData\Roaming\Microsoft\Windows\MyApp.exe", I assume this path is protected and reserved to programs that requires elevation. That might be the reason why it doesn't work for you.

Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56