使用C编辑注册表给出警告而不编辑注册表项



下面的代码应该更改这些注册表值,但它会发出警告,即使代码实际运行,它也不会设置值。它没有给出错误,只是没有执行。我还确认了函数和代码正在运行。我通过添加sizeof()参数修复了一个警告,但其余的(请参阅下面的代码(仍然存在,它仍然不会真正写入注册表。此外,当我尝试编辑字符串值时,它可以工作,但不能编辑dword值。

//dependences for anyone who wants to try to fix it
#include <winreg.h>
#include <conio.h>
#include <stdlib.h> 
#include <string.h> 
#include <stdlib.h>
#include <unistd.h>  
void startBCPE() {
//Sets registery values for BCPE
HKEY key;
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\Setup", 0, KEY_ALL_ACCESS, &key);
RegSetValueExA(key, "SystemSetupInProgress", 0, REG_DWORD, 1, sizeof(1)); //this line fails to edit the registry
RegSetValueExA(key, "SetupType", 0, REG_DWORD, 2, sizeof(2)); //this line fails to edit the registry
RegSetValueExA(key, "SetupPhase", 0, REG_DWORD, 3, sizeof(3)); //this line fails to edit the registry
RegSetValueExA(key, "CmdLine", 0, REG_SZ, "cmd.exe", sizeof("cmd.exe")); //This line works however
RegCloseKey(key);
return;
}

警告:

.Fizz.c:17:64: warning: passing argument 5 of 'RegSetValueExA' makes pointer from integer without a cast [-Wint-conversion]
RegSetValueExA(key, "SystemSetupInProgress", 0, REG_DWORD, 1, sizeof(1));
^
In file included from c:mingwincludewindows.h:52:0,
from .Fizz.c:5:
c:mingwincludewinreg.h:108:23: note: expected 'const BYTE * {aka const unsigned char *}' but argument is of type 'int'
WINADVAPI LONG WINAPI RegSetValueExA(HKEY,LPCSTR,DWORD,DWORD,const BYTE*,DWORD);
^~~~~~~~~~~~~~
.Fizz.c:18:52: warning: passing argument 5 of 'RegSetValueExA' makes pointer from integer without a cast [-Wint-conversion]
RegSetValueExA(key, "SetupType", 0, REG_DWORD, 2, sizeof(2));
^
In file included from c:mingwincludewindows.h:52:0,
from .Fizz.c:5:
c:mingwincludewinreg.h:108:23: note: expected 'const BYTE * {aka const unsigned char *}' but argument is of type 'int'
WINADVAPI LONG WINAPI RegSetValueExA(HKEY,LPCSTR,DWORD,DWORD,const BYTE*,DWORD);
^~~~~~~~~~~~~~
.Fizz.c:19:53: warning: passing argument 5 of 'RegSetValueExA' makes pointer from integer without a cast [-Wint-conversion]
RegSetValueExA(key, "SetupPhase", 0, REG_DWORD, 3, sizeof(3));
^
In file included from c:mingwincludewindows.h:52:0,
from .Fizz.c:5:
c:mingwincludewinreg.h:108:23: note: expected 'const BYTE * {aka const unsigned char *}' but argument is of type 'int'
WINADVAPI LONG WINAPI RegSetValueExA(HKEY,LPCSTR,DWORD,DWORD,const BYTE*,DWORD);
^~~~~~~~~~~~~~

使用RegSetValueExA api,您不能只放1,因为它需要BYTE类型的var指针。只需用dword类型声明您的var,然后将其转换为BYTE即可。示例:

HKEY key;
DWORD Num = 1;
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\Setup", 0, KEY_ALL_ACCESS, &key);
RegSetValueExA(key, "SystemSetupInProgress", 0, REG_DWORD, (LPBYTE)&Num, sizeof(1));
RegCloseKey(key);

最新更新