Code:
#include <windows.h>
#define RUN_KEY "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
int addRunEntry(char *name, char *path);
int main()
{
addRunEntry("bob", "c:\\niceprogram\\nonmaliciousprogram.exe");
}
int addRunEntry(char *name, char *path)
{
HANDLE key;
int len = strlen(path) + 1;
LONG r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, RUN_KEY, 0, KEY_SET_VALUE, &key);
if (r != ERROR_SUCCESS) {
// unable to open key for adding values.
return 1;
}
r = RegSetValueEx(key, name, 0, REG_SZ, path, len);
if (r != ERROR_SUCCESS) {
RegCloseKey(key);
// unable to change registry value.
return 1;
}
RegCloseKey(key);
// success
return 0;
}