将DWORD写为REG_DWORD后,注册表中的DWORD值无效?

问题描述 投票:0回答:1

我很确定我的RegSetSetValueExA工作正常,我写的数据是(CONST BYTE*)&setValue。我的setvalueDWORD,我已经用RegOpenKeyExA写入了注册表,它工作正常。我认为问题来自RegCreateKeyExA,因为我正在创建我的新密钥。

另外,我的REG_DWORD要求我出于某种原因用二进制写

https://gyazo.com/e418587d579a3e540656f06a2524901f

我试过看其他线程,但每个人的问题似乎与我的不同,因为他们正在使用RegOpenKeyExA

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <cstdio>
#include "Strsafe.h"

// Stolen microsoft error code credits:msdn

void ErrorExit(LPTSTR lpszFunction)
{
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError();

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR)&lpMsgBuf,
        0, NULL);

    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
    StringCchPrintf((LPTSTR)lpDisplayBuf,
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"),
        lpszFunction, dw, lpMsgBuf);
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw);
}

// end of stolen code
int main()
{
    DWORD Disposition = REG_CREATED_NEW_KEY;
    BYTE lpData[32];
    DWORD setValue = 2;
    PHKEY throwAwayKey = 0;
    DWORD lpType = { REG_DWORD };
    DWORD lpcbData = { sizeof(lpData) };
    HKEY hKey = 0;
    char regPath[64] = "Software\\Policies\\Microsoft\\Windows\\System";
    char lpValueName[32] = "DisableCMD";

    long RegCKExA = RegCreateKeyExA(HKEY_CURRENT_USER, regPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &Disposition);
    if (RegCKExA == ERROR_SUCCESS)
    {
        std::cout << "Successfully executed RegCreatKeyExA\n";
    }
    else
    {
        std::cout << "An error has occurred while executing RRegCreateKeyExA. Error code: ";
        ErrorExit((LPTSTR)TEXT("RegCreateKeyExA"));
        getchar();
        return EXIT_FAILURE;
    }

    long regQVExA = RegQueryValueExA(hKey, lpValueName, NULL, &lpType, (LPBYTE)lpData, &lpcbData);

    if (regQVExA == ERROR_SUCCESS)
    {
        std::cout << "Successfully executed RegQueryValueExA and DisableCMD is already on this computer. Press ENTER to continute\n";
        getchar();
        return ERROR_SUCCESS; // Difference is it returns here if DisableCMD exists
    }
    else
        std::cout << "DisableCMD not found. Starting creation of DisableCMD registry value. Press ENTER to continue";
    getchar();

    auto regSVExA = RegSetValueExA(hKey, lpValueName, 0, REG_DWORD, (CONST BYTE*)&setValue, lpcbData);

    if (regSVExA == ERROR_SUCCESS)
    {
        std::cout << "Successfully executed RegSetValueExA\n";
        getchar();
    }
    else
    {
        std::cout << "An error has occurred while executing RegSetValueExA. Error code: ";
        getchar();
        return EXIT_FAILURE;
    }

    RegCloseKey(hKey);

    return  0;
}
c++
1个回答
-2
投票

我重构了你的功能WriteDWORDReadDWORD。请注意,代码实际上与您的代码非常相似。那我为什么要打扰呢?好吧,有一个细微的区别在于我将DWORD作为输入/输出类型而不是你拥有的BYTE数组。

LSTATUS WriteDWORD(LPCSTR lpPath, LPCSTR lpValueName, DWORD dwData)
{
    LSTATUS status = ERROR_SUCCESS;
    HKEY hKey = NULL;
    DWORD dwDisposition = 0;
    status = RegCreateKeyExA(HKEY_CURRENT_USER, lpPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
    if (status != ERROR_SUCCESS)
    {
        return status;
    }

    status = RegSetValueExA(hKey, lpValueName, 0, REG_DWORD, (CONST BYTE*) &dwData, sizeof(DWORD));
    RegCloseKey(hKey);
    return status;
}

LSTATUS ReadDWORD(LPCSTR lpPath, LPCSTR lpValueName, DWORD* pdwData)
{
    LSTATUS status = ERROR_SUCCESS;
    HKEY hKey = NULL;
    DWORD dwDisposition = 0;
    status = RegCreateKeyExA(HKEY_CURRENT_USER, lpPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
    if (status != ERROR_SUCCESS)
    {
        return status;
    }

    DWORD dwType = 0;
    DWORD cbData = sizeof(DWORD);
    status = RegQueryValueExA(hKey, lpValueName, NULL, &dwType, (LPBYTE)pdwData, &cbData);
    RegCloseKey(hKey);
    return status;
}

以下是一个示例用法:

int main()
{
    char szPath[64] = "Software\\Policies\\Microsoft\\Windows\\System";
    char szValueName[32] = "DisableCMD";
    WriteDWORD(szPath, szValueName, 1234);
    DWORD dwValue = 0;
    ReadDWORD(szPath, szValueName, &dwValue); // dwValue now contains 1234
    return 0;
}

请注意,我做了一些事情:

  • 我使用DWORD dwData作为作者,但DWORD* pdwData为读者。
  • 两种先发制剂的草药(即H)

我希望这能让您深入了解问题的“二元”部分。 DWORD cbData = sizeof(DWORD);是4个字节。当你把它写入注册表时,你告诉它存储一个32位数或4字节的DWORD。当您从注册表中读取它时,要在您的应用程序中重新构建,您应该提供指向DWORD的指针。由于您给它一个字节数组,因此32位数字填充了您提供的数组的前4个字节。您可能没有理解它,但这是正确的行为。

如果您使用DWORD,您会发现由于C ++类型的重载,它对相同的4个字节的反应不同。如果你使用过DWORD,你会看到你的号码。但是,由于你在字节数组中有它,它将是二进制乱码。

© www.soinside.com 2019 - 2024. All rights reserved.