Can Unreal access windows registry?

From either Blueprint or C++?

1 Like

Sure why not? Include Winreg.h and call RegQueryValueEx. There isn’t anything in the engine to allow a platform-generic way of doing this since the Windows registry is Windows platform-specific, so you won’t find it in Blueprints. If you however wanted to expose a function to your blueprints that did this you could do so; the following code would give you the ability to call “ReadRegistryValue” anywhere from a blueprint.

#include "AllowWindowsPlatformTypes.h"
#include <Winreg.h>
#include "Kismet/BlueprintFunctionLibrary.h"
#include "BlueprintRegistryAccess.generated.h"

UCLASS()
class UBlueprintRegistryAccess : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:
	// Reads a registry key value
	UFUNCTION(BlueprintPure)
	static FString ReadRegistryValue(const FString& KeyName, const FString& ValueName)
    {
         HANDLE hKey;
         LONG Result = RegOpenKeyExW(HKEY_CURRENT_USER, *KeyName, 0, KEY_READ, &hKey);
         if (Result != ERROR_SUCCESS)
         { 
             // Handle error 
         }

         TCHAR Buffer[MaxBufferSize];
         DWORD BufferSize = sizeof(Buffer);
         HRESULT hResult = RegQueryValueEx(hKey, *ValueName, 0, nullptr, reinterpret_cast<LPBYTE>(Buffer), &BufferSize);
         if (hResult != SUCCESS)
         {
             // Handle error 
         }
         return FString(Buffer);
    }

private:
    static const int MaxBufferSize = 256;
};

Note:

  • It may be advisable to implement a function that is platform aware and only calls the registry on Windows; does something else on other platforms or simply #errors to tell the developer it’s not implemented on other platforms.
  • The function calls will introduce a dependency on Advapi32.dll - whilst this is available on all Windows PCs since Windows 2000, you may need to modify your UE4 build to link against the library. I’m sure this is documented or there will be forum posts that deal directly with library linkage.
  • Obviously don’t inline the function in the header; implement the function itself in the corresponding .cpp and include winreg.h from there. The code above is inlined for simplicity.

References:

1 Like

Thank you for the detailed answer! I just thought that UEC++ has somehow limited functionality, but if that’s not the case then it’s great!

if you’re stuck on

C:\Program Files (x86)\Windows Kits\8.1\include\um\winnt.h(147): fatal error C1189: #error:  "No Target Architecture"

try adding this include:
#include “WindowsHWrapper.h”
:slight_smile:

edit: i found some typos, below a snippet that will compile:

#include "AllowWindowsPlatformTypes.h"
#include <winreg.h>

FString ReadRegistryValue(const FString& KeyName, const FString& ValueName)
{
	HKEY hKey;
	LONG Result = RegOpenKeyExW(HKEY_CURRENT_USER, *KeyName, 0, KEY_READ, &hKey);
	if (Result != ERROR_SUCCESS)
	{
		// Handle error 
	}
	TCHAR Buffer[MAX_PATH];
	DWORD BufferSize = sizeof(Buffer);
	HRESULT hResult = RegQueryValueEx(hKey, *ValueName, 0, nullptr, reinterpret_cast<LPBYTE>(Buffer), &BufferSize);
	if (hResult != ERROR_SUCCESS)
	{
		// Handle error 
	}
	return FString(Buffer);
}
1 Like

Incorporated your changes, thankyou lostwoods91.

NB. the use of MAX_PATH may be considered ill-advised. This is just preprocessor-defined in Windows to 260; the arbitrary limit for file paths in non-extended path API calls under Windows which is no longer enforced. It shouldn’t be used in any modern program (it could introduce file system issues); plus it has no meaning in this context anyway - we’re not storing a path.

1 Like