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

Perhaps I was not clear enough, but the controle works, the following is shown on my screen:

The register can be used.
HKLM subkey was openend succesfully.
Could not set value.

As you can see, the setting a value at one of the values of that key does not work. The value does exist (C_test_val)

What is wrong with the function call (line 71-76)?

EDIT: my compiler gives the following warnings:


warning: passing arg 3 of `RegOpenKeyExA' makes integer from pointer without a cast
warning: passing arg 3 of `RegOpenKeyExA' makes integer from pointer without a cast
warning: passing arg 3 of `RegSetValueExA' makes integer from pointer without a cast
warning: passing arg 4 of `RegSetValueExA' makes integer from pointer without a cast
warning: passing arg 6 of `RegSetValueExA' makes integer from pointer without a cast
||=== Build finished: 0 errors, 5 warnings ===|


~G

Edited 6 Years Ago by Graphix: n/a

I finally found the solution: the datatype was not correct, datalen was not the good length.

The new (and working!) code:

#include <stdlib.h>
#include <stdio.h>
#include <windows.h>

int main(void) {

    /* Variable declarations */
    char data[] = "D:\\some\\path\\to\\a\\file"; // 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


    /*
    *************************************************
    ** Checking wether the register can be openend **/

    if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE,
    key1,
    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");

    /* 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 (!RegOpenKeyEx(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");
    }

    /*
     * Creating a value in the key
    */

    if (RegSetValueEx(hkey,"C_test_val",NULL,REG_SZ,data,strlen(data) + 1) == ERROR_SUCCESS)
    {
        printf("The value was succesfully set. Content: '%s'", data);
    }
    else
    {
        printf("Could not set value.");
        return GetLastError();
    }


    /* Closing key handle */
    RegCloseKey(hkey);

    return 0;

}

~G

This article has been dead for over six months. Start a new discussion instead.