What's new

Vb6 Question

Olger901

Banned
Hello there I got a file called test.reg with some registry lines and I want to add it to the registry with a vb command. How would I do this???
 

Doomulation

?????????????????????????
There is no command to my knowlege. You'll have to read it and add the settings accordingly (correct me if i'm wrong).
 

Eagle

aka Alshain
Moderator
ok, registry editing has to be done through Windows API. There are several functions and unfortunately unlike C++ you cant just overload so they will all have the same name. So basically we will have several functions, one for writing strings, one for writing DWords (long integers), one for writing Binary (Bytes). In addition for each of these you need one to save the value and one to get the value. Then you need one for creating a key and deleteing a key and one for deleting a value (the values are automatically created if they dont exist when you try to write to them). All of this is very complicated and at your point would probably futile to try and explain completely but thats the beauty of functions, subs and modules, so... add the following module to your program and use this to call the functions/subs

Hkey Constants

HKLM = HKEY_LOCAL_MACHINE
HKCU = HKEY_CURRENT_USER
HKU = HKEY_USERS
HKCC = HKEY_CURRENT_CONFIG
HKCR = HKEY_CLASSES_ROOT

In most cases you will want to use only HKLM unless your program is to only affect the user that is logged on currently in which case you would use HKCU. Below you will substitute [HKEY] with one of the above.


For the following, [PATH] refers to the location of the key or value. In the case of creating a key or deleting a key it will also include the name of the key you wish to create or delete.

Example: "Software\VirtuaWallpaper\Settings"
With this example, you would either be creating or deleting the Settings key OR you would be placing a value in the settings key. The VirtuaWallpaper key would remain intact if you deleted it, If you were creating a key the VirtuaWallpaper key would be created if it didnt already exisit (I think) It would be best to create the keys in seperate lines cause I'm not sure if that would work or not.

[VALUE] is the name of the value you wish to write/read/delete.

[DATA] is what you want to write to the key

[DEFAULT] is what the function will return if for some reason the value can not be read properly and IS OPTIONAL.

Please pay attention to where quote marks are located. If the information is stored in a variable you would of course omit the quotes.

Keys

Creating
Code:
CreateRegKey([HKEY], "[PATH]")

Deleting
Code:
DeleteRegKey([HKEY], "[PATH]")

Values

Deleting
Code:
DeleteRegValue([HKEY], "[PATH]", "[VALUE]")

Creating/Writing String, Longs(DWords), and Binary(Byte) data
Code:
SaveSettingString([HKEY], "[PATH]", "[VALUE]", "[DATA]")
SaveSettingLong([HKEY], "[PATH]", "[VALUE]", [DATA])
SaveSettingByte([HKEY], "[PATH]", "[VALUE]", [DATA])

Reading String, Long, and Binary data
Code:
dim strString as String
dim lngLong as Long
dim intBinary as Integer

strString = GetSettingString([HKEY], "[PATH]", "[VALUE]", "[DEFAULT]")
lngLong = GetSettingLong([HKEY], "[PATH]", "[VALUE]", [DEFAULT])
intBinary = GetSettingByte([HKEY], "[PATH]", "[VALUE]", [DEFAULT])


Well that about covers all the practical purposes of the registry. Remember to add the module to your program and remember all your registry values should be in the HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER under the Software sub key under a subkey of your own creation, unless you are trying to do something odd such as get your program to start with windows in which case you would have to write it to the appropriate place. Good Luck.
 

Eagle

aka Alshain
Moderator
Well, that will teach me to read everything carefully first. I just noticed you wanted to add an existing *.reg file. This can be done easily actually, but can be messy in regards to user friendliness. Since *.reg files are associated with the REGEDIT program, you can just run them directly with their default program. The following would open a reg file with REGEDIT, or a text file with Notepad, or a doc file with Word, etc etc.

Code:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Call ShellExecute(0, "Open", FileName, 0&, 0&, vbNormalFocus)

This however always prompts the user and asks if the reg file should be added (just like double clicking the reg file) If the user chooses no, that could be kinda messy. You should always think of your user as being the most computer illiterate person on the face of the planet, cause they might just be. Therefore you need to make your program do all the "hard to understand" stuff automatically without bothering the user. Quite often the user may be stupid enough to click "No".
 
Last edited:

Doomulation

?????????????????????????
Small note: the savesetting api does NOT create a key if it does not exist. It will return an error. "Key does not exist"
 

Eagle

aka Alshain
Moderator
Doomulation said:
Small note: the savesetting api does NOT create a key if it does not exist. It will return an error. "Key does not exist"

Correct, but it will create a value. At least I think it will. I dont know I never really gave it the chance, my install program always creates all the necessary keys/values and the program just modifies it.

EDIT: No I have given it the chance, the start with windows option on VirtuaWallpaper creates/deletes a registry value in the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run path.
 

Top