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
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 of std::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));