简体   繁体   English

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

[英]Invalid DWORD value in Registry after writing a DWORD as REG_DWORD?

I'm pretty sure my RegSetSetValueExA works fine, My data I'm writing is (CONST BYTE*)&setValue . 我很确定我的RegSetSetValueExA可以正常工作,我正在写的数据是(CONST BYTE*)&setValue My setvalue is a DWORD and I've already wrote to the registry with this with RegOpenKeyExA and it works fine. 我的setvalue是一个DWORD ,我已经用RegOpenKeyExA将此写入了注册表,并且工作正常。 I think the problem is coming from RegCreateKeyExA because I'm creating my new key from that. 我认为问题出在RegCreateKeyExA因为我是RegCreateKeyExA创建新密钥的。

Also, my REG_DWORD requires me to write in Binary for some reason 另外,由于某些原因,我的REG_DWORD要求我用Binary编写

https://gyazo.com/e418587d579a3e540656f06a2524901f https://gyazo.com/e418587d579a3e540656f06a2524901f

I've tried looking at other threads but everyone's problem seems different to mine because they're using RegOpenKeyExA . 我尝试查看其他线程,但是每个人的问题似乎都不尽相同,因为他们正在使用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;
}

I refactored your functions as WriteDWORD and ReadDWORD . 我将您的函数重构为WriteDWORDReadDWORD Note that the code is actually VERY SIMILAR to your code. 请注意,该代码实际上与您的代码非常相似 So why did I bother? 那我为什么要打扰? Well, there is one subtle difference in that I made DWORD the input/output type instead of the BYTE array you had. 好吧,有一个微妙的区别,就是我将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;
}

Here is a sample usage: 这是一个示例用法:

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;
}

Note that I did a few things: 请注意,我做了几件事:

  • I used DWORD dwData for the writer but DWORD* pdwData for the reader. 我将DWORD dwData用于DWORD dwData程序,但将DWORD* pdwData用于读取程序。
  • I preinitialized DWORD cbData = sizeof(DWORD); DWORD cbData = sizeof(DWORD);初始化了DWORD cbData = sizeof(DWORD); (ie 4) (即4)

I hope this gives you insight to the "Binary" part of your question. 我希望这能使您洞悉问题的“二进制”部分。 A DWORD is 4 bytes. DWORD为4个字节。 When you wrote it to the registry, you are telling it to store a DWORD which is a 32 bit number or 4 bytes. 当您将其写入注册表时,您要告诉它存储一个32位数字或4个字节的DWORD When you read it back from registry, to reconstitute in your app you should supply a pointer to a DWORD. 当您从注册表中读取它时,要在您的应用中重新构建,您应该提供一个指向DWORD的指针。 Since you gave it a byte array, the 32 bit number populated the first 4 bytes of the array you supplied. 由于您给它提供了一个字节数组,因此32位数字填充了您提供的数组的前4个字节。 You may not have understood it but it is the correct behavior. 您可能不了解它,但这是正确的行为。

If you used std::cout you will find that it reacts differently to the same 4 bytes because of the overloaded C++ type. 如果您使用std::cout您会发现由于重载的C ++类型,它对相同的4个字节的反应不同。 If you had used a DWORD you would have seen your number. 如果您使用了DWORD,您将看到您的号码。 However, since you have it in your byte array, it will be binary gibberish. 但是,由于字节数组中有它,因此它将是二进制乱码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM