Hey everybody,
I currently making a program that needs to activated on startup. So after extensive googling I decided to do it the registry way. However, creating a new value or editing an existing value, is not working. Could you explain how to edit and/or create a value in a key?
My code so far:
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
int main(void) {
/* Variable declarations */
char data[256] = "D:\\someRandomProcess.exe"; // The data that needs to be stored in the key values, file location
// Path to the autostart key:
char key1[180]="Software\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY hkey; // Handle to registry key
unsigned long datalen; // data field length(in), data returned length(out)
unsigned long datatype; // #defined in winnt.h (predefined types 0-11)
/*
*************************************************
** Checking wether the register can be openend **/
if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"",
NULL,
KEY_QUERY_VALUE, // Set up field value query activity
&hkey) == ERROR_SUCCESS)
{
printf("Error opening the register.\n");
return GetLastError();
}
printf("The register can be used.\n");
// Resetting datalen (in and out field)
datalen = 255;
/* Closing the key */
RegCloseKey(hkey);
/* End of register check **
*************************************************
*/
/*
* First key that will be openend: key1
* This key is directed to the autostart key
*
*/
/*
* Opening the key
*/
if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE,
key1,
NULL,
KEY_SET_VALUE,
&hkey) == ERROR_SUCCESS)
{
printf("Error opening HKLM subkey: %s\n",key1);
return GetLastError();
} else {
printf("HKLM subkey was openend succesfully.\n");
}
/*
* Reading the key
*/
datalen = 255;
if (RegSetValueEx(hkey,
"C_test_val",
NULL,
&datatype,
data,
&datalen) == ERROR_SUCCESS)
{
printf("The value was succesfully set");
}
else
{
printf("Could not set value.");
return GetLastError();
}
/* Resetting datalen */
datalen = 255;
/* Closing key handle */
RegCloseKey(hkey);
return 0;
}
Thanks,
~G