1
votes

At work we are constantly juggling multiple licenses of a software package. The license key is stored in the registry in HKEY_LOCAL_MACHINE so I wrote a Windows Batch File that when run as administrator, edits the registry key to one of the license strings.

Most recently I've written a GUI based application in VB.net that uses the software package's API to add some cool functionality. I also wanted to include a GUI based license switching module to make the juggling even easier. However, I haven't been able to succeed in doing so.

I've tried multiple methods, running the program as Administrator:

Using VB.Net's Registry module

    Dim key As RegistryKey = Registry.LocalMachine
    Dim autoshell As RegistryKey
    autoshell = key.OpenSubKey(registryKeyDirectory, True)
    autoshell.SetValue("Software", licenseKey)
    autoshell.Close()

Running a .reg script

    Dim p As New ProcessStartInfo

    p.FileName = "regedit.exe"
    p.UseShellExecute = True
    p.Arguments = """C:\test.reg"""
    Process.Start(p)

Sending the Registry edit command to CMD

    Dim process As New Process()
    process.StartInfo.FileName = "cmd.exe"
    process.StartInfo.Verb = "runas"
    process.StartInfo.UseShellExecute = True
    process.StartInfo.Arguments = "/K reg add ""HKEY_LOCAL_MACHINE\SOFTWARE\Software\Licenses\Serial Numbers"" /f /v ""Software"" /t REG_SZ /d """ + licenseKey + """"
    process.Start()

Running the Windows Batch Script through CMD in a similar way to above

'Code is much the same as above

All of these methods worked perfectly with editing a Key in HKEY_CURRENT_USER. I thought it might be the particular key and the permissions relating to it, but after further testing I found that none of the above code could edit any arbitrary key in HKEY_LOCAL_MACHINE. I've looked at just about every link on Stack Overflow relating to this and no-one seems to be having the same problem as me. I suspected it might have to do with the privileges of my user account on the office network, so I took the code and tested it on my home PC but to no avail.

At this point I'm really at my wit's end and would greatly appreciate any help!

Thank you in advance for reading :)

1
Any exception caught? If it is due to insufficient privileges, you should get an exception when you call OpenSubKey or SetValue.kennyzx
Did you see what happens if you try to modify it manually through regedit?Visual Vincent
@kennyzx There were exceptions when run without admin privileges. When run with admin privileges it would frustratingly notify me that the edit was successful (in the case of the .reg script)Josh Kot Sheiss
@VisualVincent Yes, I can modify it manually through regedit and by running a Windows Batch Script no problem. Weird right?Josh Kot Sheiss
maybe your program is 32bit and the system is 64bit and you are actually modifying the values under WOW6432Node. You can get the value immediately after you have set it to see if the value has been actually modified or not.kennyzx

1 Answers

1
votes

This is caused by the Registry Redirector. (Noted: This link seems to be written in the win7 era and kind of outdated). Your program is 32-bit, when running on a 64-bit OS, the call to registry is redirected to the 32-bit logical view of the registry.

In Visual Studio, compile your program to target AnyCPU, also uncheck the "Prefer 32-bit" checkbox. Then the program would run as 64-bit on 64-bit OS, and 32-bit on 32-bit OS.

enter image description here