Using 32-bit app to write to 64-bit sections of the registry

To start with I’m using an old version RealBasic 2008 r4.2

My 32-bit app will be run on both 32-bit and 64-bit Windows systems. I’m trying to edit the registry to set the Powershell ExecutionPolicy and on 32-bit systems it works correctly, but on 64-bit systems the path it’s writing to is being redirected by the OS.

  dim registry As RegistryItem
  registry = New RegistryItem("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\PowerShell\\1\\ShellIds\\Microsoft.PowerShell")
  registry.Value("ExecutionPolicy") = "Unrestricted"

On 64-bit systems, Microsoft reroutes that path to:

I need to be able to write to both paths.

I had the same type of problem when writing files to the system32 directory. In that case I found that if I use sysnative instead of System32 in the path, I was able to write the files to the system32 directory instead of it being redirected to C:\Windows\SysWOW64. Is there a similar way to do this in the registry?

If this isnt supported in the Xojo framework, you will have to make the calls yourself using declares, see for more information.

I’m not sure how this will affect you.

For MBS Plugins, you can use the RegistryMBS classes.

There you can set Use64bitRegistry parameter to true to read/write into 64-bit registry from 32-bit app.

Thanks, I will take a look at both of those options.

Is the MBS plugin free?

If you plan to use the MBS Plugins, we’d be happy if you buy a license key.

Sorry for the delay in responding. I was side tracked.

Christian, I’d love to upgrade and get the MSB plugins, but unfortunately management doesn’t want to spend the money if we can meet our goal with what we already have. I think that decision is short sided, but it’s what I have to deal with.

Julian, I’m currently testing using declares but I’m not a C programmer so getting the calls constructed correctly is proving to be a little more difficult that I expected. Creating the declare to RegOpenKeyEx is working but I’m still working on the correct way to construct the declare for the RegSetValueEx function. I think I’ll have that figured out soon.

Here you go Ron, I was going to give you a project but I dont have RealBasic 2008 r4.2 so here’s the code, it might need a little tweak for 2008 r4.2 but I’m sure you’ll figure it out :slight_smile: Let me know if not.

[code]'For declare help see https://blog./2017/01/22/windows-to-xojo-data-type-conversion/
Declare Function RegOpenKeyEx Lib “Advapi32” Alias “RegOpenKeyExW” (hKey As Integer, lpSubKey As WString, ulOptions As UInt32, samDesired As UInt32, ByRef phkResult As Integer) As Int32
Declare Function RegSetValueEx Lib “Advapi32” Alias “RegSetValueExW” (hKey As Integer, lpValueName As WString, Reserved As UInt32, dwType As UInt32, lpData As WString, cbData As UInt32) As Int32
Declare Function RegCloseKey Lib “Advapi32” (handle As Integer) As Int32

Const HKEY_LOCAL_MACHINE = &h80000002

Const KEY_SET_VALUE = &h0002

Const KEY_WOW64_64KEY = &h0100
Const KEY_WOW64_32KEY = &h0200

Const REG_SZ = 1

Dim subKey As String = “SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell”
Dim handle As Integer
Dim ok As Integer

'ok = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey, 0, BitwiseOr(KEY_SET_VALUE, KEY_WOW64_32KEY), handle) ’ into 32 bit node Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft…
ok = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey, 0, BitwiseOr(KEY_SET_VALUE, KEY_WOW64_64KEY), handle) ’ into 64 bit node Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft…

If ok = 0 Then
dim s as string = “Unrestricted”
ok = RegSetValueEx(handle, “ExecutionPolicy”, 0, REG_SZ, s, (s.len*2)) '*2 because we are using WString not CString
ok = RegCloseKey(handle)
End If

Great, thanks for the code example. I’ll give it a try in a little bit.

I was working off the example in this thread which does a query but doesn’t set any value.

I added another MsgBox statement in an else block in-case the conditional failed.

The code worked correctly when tested on my Win7 32-bit system, but both RegOpenKeyEx statements failed with error code 5 when I tested on my Win10 64-bit system.

You need to run the exe as admin if you’re writing there.

KEY_SET_VALUE is for “Required to create, delete, or set a registry value.” so the error will occur there if it can’t.

Dumb me, I should have known that. I was running it within the IDE.

It works When run as administrator.

One last question on this topic. Is better (best practice) to write hard declares like you’ve shown or soft declares?

Soft would be better as it allows you to catch problems inside Try Catch blocks rather than the program not even launching, I just throw them together so fast I forget about that :slight_smile: