// Registry Editing in Visual Basic
//
// http://www.sturmnacht.de.vu
//
// Michael K. (m1o1d1)
// 04.03.2004
Introduction
------------
Many do not know the actual potential of Visual Basic. Many C++ Programmers believe,
that most of the stuff they do on C++ is not possible in VB. I know both languages
and from experience, there is almost nothing VB can't do that C++ can do. Alot
of people don't even know that you CAN use ASM inside of VB, similar to C++. When
people hear VB they think of a language targeted at people who like it simple and
are more interested in how the application looks than rather how it looks. But VB
IS powerful. The only downside is the runtime files it needs. As an example of VBs
capabilities, this is supposed to be a small introduction to API-use in VB (especially
the Registry related API calls).
What is API?
------------
API (short for Application Programming Interface) is a more or less a set of DLLs
that holds functions to ease the life of a programmer. API is a kind of Wrapper
DLL one could say (But it goes far beyond).
How do i use API in VB?
-----------------------
This is the sad thing about VB. In order to use non-VB DLLs, you have to explicitly
declare each function you wish to use from a certain DLL as follows:
Declare Function Lib "" () As
As an Example:
Public Declare Function SetPixelV Lib "gdi32" (ByVal HDC As Long, ByVal X As Long, _
ByVal Y As Long, ByVal crColor As Long) As Long
The usage of this function is exactly the same as with other functions. Simply call it.
Registry? What for?
-------------------
As a normal Programmer, you do not often use registries and your Program is most likely
only set up to call the Registry keys when it's loaded or shutdown, but not permanently
to alter data. This can be different if you are programming a Virus or Trojan or any other
type of malicious code. The registry stores alot of information on Windows computer.
For example, you can set the registry to load your application on Startup. Or retrieve
Information About the OS and it's settings (because Windows saves most settings here).
It's quite powerful, but no use if you do not know what keys are important and which
are not.
Registry API functions
----------------------
To Edit the registry, there are several API functions we can choose from:
/CODE
'open a registry key for access
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
'close an open registry key
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
'create a new registry key
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
'delete the specified registry key
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
'delete a registry value
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
'set the information of an existing value.
Private Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
'get value information.
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
/END OF CODE
Simply copy the above code into your Source code and it is ready to use.
To make it more understandable on how to actually use the Functions. To use them
we need to make a small buffer to store the data the API gives us. We need 2
of those, one for the Key and one for the Value
Dim BufferKey As Long
Dim BufferValue As String
BufferValue = Space(256)
Next we need to Open a Key by using the RegOpenKey function as follows:
RegOpenKey &H80000002, "SOFTWARE\SomeCompany\SomeKey", BufferKey
The first Part is the selection of which "hive" we want to access. With hive,
microsoft basically means which Main directory of the Registry we want. Heres
a list of the other directory and their value:
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_CONFIG = &H80000005
HKEY_CURRENT_USER = &H80000001
HKEY_DYN_DATA = &H80000006
HKEY_LOCAL_MACHINE = &H80000002
HKEY_PERFORMANCE_DATA = &H80000004
HKEY_USERS = &H80000003
The Second Argument is the Actual Key insied of the Hive. In our case it's
the "SomeKey" key, located at "HKEY_LOCAL_MACHINE\SOFTWARE\SomeCompany\".
You can go to Start->Run and type in "Regedit" and have a visual display of all Keys
on your computer. Regedit will help you in locating certain keys you wish to change
The third argument is simply the Buffer the API will store the (if i am not mistaken)
handle of the key opened. We will need this to change Values.
To Get the Values from a Key, we use the "RegQueryValueEx" function as follows:
RegQueryValueEx BufferKey, "SomeValue", 0, REG_SZ, BufferValue, len(BufferValue)
Our first argument is the Handle of our Key we opened.
Next is simply the Name of the Value we want to extract, called "SomeValue".
REG_SZ specifies what type of Value it is (REG_SZ simply means it is a String).
After this we give the API function the Buffer to fill the info in,
and finally the size of the buffer.
Now you have the information inside of your buffer and can read it using standard
methods.
The other Functions work the same and should be pretty straight forward.
Note that the above list of registry API function is not complete, but i
tried to include all the important ones.
It's 3 AM and i'm tired. So good night, and good luck coding :P
               (
geocities.com/xsturmnachtx)