这个问题与c ++有各种各样的变化,但是我正在尝试在C语言中使用注册表函数。我知道包含include,所以为什么看不到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%s\n", 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, ^ ~~~~~~~~~~
RegistryParser.c:29:3:错误:未声明“ RRF_RT_ANY”(此函数中的首次使用) RRF_RT_ANY | RRF_SUBKEY_WOW6464KEY, ^ ~~~~~~~~~~ RegistryParser.c:29:3:注意:每个未声明的标识符对于出现在其中的每个函数仅报告一次
RegistryParser.c:29:16:错误:未声明“ RRF_SUBKEY_WOW6464KEY”(此功能中的首次使用) RRF_RT_ANY | RRF_SUBKEY_WOW6464KEY, ^ ~~~~~~~~~~~~~~~~~~~
答案 0 :(得分:0)
我弄清楚了我的问题。这是我的编译器。简短而有趣的是,我通过
更新了C:\MinGW\include
文件夹
[1]转到https://sourceforge.net/projects/mingw-w64/files/
[2]单击MinGW-W64-install.exe
。保存并运行
[3],然后将'include'文件夹从C:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\x86_64-w64-mingw32\include
(这是我的默认位置)复制到C:\MinGW
,覆盖所有标头(详细信息部分的愚蠢解释)< / p>
我认为我的问题中的代码将有效,因为https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-reggetvaluew处的信息。我在看Windows Kits C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um\Windows.h
中的标头,其中包含代码块
#if !defined(_MAC) || defined(_WIN32REG)
#include <winreg.h>
#endif
和C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um\winreg.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在其中的include文件夹中使用了自己修改的标头。如果已将其保存在默认路径C:\MinGW\include
中。因此,它将查看C:\MinGW\include
文件夹中的标题,而其中的C:\MinGW\include\winreg.h
则没有RegGetValue()
函数。
我会显示原始C:\MinGW\include\winreg.h
标题中的内容,但是我已经覆盖了它。
因此,我然后在前面提到的网站上获得了MinGW的更新版本,并尝试按照编辑“系统变量”的步骤来更新Path。我删除了C:\ MinGW \ bin的Path以添加新的
的路径C:\ Program Files \ mingw-w64 \ x86_64-8.1.0-win32-seh-rt_v6-rev0 \ mingw64 \ bin
C:\ Program Files \ mingw-w64 \ x86_64-8.1.0-win32-seh-rt_v6-rev0 \ mingw64 \ x86_64-w64-mingw32 \ bin
C:\ Program Files \ mingw-w64 \ x86_64-8.1.0-win32-seh-rt_v6-rev0 \ mingw64 \ opt \ bin
我仍然有编译警告和错误,所以那时我只是想想MinGW\include
文件夹中需要的标题。因此,我删除了新路径,并将路径放回C:\ MinGW \ bin。并做了我在帖子开头提到的内容。我必须修改代码并删除“ | RRF_SUBKEY_WOW6464KEY”,因为它在MinGW的winreg.h
中没有定义,但其他所有内容都相同,并且编译时没有警告或错误,并返回了我期望的结果。
很抱歉,很长的帖子。