PDA

Click to See Complete Forum and Search --> : reg


Dec 19th, 2000, 10:36 PM
hey, i cant seem to get all the reg stuff i find on the internet to work, i
was wondering if you could make an api for me;
a text box to enter the decimal number, a command button to wright the
number entered in the text box to a dword value, the place of the dword
value to edit is (HKEY_CURRENT_USER\Software\Microsoft\Microsoft Games\Age
of Empires II: The Conquerors Expansion\1.0) the name of the value is (Game
Speed), so all i have to do is type 20 in the text box and click on the
button and the decimal value of 20 is enterd to the dwordvalue named Game
Speed( witch alreday exists)

Jerry Grant
Dec 20th, 2000, 06:30 AM
The following code allows access to all areas of the registry, both for strings and DWords. This looks like overkill, but the registry is a funny thing if you have ever tried to access it in NT!

Copy the following to a BAS file and call the P_SetKeyValue function to send the data to your key:


Global Const REG_SZ As Long = 1
Global Const REG_DWORD As Long = 4

'// These are the four main sections of the registy //
Global Const HKEY_CLASSES_ROOT = &H80000000
Global Const HKEY_CURRENT_USER = &H80000001
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const HKEY_USERS = &H80000003

Global Const ERROR_NONE = 0
Global Const ERROR_BADDB = 1
Global Const ERROR_BADKEY = 2
Global Const ERROR_CANTOPEN = 3
Global Const ERROR_CANTREAD = 4
Global Const ERROR_CANTWRITE = 5
Global Const ERROR_OUTOFMEMORY = 6
Global Const ERROR_INVALID_PARAMETER = 7
Global Const ERROR_ACCESS_DENIED = 8
Global Const ERROR_INVALID_PARAMETERS = 87
Global Const ERROR_NO_MORE_ITEMS = 259

Global Const KEY_QUERY_VALUE = &H1
Global Const KEY_ENUMERATE_SUB_KEYS = &H8
Global Const KEY_NOTIFY = &H10
Global Const SYNCHRONIZE = &H100000
Global Const READ_CONTROL = &H20000
Global Const STANDARD_RIGHTS_READ = (READ_CONTROL)

Global Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Global Const KEY_ALL_ACCESS = &H3F

Global Const REG_OPTION_NON_VOLATILE = 0

Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long

Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias _
"RegCreateKeyExA" (ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal Reserved As Long, _
ByVal lpClass As String, _
ByVal dwOptions As Long, _
ByVal samDesired As Long, _
ByVal lpSecurityAttributes As Long, _
phkResult As Long, _
lpdwDisposition As Long) As Long

Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
phkResult As Long) As Long

Declare Function RegQueryValueExString Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
ByVal lpData As String, _
lpcbData As Long) As Long

Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, lpData As Long, _
lpcbData As Long) As Long

Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
ByVal lpData As Long, _
lpcbData As Long) As Long

Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal Reserved As Long, _
ByVal dwType As Long, _
ByVal lpValue As String, _
ByVal cbData As Long) As Long

Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal Reserved As Long, _
ByVal dwType As Long, _
lpValue As Long, _
ByVal cbData As Long) As Long


Function F_QueryKeyValue( _
ByVal hKey As Long, _
ByVal lpszSubKey As String, _
ByVal szValueName As String, _
ByRef vValue As Variant _
) As Long

Dim cch As Long
Dim lrc As Long
Dim lType As Long
Dim lValue As Long
Dim sValue As String
Dim lhKey As Long 'handle of opened key

On Error GoTo F_QueryKeyValue_Error

lrc = RegOpenKeyEx(hKey, _
lpszSubKey, _
0, _
KEY_READ, _
lhKey)

If lrc <> ERROR_NONE Then Error 5

'// Determine the size and type of data to be read
lrc = RegQueryValueExNULL(lhKey, _
szValueName, _
0&, _
lType, _
0&, _
cch)

If lrc <> ERROR_NONE Then Error 5

Select Case lType
'// For strings
Case REG_SZ:
sValue = String(cch, 0)
lrc = RegQueryValueExString(lhKey, _
szValueName, _
0&, _
lType, _
sValue, _
cch)
If lrc = ERROR_NONE Then
vValue = Left$(sValue, cch - 1)
Else
vValue = Empty
End If
'// For DWORDS
Case REG_DWORD:
lrc = RegQueryValueExLong(lhKey, _
szValueName, _
0&, _
lType, _
lValue, _
cch)
If lrc = ERROR_NONE Then vValue = lValue
Case Else
'// All other data types not supported
lrc = -1
End Select

F_QueryKeyValue_Exit:
F_QueryKeyValue = lrc
RegCloseKey (hKey)

Exit Function
F_QueryKeyValue_Error:
Resume F_QueryKeyValue_Exit
End Function

Public Sub P_SetKeyValue( _
ByVal hKey As Long, _
sKeyName As String, _
sValueName As String, _
vValueSetting As Variant, _
lValueType As Long _
)

Dim lValue As Long
Dim sValue As String
Dim lRetVal As Long 'result of the SetValueEx function
Dim lhKey As Long 'handle of opened key

lRetVal = RegOpenKeyEx(hKey, _
sKeyName, _
0, _
KEY_ALL_ACCESS, _
lhKey)
Select Case lValueType
Case REG_SZ
sValue = vValueSetting & Chr$(0)
lRetVal = RegSetValueExString(lhKey, _
sValueName, _
0&, _
lValueType, _
sValue, _
Len(sValue))
Case REG_DWORD
lValue = vValueSetting
lRetVal = RegSetValueExLong(lhKey, _
sValueName, _
0&, _
lValueType, _
lValue, _
4)
End Select
RegCloseKey (lhKey)

End Sub



To call use the following




Private Sub Command1_Click()

Call P_SetKeyValue( _
HKEY_CURRENT_USER, _
"Software\Microsoft\Microsoft Games\Age of Empires II: The Conquerors Expansion\1.0", _
"Game Speed", _
20, _
REG_DWORD _
)

End Sub


Hope this helps :cool:

Dec 20th, 2000, 11:12 AM
Call P_SetKeyValue( _
HKEY_CURRENT_USER, _
"Software\Microsoft\Microsoft Games\Age of Empires II: The Conquerors Expansion\1.0", _
"Game Speed", _
20, _
REG_DWORD _
)

it works great but the number 20 entered is fixed,
is there any way i can change it in the program after it is compiled?
like a text box to change the fxixed number 20 to sumthing i type in the text box?

Jerry Grant
Dec 20th, 2000, 03:23 PM
I assumed this was obvious!

Create a Sub routine of Function, thus:

Public Sub SetMyValue(ByVal lngNum as Long)

Call P_SetKeyValue( _
HKEY_CURRENT_USER, _
"Software\Microsoft\Microsoft Games\Age of Empires II: The Conquerors Expansion\1.0", _
"Game Speed", _
lngNum, _
REG_DWORD _
)

End Sub


Call by


If IsNumeric(Text1.Text) Then
Call SetMyValue(Text1.Text)
Else
MsgBox "Enter a valid number!"
End if


I hope this is OK now

:cool:

Dec 20th, 2000, 04:05 PM
THx, jus what i was looking for Works Fine!