2

我不擅长 C 代码,这是我的问题:

我尝试写入REG_DWORD注册表(我使用 minGW)。

但是我收到错误 998ERROR_NOACCESS并且不知道为什么。

所有的东西似乎都是有效的。

就我而言,我尝试写777

真的很奇怪为什么如此基本的任务,比如为注册表增加价值似乎如此复杂,而谷歌不包含任何信息

请帮忙

这是我的代码:

#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;
}

[汇编]

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

[跑]

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

[之前的注册]

在此处输入图像描述

[编辑 1]

关于@Mat 评论:

我收到错误 998,来自Winerror.h

ERROR_NOACCESS 998L

[编辑 2]

当我运行时:

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

我没有收到错误,但在注册表中我看到了Invalid DWORD (32-bit) value

我尝试使用:

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

得到同样的错误

[编辑 3]

为了确保我有正确的基础设施,我运行了一些测试:

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

我得到了定义为 REG_SZ 的注册表正确值“bobo”

4

2 回答 2

2

感谢@Edward Clements 和@Mat 的帮助,

经过数小时的“猴子工作”后,我终于做到了:

我需要添加模式:KEY_ALL_ACCESS

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

并更改LPBYTEBYTE

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

所以现在这是工作代码:

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;
}

我用来回答这个例子

于 2013-08-09T14:31:07.110 回答
1

您可以尝试添加KEY_READ您的调用以RegOpenKeyEx()使第 4 个参数读取KEY_READ | KEY_WRITE吗?

[可能是您在更新现有值时还需要请求读取权限]

于 2013-08-09T15:30:46.890 回答