RegGetValue(), header <winreg.h> 在 c 中不起作用



这个问题与c ++有关有多种变体,但我正在尝试在C中使用注册表函数。我知道这包括,那么为什么它看不到RegGetValue((。它是C++独有的吗?有没有办法在 C 中使用它?

这是我发现的一些代码,我试图用它来测试将显示的内容。

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <Windows.h>
#include <errno.h>

#define BUFFER  8192
int main()
{
char value[255];
DWORD BufferSize = BUFFER;
RegGetValue(HKEY_LOCAL_MACHINE, 
"SOFTWARE\Microsoft\Windows NT\CurrentVersion", 
"RegisteredOwner", 
RRF_RT_ANY | RRF_SUBKEY_WOW6464KEY, 
NULL, 
(PVOID)&value, 
&BufferSize);
printf("n%sn", value);
system("pause");
return 0;
}

我是这样编译的

gcc -Wall RegistryParser.c -o RegistryParser.exe

我收到此警告和错误

RegistryParser.c:在函数'main'中: RegistryParser.c:26:2:警告:函数"RegGetValue"的隐式声明[-Wimplicit-function-declaration] RegGetValue(HKEY_LOCAL_MACHINE, ^~~~~~~~~~~

注册表解析器.c:29:3:错误:"RRF_RT_ANY"未声明(首次在此函数中使用( RRF_RT_ANY |RRF_SUBKEY_WOW6464KEY, ^~~~~~~~~~ RegistryParser.c:29:3:注意:每个未声明的标识符只报告一次,它出现在的每个函数中

注册表解析器.c:29:16:错误:"RRF_SUBKEY_WOW6464KEY"未声明(在此函数中首次使用( RRF_RT_ANY |RRF_SUBKEY_WOW6464KEY, ^~~~~~~~~~~~~~~~~~~~~

在我回答我自己的问题之前,这只是我为了让它工作而做的一个黑客,它可能会在以后中断

我弄清楚了我的问题是什么。它与我的编译器有关。简短而甜蜜的是我更新C:MinGWinclude文件夹

[1] 去 https://sourceforge.net/projects/mingw-w64/files/

[2] 单击MinGW-W64-install.exe。保存并运行它

[3] 然后从C:Program Filesmingw-w64x86_64-8.1.0-win32-seh-rt_v6-rev0mingw64x86_64-w64-mingw32include复制"include"文件夹(这就是默认位置对我来说的位置(以C:MinGW覆盖所有标题(在详细信息部分中解释这种愚蠢行为(

我认为我的问题中的代码会起作用,因为信息 https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-reggetvaluew.我正在查看Windows工具包中的标题C:Program Files (x86)Windows Kits10Include10.0.17763.0umWindows.h其中包含代码块

#if !defined(_MAC) || defined(_WIN32REG)
#include <winreg.h>
#endif

C:Program Files (x86)Windows Kits10Include10.0.17763.0umwinreg.h有代码

//
// RRF - Registry Routine Flags (for RegGetValue)
//
#define RRF_RT_REG_NONE        0x00000001  // restrict type to REG_NONE      (other data types will not return ERROR_SUCCESS)
#define RRF_RT_REG_SZ          0x00000002  // restrict type to REG_SZ        (other data types will not return ERROR_SUCCESS) (automatically converts REG_EXPAND_SZ to REG_SZ unless RRF_NOEXPAND is specified)
#define RRF_RT_REG_EXPAND_SZ   0x00000004  // restrict type to REG_EXPAND_SZ (other data types will not return ERROR_SUCCESS) (must specify RRF_NOEXPAND or RegGetValue will fail with ERROR_INVALID_PARAMETER)
#define RRF_RT_REG_BINARY      0x00000008  // restrict type to REG_BINARY    (other data types will not return ERROR_SUCCESS)
#define RRF_RT_REG_DWORD       0x00000010  // restrict type to REG_DWORD     (other data types will not return ERROR_SUCCESS)
#define RRF_RT_REG_MULTI_SZ    0x00000020  // restrict type to REG_MULTI_SZ  (other data types will not return ERROR_SUCCESS)
#define RRF_RT_REG_QWORD       0x00000040  // restrict type to REG_QWORD     (other data types will not return ERROR_SUCCESS)
#define RRF_RT_DWORD           (RRF_RT_REG_BINARY | RRF_RT_REG_DWORD) // restrict type to *32-bit* RRF_RT_REG_BINARY or RRF_RT_REG_DWORD (other data types will not return ERROR_SUCCESS)
#define RRF_RT_QWORD           (RRF_RT_REG_BINARY | RRF_RT_REG_QWORD) // restrict type to *64-bit* RRF_RT_REG_BINARY or RRF_RT_REG_DWORD (other data types will not return ERROR_SUCCESS)
#define RRF_RT_ANY             0x0000ffff                             // no type restriction
#if (_WIN32_WINNT >= _WIN32_WINNT_WINTHRESHOLD)
#define RRF_SUBKEY_WOW6464KEY  0x00010000  // when opening the subkey (if provided) force open from the 64bit location (only one SUBKEY_WOW64* flag can be set or RegGetValue will fail with ERROR_INVALID_PARAMETER)
#define RRF_SUBKEY_WOW6432KEY  0x00020000  // when opening the subkey (if provided) force open from the 32bit location (only one SUBKEY_WOW64* flag can be set or RegGetValue will fail with ERROR_INVALID_PARAMETER)
#define RRF_WOW64_MASK         0x00030000
#endif
#define RRF_NOEXPAND           0x10000000  // do not automatically expand environment strings if value is of type REG_EXPAND_SZ
#define RRF_ZEROONFAILURE      0x20000000  // if pvData is not NULL, set content to all zeros on failure

。再写一些代码,然后

#if (_WIN32_WINNT >= 0x0502)
WINADVAPI
LSTATUS
APIENTRY
RegGetValueA(
_In_ HKEY hkey,
_In_opt_ LPCSTR lpSubKey,
_In_opt_ LPCSTR lpValue,
_In_ DWORD dwFlags,
_Out_opt_ LPDWORD pdwType,
_When_((dwFlags & 0x7F) == RRF_RT_REG_SZ ||
(dwFlags & 0x7F) == RRF_RT_REG_EXPAND_SZ ||
(dwFlags & 0x7F) == (RRF_RT_REG_SZ | RRF_RT_REG_EXPAND_SZ) ||
*pdwType == REG_SZ ||
*pdwType == REG_EXPAND_SZ, _Post_z_)
_When_((dwFlags & 0x7F) == RRF_RT_REG_MULTI_SZ ||
*pdwType == REG_MULTI_SZ, _Post_ _NullNull_terminated_)
_Out_writes_bytes_to_opt_(*pcbData,*pcbData) PVOID pvData,
_Inout_opt_ LPDWORD pcbData
);
WINADVAPI
LSTATUS
APIENTRY
RegGetValueW(
_In_ HKEY hkey,
_In_opt_ LPCWSTR lpSubKey,
_In_opt_ LPCWSTR lpValue,
_In_ DWORD dwFlags,
_Out_opt_ LPDWORD pdwType,
_When_((dwFlags & 0x7F) == RRF_RT_REG_SZ ||
(dwFlags & 0x7F) == RRF_RT_REG_EXPAND_SZ ||
(dwFlags & 0x7F) == (RRF_RT_REG_SZ | RRF_RT_REG_EXPAND_SZ) ||
*pdwType == REG_SZ ||
*pdwType == REG_EXPAND_SZ, _Post_z_)
_When_((dwFlags & 0x7F) == RRF_RT_REG_MULTI_SZ ||
*pdwType == REG_MULTI_SZ, _Post_ _NullNull_terminated_)
_Out_writes_bytes_to_opt_(*pcbData,*pcbData) PVOID pvData,
_Inout_opt_ LPDWORD pcbData
);
#ifdef UNICODE
#define RegGetValue  RegGetValueW
#else
#define RegGetValue  RegGetValueA
#endif

但是,由于我使用的是 MinGW gcc,MinGW 在包含文件夹中使用自己修改的标头。如果将其保存在默认路径C:MinGWinclude中。因此,它会查看其C:MinGWinclude文件夹中的标头,并且它们C:MinGWincludewinreg.h没有RegGetValue()功能。

我会显示原始C:MinGWincludewinreg.h标题中的内容,但我已经覆盖了它。

因此,我随后在我之前提到的网站上获取了MinGW的更新版本,并尝试按照以下步骤编辑"系统变量"来更新路径。我删除了 C:\MinGW\bin 的路径以添加新路径

C:\Program Files\mingw-w64\x86_64-8.1.0-win32-she-rt_v6-rev0\mingw64\bin

C:\Program Files\mingw-w64\x86_64-8.1.0-win32-she-rt_v6-rev0\mingw64\x86_64-w64-mingw32\bin

C:\Program Files\mingw-w64\x86_64-8.1.0-win32-she-rt_v6-rev0\mingw64\opt\bin

我仍然有编译警告和错误,所以那时我想的只是MinGWinclude文件夹中我需要的标头。所以我删除了新路径并将路径放回 C:\MinGW\bin。并做了我在帖子开头提到的。我不得不修改我的代码并删除"|RRF_SUBKEY_WOW6464KEY",因为它没有在 MinGW 的winreg.h中定义,但其他一切都是一样的,它的编译没有警告或错误并返回我期望的。

对不起,长帖子。

最新更新