1. Frank Zettanucci
  2. PowerBuilder
  3. Tuesday, 14 March 2023 20:52 PM UTC

Does PowerBuilder have a baked in function to rename a registry key?

MS API has a RegRenameKey 

https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regrenamekey

I am trying to create the external declaration for Powerbuilder to use but says to provide HKEY and string for the source target

LSTATUS RegRenameKey(

  HKEY    hKey,
  LPCWSTR lpSubKeyName,
  LPCWSTR lpNewKeyName
);

The PowerBuilder Declaration I came up with so far is:


FUNCTION ulong RegRenameKey(ulong hKey,ref string lpSubKeyName, ref string lpNewKeyName) LIBRARY "advapi32.dll"

My question is how exactly can I get the HKEY for HKEY_CURRENT_USER so I can do the following:

Rename:
HKEY_CURRENT_USER\SOFTWARE\PackageName\1.0\KeyOne
to
HKEY_CURRENT_USER\SOFTWARE\PackageName\1.0\KeyTwo

without loosing any of the sub-keys and values in KeyOne

Frank

Accepted Answer
Roland Smith Accepted Answer Pending Moderation
  1. Thursday, 16 March 2023 01:05 AM UTC
  2. PowerBuilder
  3. # Permalink

So I found that I have code that does this.

Function ulong RegRenameKey ( ulong hKey, string lpSubKeyName, string lpNewKeyName ) Library "advapi32.dll"

CONSTANT ulong HKEY_CURRENT_USER = 2147483649
CONSTANT ulong HKEY_LOCAL_MACHINE = 2147483650

lul_result = RegRenameKey(HKEY_CURRENT_USER, ls_oldreg, ls_newname)

You don't need to open the reg key if you use one of the predefined nodes.

Comment
  1. Roland Smith
  2. Thursday, 16 March 2023 01:06 AM UTC
For 64bit, hKey probably should be longptr.
  1. Helpful 3
There are no comments made yet.
Olan Knight Accepted Answer Pending Moderation
  1. Tuesday, 14 March 2023 20:58 PM UTC
  2. PowerBuilder
  3. # 1

Frank -

   PowerBuilder has the following built-in Registry functions: 

RegistryDelete

RegistryGet

RegistryKeys

RegistryValues

   Here's the link to the documentation:  https://docs.appeon.com/pb2022/powerscript_reference/registrySet_func.html

 
  What I would do is to use these built in functions to create a COPY of the original Registry Key, including all of the sub-keys, and save it with the desired new name.

   Then you just delete the old key(s).

Comment
  1. Armeen Mazda @Appeon
  2. Wednesday, 15 March 2023 02:09 AM UTC
Maybe admin rights is wrong terminology, but depending on how the corporate group policies are set it might restrict modifying the registry at all regardless it is current user.
  1. Helpful
  1. Olan Knight
  2. Thursday, 16 March 2023 00:10 AM UTC
One final note: In general, the Windows Registry should NOT be updated unless you KNOW for a fact that the user(s) will have ADMIN rights, AND there is not a reasonable alternative. Remember that you can READ and WRITE to the HKCU, and you can READ from HKLM.



https://stackoverflow.com/questions/53135/what-registry-access-can-you-get-without-administrator-privileges
  1. Helpful 1
  1. Frank Zettanucci
  2. Tuesday, 21 March 2023 23:20 PM UTC
Thanks for the great points guys, HKCU is still a winner for my situation.

Thanks for the rename declarations that will make it simpler, either way its dicey if something goes wrong but hey its IT, what could go wrong that we cant fix?
  1. Helpful
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Tuesday, 14 March 2023 22:00 PM UTC
  2. PowerBuilder
  3. # 2

Hi, Frank - 

In perusing the WinAPI documentation, I can quickly see that using the RegRenameKey API is not a simple coding task.

You need to first open the desired Registry key with RegOpenKeyExW. For that, you need the proper Registry Security Key value (along with the prerequisite rights). There may be additional concerns... I did not delve too deeply into the docs.

The first argument value to RegRenameKey is handle to a Registry key. Window handles should always be declared as PB datatype Longptr so that the API call will work when executed in a 64-bit process/app. There is a pre-defined key handle for HKEY_CURRENT_USER, the #define for which can likely be found in Windows.h.

Also, the RegRenameKey API documentation states that both string values are considered to be constants, so including the REF keyword in the external function declaration is not correct. PB string values are always passed as a memory address (pointer) because they are implemented as a zero-byte-terminated array of Unicode characters. The REF keyword is only needed when the API function will be changing/setting a value to the PB string argument.

My recommendation for you is to bite the bullet and code your desired functionality using the PB-supplied Registry interface functions.

Best regards, John

Comment
  1. Frank Zettanucci
  2. Wednesday, 15 March 2023 02:24 AM UTC
Well thanks everyone Seems that the sensible approach would be use the functions provided,



Maybe appeon can take a hint and prep us a nice clean PB RegistryRename function
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Tuesday, 14 March 2023 22:30 PM UTC
  2. PowerBuilder
  3. # 3

lol,

call me 'old school' but INI files were so much simpler!

Comment
There are no comments made yet.
Frank Zettanucci Accepted Answer Pending Moderation
  1. Tuesday, 14 March 2023 21:28 PM UTC
  2. PowerBuilder
  3. # 4

That is the very amount of work I was hoping to avoid with a single api call (or a few)

it would be much safer imho than copying in/out values and keys and subkeys , which, if went wrong ...

that approach already gives me stress thinking about it

Frank

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.