Restored from previous forum. Originally posted by PB.
I think there is a bug in PB because you are also unable to use Windows API to access the Registry in versions 2.50 and higher.
I'm still waiting for someone to look into this and find a solution.
Here's how I used to read/write the Registry in Visual Basic...
Perhaps you (or someone else) can translate it to PureBasic?
Code: Select all
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_DYN_DATA = &H80000006
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_USERS = &H80000003
'
' Read Registry string
Public Function GetSettingString(hKey As Long, strPath As String, strValue As String, Optional Default As String) As String
Dim hCurKey As Long
Dim lValueType As Long
Dim strBuffer As String
Dim lDataBufferSize As Long
Dim intZeroPos As Integer
Dim lRegResult As Long
If Not IsEmpty(Default) Then
GetSettingString = Default
Else
GetSettingString = ""
End If
lRegResult = RegOpenKey(hKey, strPath, hCurKey)
lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
If lRegResult = 0& Then
If lValueType = 1 Then
strBuffer = String(lDataBufferSize, " ")
lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
intZeroPos = InStr(strBuffer, Chr(0))
If intZeroPos > 0 Then
GetSettingString = Left(strBuffer, intZeroPos - 1)
Else
GetSettingString = strBuffer
End If
End If
End If
lRegResult = RegCloseKey(hCurKey)
End Function
'
' Write Registry string
Public Sub SaveSettingString(hKey As Long, strPath As String, strValue As String, strData As String)
Dim hCurKey As Long
Dim lRegResult As Long
lRegResult = RegCreateKey(hKey, strPath, hCurKey)
lRegResult = RegSetValueEx(hCurKey, strValue, 0, 1, ByVal strData, Len(strData))
lRegResult = RegCloseKey(hCurKey)
End Sub
'
' Read Registry DWord
Public Function GetDWord(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim ret As Long
Dim keyhand As Long
ret = RegOpenKey(hKey, strPath, keyhand): lDataBufSize = 4
lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
If lResult = 0& And lValueType = 4& Then GetDWord = lBuf
ret = RegCloseKey(keyhand)
End Function
'
' Example usages...
'
' Read PC's current IP address (string)
Dim ip As String
ip = GetSettingString(HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Services\Class\NetTrans\0000\", "IPAddress")
'
' Change PC's IP address to "192.0.0.1" (string)
Dim ip As String
ip = "192.0.0.1"
SaveSettingString HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Services\Class\NetTrans\0000\", "IPAddress", ip
'
' Read PC's "Show Full Folder Path" setting (DWord)
Dim r As Long
r = GetDWord(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\CabinetState\", "FullPath")
PB - Registered PureBasic Coder
Edited by - PB on 08 December 2001 02:22:02