代码里有详细的注释,代码如下:
AOperationRegActor.h
UCLASS()
class NEWWINDOW_API AOperationRegActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AOperationRegActor();
//读取注册表中的键值
UFUNCTION(BlueprintPure, Category = Reg)
FString ReadRegValue(const FString& PathDir, const FString& KeyName, bool& bIsFind);
//写入注册表中的键值,如果没有就会创建
UFUNCTION(BlueprintCallable, Category = Reg)
bool WriteRegValue(const FString &PathDir, const FString &KeyName, const FString &Value);
//删除注册表中的键
UFUNCTION(BlueprintCallable, Category = Reg)
bool DeleteRegKey(const FString &PathDir, const FString &KeyName);
private:
//打开键值的最大字节数
static const int32 MaxBuffsize;
};
AOperationRegActor.cpp
//初始化
const int32 AOperationRegActor::MaxBuffsize= 64;
// Sets default values
AOperationRegActor::AOperationRegActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
FString AOperationRegActor::ReadRegValue(const FString& PathDir, const FString& KeyName, bool& bIsFind)
{
HKEY hKey;
/*
RegOpenKeyEx( //返回值为零(ERROR_SUCCESS)表示成功。其他任何值都代表一个错误代码
_In_ HKEY hKey, //要打开的键的句柄,通常是主(根)键
_In_opt_ LPCWSTR lpSubKey, //要打开的子键的地址,指向包含了要打开键的名字的以空字符结束的字符串,
这个键必须是能被hKey参数识别的子键,通常是子键的路径地址
_In_opt_ DWORD ulOptions, //参数作用不明确,系统保留,指定为0
_In_ REGSAM samDesired, //打开权限:KEY_READ(只读子键),KEY_ALL_ACCESS(允许所有权限)
_Out_ PHKEY phkResult //返回打开句柄
);
*/
if (RegOpenKeyEx(HKEY_CURRENT_USER, *PathDir, 0, KEY_WOW64_64KEY | KEY_READ, &hKey) == 0)
{
TCHAR Buffer[MaxBuffsize];
DWORD BufferSize = sizeof(Buffer);
/*
WINADVAPI
LSTATUS
APIENTRY
RegQueryValueExW( //返回值为零(ERROR_SUCCESS)表示成功。其他任何值都代表一个错误代码
_In_ HKEY hKey, //一个已打开项的句柄,或者指定一个标准键值项名
_In_opt_ LPCWSTR lpValueName, //要获取值的键值项(Key)的名称
_Reserved_ LPDWORD lpReserved, //参数作用不明确,指定为0
_Out_opt_ LPDWORD lpType, //用于装载取回数据类型的一个变量
_Out_writes_bytes_to_opt_(*lpcbData, *lpcbData) __out_data_source(REGISTRY) LPBYTE lpData, // 用于装载指定值的一个缓冲区
_When_(lpData == NULL,_Out_opt_) _When_(lpData != NULL,_Inout_opt_) LPDWORD lpcbData //用于装载lpData缓冲区长度的一个变量。一旦返回,
它会设为实际装载到缓冲区的字节数
);
*/
HRESULT hResult = RegQueryValueEx(hKey, *KeyName, 0, nullptr, reinterpret_cast<LPBYTE>(Buffer), &BufferSize);
RegCloseKey(hKey);
if (hResult != 0)
{
return FString(TEXT("Find'n Key"));
}
bIsFind = true;
return FString(Buffer);
}
return FString(TEXT("Find'n Reg"));
}
bool AOperationRegActor::WriteRegValue(const FString &PathDir, const FString &KeyName, const FString &Value)
{
bool bSuccess = false;
HKEY hRootKey;
/*
RegCreateKeyEx( //返回值为零(ERROR_SUCCESS)表示成功。其他任何值都代表一个错误代码
_In_ HKEY hKey, //要打开的键的句柄,通常是主(根)键
_In_ LPCWSTR lpSubKey, //要打开的子键的地址,指向包含了要打开键的名字的以空字符结束的字符串,
这个键必须是能被hKey参数识别的子键,通常是子键的路径地址
_Reserved_ DWORD Reserved, //参数作用不明确,指定为0
_In_opt_ LPWSTR lpClass, //定义子键类名,通常设为nullptr
_In_ DWORD dwOptions, //创建子键时的选项
_In_ REGSAM samDesired, //权限
_In_opt_ CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes, //指向SECURITY_ATTRIBUTES结构,指定键句柄的继承性,通常设为nullptr
_Out_ PHKEY phkResult, //返回创建句柄
_Out_opt_ LPDWORD lpdwDisposition //通常设为nullptr
);
如果这个键在注册表中已经存在,这个函数打开它。
*/
if (RegCreateKeyEx(HKEY_CURRENT_USER, *PathDir, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WOW64_64KEY | KEY_ALL_ACCESS, nullptr, &hRootKey, nullptr) == 0)
{
/*
WINADVAPI
LSTATUS
APIENTRY
RegSetValueEx( //返回值为零(ERROR_SUCCESS)表示成功。其他任何值都代表一个错误代码
_In_ HKEY hKey, //一个已打开项的句柄,或者指定一个标准键值项名
_In_opt_ LPCWSTR lpValueName, //要设置的键值项(Key)的名地址
_Reserved_ DWORD Reserved, //参数作用不明确,指定为0
_In_ DWORD dwType, //键值Vlaue的类型
_In_reads_bytes_opt_(cbData) CONST BYTE* lpData, //键值Vlaue
_In_ DWORD cbData //键值Vlaue长度
);
使用这个函数的时个有一点需要注意,其中参数lpDate和cbDate的值要跟据dwType的值来设定,按常用设置我们分三种情况
(1)当dwType为REG_SZ时,这时跟通常一样,lpDate为要设置的数据, cbDate为数据的长度。
(2)当dwType为REG_DWORD 时,cbDate必须设为4。
(3)当dwType为REG_BINARY 时,cbDate也必须设为4。
如果调用时,键值项名称已经存在,则会覆盖原有键值项。如果没有就新建一个。
*/
LRESULT Result = RegSetValueEx(hRootKey, *KeyName, 0, REG_SZ, (const BYTE*)*Value, (Value.Len() + 1) * sizeof(TCHAR));
RegCloseKey(hRootKey);
if (Result == ERROR_SUCCESS)
{
bSuccess = true;
}
}
return bSuccess;
}
bool AOperationRegActor::DeleteRegKey(const FString &PathDir, const FString &KeyName)
{
bool bSuccess = false;
HKEY hKey;
if (RegOpenKeyEx(HKEY_CURRENT_USER, *PathDir, 0, KEY_WOW64_64KEY | KEY_READ, &hKey) == 0)
{
/*
WINADVAPI
LSTATUS
APIENTRY
RegDeleteKey ( //返回值为零(ERROR_SUCCESS)表示成功。其他任何值都代表一个错误代码
_In_ HKEY hKey, //一个已打开项的句柄,或者指定一个标准键值项名
_In_ LPCWSTR lpSubKey //要设置的键值项(Key)的名地址
);
*/
LRESULT Result = RegDeleteKey(hKey, *KeyName);
RegCloseKey(hKey);
if (Result == 0)
{
bool bRes = true;
return bRes;
}
}
return bSuccess;
}
AOperationRegActor.cpp额外包含的头文件
#include "AllowWindowsPlatformTypes.h"
#include <winreg.h>
#include "HideWindowsPlatformTypes.h"
蓝图如下:
执行读写操作的结果:
参考:https://blog.csdn.net/u014532636/article/details/99726677