Home > Net >  undeclared function RegGetValue MinGW C compiler
undeclared function RegGetValue MinGW C compiler

Time:11-23

I am trying to compile a C program using MinGW on Windows 7 (64-bit). The code is given below:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>

void readRegDwordValue() {

    HKEY hKey = HKEY_LOCAL_MACHINE;
    char const *subKey = "Software\\Metpl\\My Program";
    char const *pValue = "MJP_XXX";
    uint32_t flags = RRF_RT_REG_DWORD;
    int *pvData = NULL;

    int64_t result = RegGetValue(hKey, &subKey, pValue, flags, NULL, pvData, sizeof(DWORD));

    if (result != ERROR_SUCCESS) {
        printf("Error getting value. Code: ");
        printf("%" PRId64 "\n", result);
    } else {
        printf("Value data: ");
        printf("%" PRId32 "\n", *(int32_t*)pvData);
    }
}

int main() {
    readRegDwordValue();
    return 0;
}

I get the following warning:

    gcc -O3 -Wall -c -o readReg.o readReg.c

readReg.c: In function 'readRegDwordValue':
readReg.c:13:22: warning: implicit declaration of function 'RegGetValue'; did you mean 'RegSetValue'? [-Wimplicit-function-declaration]
13 |     int64_t result = RegGetValue(hKey, &subKey, pValue, flags, NULL, pvData, sizeof(DWORD));
    |                      ^~~~~~~~~~~
    |                      RegSetValue

I have included windows.h which includes winreg.h that contains the definition of the RegGetvalue function. Why is the compiler not able to find it? Also, since it is suggesting that I meant RegSetValue, does it mean it is able to find this one? !!

The linker gives the following error:

gcc readReg.o -o readReg.exe -L -liphlpapi -ladvapi32

d:/__sdk/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: readReg.o:readReg.c:(.text 0x45):
undefined reference to `RegGetValue'
collect2.exe: error: ld returned 1 exit status

What am I missing here? I have been pulling my hair over this for over 8 hours now and not able to understand where I am making the mistake. I have not been able to find much relevant discussion online on this either.

Desperately request some input on this so that I can move forward. Thanks in advance.

CodePudding user response:

Finally! All the hair-pulling bore fruit. And, all the peripheral learning along the journey now seems exhilarating. Following is what works with MinGW-w64 (32-bit):

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>

char buffer[1024]; // Based on need. This is the max that I need.
DWORD bufferSize = sizeof(buffer);

DWORD readRegDwordValue(HKEY hKey, char const *subKey, char const *pValue) {
    long unsigned int *p = NULL;
    int64_t result = RegGetValue(hKey, subKey, pValue, RRF_RT_REG_DWORD, p, &buffer, &bufferSize);

    if (result != ERROR_SUCCESS) {
        return 4294967295; // max unsigned int.
    }

    return ((DWORD *)buffer)[0]; // return the first element of the buffer.
}

char * readStringValue(HKEY hKey, char const *subKey, char const *pValue) {
    long unsigned int *p = NULL;
    int64_t result = RegGetValue(hKey, subKey, pValue, RRF_RT_REG_SZ, p, &buffer, &bufferSize);

    if (result != ERROR_SUCCESS) {
        return "Error";
    }

    return (char *)buffer;
}

int main() {
    char *string = readStringValue(HKEY_LOCAL_MACHINE, "Software\\Metpl\\My Program", "companyName");
    printf("string:%s%s%d%s \n", string, " (length = ", strlen(string), ")");

    DWORD integerValue = readRegDwordValue(HKEY_LOCAL_MACHINE, "Software\\Metpl\\My Program", "sampleInteger");
    printf("integer:(%lu%s\n", integerValue, ")");

    return 0;
}

I have tested this code and it gives correct results. Since I only need to get the correct result or an error, I have not added additional error-checking in the code.

Thanks, @CGio3, for pointing me to MinGW-w64. I have made many Portable C programs and a few Windows programs with the version I have and got too comfortable with it, I guess. Anyway, lesson learnt.

Hope this helps someone who has been struggling with the subject.

  • Related