首页 > 解决方案 > RegGetValue(),标头不在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%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: error: 'RRF_RT_ANY' undeclared (first use in this function) RRF_RT_ANY | RRF_SUBKEY_WOW6464KEY, ^~~~~~~~~~ RegistryParser.c:29:3: 注意:每个未声明的标识符对于它出现的每个函数只报告一次

RegistryParser.c:29:16: 错误:'RRF_SUBKEY_WOW6464KEY' 未声明(在此函数中首次使用)RRF_RT_ANY | RRF_SUBKEY_WOW6464KEY,^~~~~~~~~~~~~~~~~~~~~~~~

标签: cregistryinclude-path

解决方案


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

我弄清楚我的问题是什么。这是我的编译器。简短而甜蜜的是我更新C:\MinGW\include了文件夹

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

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

C:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\x86_64-w64-mingw32\include[3] 然后从(这是我的默认位置)复制“包含”文件夹以C:\MinGW覆盖所有标题(详细信息部分中的这种愚蠢解释)

细节

我认为我的问题中的代码会起作用,因为https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-reggetvaluew上的信息。我正在查看C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um\Windows.h具有代码块的 Windows 工具包中的标题

#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 在包含文件夹中使用他们自己修改的标头。如果您将其保存在默认路径C:\MinGW\include中。因此,它会查看其C:\MinGW\include文件夹中的标题,并且它们C:\MinGW\include\winreg.h没有该RegGetValue()功能。

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

所以我然后去我之前提到的网站上获取更新版本的 MinGW 并尝试按照步骤编辑“系统变量”来更新路径。我删除了 C:\MinGW\bin 的路径以添加新路径

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但其他一切都是一样的,它编译时没有警告或错误,并返回我所期望的。

对不起,很长的帖子。


推荐阅读