Get ERROR_NOACCESS error on write DWORD value to registry by using C code

2

I'm not strong in C code and this is my problem:

I try to write REG_DWORD to registry (I use minGW).

However I get an error 998 ERROR_NOACCESS and don't know why.

All stuff seems valid.

In my case I try to write 777

It's really strange why so basic task, like to add value to registry seems so complicated and Google doesn't contains any information

Please, help

this is my code:

#define _WIN32_WINNT 0x0501
//#define _WIN32_WINNT 0x0500   // Windows 2000
//#define _WIN32_WINNT 0x0400   // Windows NT 4.0
//#define _WIN32_WINDOWS 0x0500 // Windows ME
//#define _WIN32_WINDOWS 0x0410 // Windows 98
//#define _WIN32_WINDOWS 0x0400 // Windows 95
#include <windows.h>
#include <stdio.h>

....

const char *WriteValue()
{
HKEY hkey;
LONG result_open, result_close;
const char *defaultVal = "0";
DWORD data = 777;
DWORD dwValue, dwType, dwSize = sizeof(dwValue);
DWORD szType = REG_DWORD;

printf("Opening Key...\n");
result_open = RegOpenKeyEx(HKEY_CURRENT_USER, 
              "Software\\screen-capture-recorder",
              0, KEY_WRITE, &hkey);


if(result_open != ERROR_SUCCESS) {
    if(result_open  == ERROR_FILE_NOT_FOUND) {
        printf("Not found\n");
    } else {
        printf("Error Opening Key\n");
    }
} else {
    printf("SUCCESS!!!\n");
}



 printf("Writing Value named capture_height\n");
 LONG result_write = RegSetValueEx(hkey, "capture_height", 0, szType, (LPBYTE)data, dwSize+1);
if(result_write != ERROR_SUCCESS) {
    printf("Error Writing Value\n");
} else {
    printf("SUCCESS!!!\n");
}

printf("Closing Key...\n");
result_close = RegCloseKey(hkey);
if(result_close != ERROR_SUCCESS) {
    printf("Error Closing Key\n");
} else {
    printf("SUCCESS!!!!\n");
}


return defaultVal;
}

[Compilation]

$ gcc -L/local/lib -I/local/include -o readRegistry readRegistry.c

[Run]

$ ./readRegistry.exe
Opening Key...
SUCCESS!!!
Writing Value named capture_height
Error Writing Value
Closing Key...
SUCCESS!!!!

[Registry before]

enter image description here

[EDIT 1]

Regards to @Mat comment:

I get error 998, from Winerror.h

ERROR_NOACCESS 998L

[EDIT 2]

When I run:

LONG result_write = RegSetValueEx(hkey, "capture_height", 0, szType, (LPBYTE)"0x00000006", dwSize+1);

I get no error but in registry I see Invalid DWORD (32-bit) value.

I tried to use:

int32_t i;
i = 0x00000777; 
LONG result_write = RegSetValueEx(hkey, "capture_height", 0, szType, (LPBYTE)i, dwSize+1);

get the same error

[EDIT 3]

To be sure that i have right infra i run some test:

LONG result_write = RegSetValueEx(hkey, "capture_height1", 0, REG_SZ, (LPBYTE)"bobo", dwSizeI*2+1);

And I get in registry right value "bobo" defined as REG_SZ

c
registry
asked on Stack Overflow Aug 9, 2013 by Maxim Shoustin • edited Aug 9, 2013 by Maxim Shoustin

2 Answers

2

Thanks to @Edward Clements and @Mat for help,

After hours of "monkey-work" at last I did it:

I need add mode: KEY_ALL_ACCESS

 result_open = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\screen-capture-recorder", 0, KEY_ALL_ACCESS, &hkey);

and change LPBYTE to BYTE

LONG result_write = RegSetValueEx(hkey, "capture_height1", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));

so now this is working code:

const char *WriteValue()
{
HKEY hkey;
LONG result_open, result_close;
const char *defaultVal = "0";


result_open = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\screen-capture-recorder", 0, KEY_ALL_ACCESS, &hkey);

if(result_open != ERROR_SUCCESS) {
    if(result_open  == ERROR_FILE_NOT_FOUND) {
        printf("Not found\n");
    } else {
        printf("Error Opening Key\n");
    }
} else {
    printf("SUCCESS!!!\n");
}



DWORD value=777;
printf("Writing Value named capture_height\n");
LONG result_write = RegSetValueEx(hkey, "capture_height1", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));

printf("response %ld\n", result_write);

if(result_write != ERROR_SUCCESS) {
    printf("Error Writing Value\n");
} else {
    printf("SUCCESS!!!\n");
}

printf("Closing Key...\n");
result_close = RegCloseKey(hkey);
if(result_close != ERROR_SUCCESS) {
    printf("Error Closing Key\n");
} else {
    printf("SUCCESS!!!!\n");
}


return defaultVal;
}

I used for answer this example

answered on Stack Overflow Aug 9, 2013 by Maxim Shoustin • edited Feb 15, 2018 by Maxim Shoustin
1

Could you try adding KEY_READ in your call to RegOpenKeyEx() so that the 4th parameter reads KEY_READ | KEY_WRITE?

[It could be that you also need to ask for the read permission when you are updating existing values]

answered on Stack Overflow Aug 9, 2013 by Edward Clements

User contributions licensed under CC BY-SA 3.0