x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Can Unreal access windows registry?

From either Blueprint or C++?

Product Version: UE 4.16
Tags:
more ▼

asked Jul 27 '17 at 02:13 PM in C++ Programming

avatar image

CrimsonHawk
531 23 24 40

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

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:

more ▼

answered Jul 28 '17 at 07:40 PM

avatar image

DerPurpelHelmut
511 8 9 25

avatar image CrimsonHawk Jul 29 '17 at 10:16 AM

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!

(comments are locked)
10|2000 characters needed characters left

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" :)


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);
 }
more ▼

answered Oct 01 '18 at 03:35 PM

avatar image

lostwoods91
36 1 3

avatar image DerPurpelHelmut Oct 01 '18 at 06:39 PM

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.

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question