im r
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&
' Registry API prototypes
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
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
Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal Hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Public Const REG_SZ = 1 ' Unicode nul terminated string
Public Const REG_DWORD = 4 ' 32-bit number
Public Sub SaveKey(Hkey As Long, strPath As String)
Dim keyhand&
r = RegCreateKey(Hkey, strPath, keyhand&
r = RegCloseKey(keyhand&
End Sub
Public Function GetString(Hkey As Long, strPath As String, strValue As String)
Dim keyhand As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
r = RegOpenKey(Hkey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " "
lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))
If intZeroPos > 0 Then
GetString = Left$(strBuf, intZeroPos - 1)
Else
GetString = strBuf
End If
End If
End If
RegCloseKey keyhand
End Function
Public Sub SaveString(Hkey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(Hkey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
r = RegCloseKey(keyhand)
End Sub
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 r As Long
Dim keyhand As Long
If lResult = ERROR_SUCCESS Then
If lValueType = REG_DWORD Then
GetDword = lBuf
End If
'Else
' Call errlog("GetDWORD-" & strPath, False)
End If
r = RegCloseKey(keyhand)
End Function
Function SaveDword(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
Dim lResult As Long
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(Hkey, strPath, keyhand)
lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD, lData, 4)
'If lResult <> error_success Then Call errlog("SetDWORD", False)
r = RegCloseKey(keyhand)
End Function
Public Function DeleteKey(ByVal Hkey As Long, ByVal strKey As String)
Dim r As Long
r = RegDeleteKey(Hkey, strKey)
End Function
Public Function DeleteValue(ByVal Hkey As Long, ByVal strPath As String, ByVal strValue As String)
Dim keyhand As Long
r = RegOpenKey(Hkey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)
End Function
Вот простой пример кода, который я пытаюсь использовать, вместо параметра всегда возвращается пустая строка. Подскажите, где тут ошибка?
#Compile Exe
#Dim All
#Include "WIN32API.INC"
Function RegGetValuePB(MainKey As Long, SubKey As Asciiz * 255, Value As Asciiz * 255, ByVal pData As Long, ByVal cbDataSize As Long) As Long
Dim lValueType As Long
Dim lRet As Long
Dim lpHKey As Long
If MainKey >= &H80000000 And MainKey <= &H80000006 Then
lRet = RegOpenKeyEx(MainKey, SubKey, 0&, %KEY_READ, lpHKey)
readmore:
lRet = RegQueryValueEx(lpHKey, Value, ByVal %Null, lValueType, ByVal pData, cbDataSize)
If lRet = %ERROR_MORE_DATA Then GoTo readmore
lRet = RegCloseKey(lpHKey)
End If
End Function
Function PBMain () As Long
Dim sMsg As String
sMsg = Space$(2048)
RegGetValuePB(%HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "REG_EXPAND_SZ Startup", StrPtr(sMsg), 1024)
MsgBox sMsg
End Function
Вообще-то Startup может быть и не REG_EXPAND_SZ, а REG_SZ, кроме того, он может называться "Common Startup"
Поэтому неплохо было бы проверить на тип значения, и на ERROR_SUCCESS. И кроме того, буферы строк делать AS ASCIIZ * 256
 IM sMsg AS ASCIIZ * 256
 IM KeyName AS ASCIIZ * 256
 IM ValueName AS ASCIIZ * 256
 IM res AS LONG
 IM lValueType AS LONG
 IM lRet AS LONG
 IM lpHKey AS LONG
KeyName = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
ValueName = "Personal"
lRet = RegOpenKeyEx(%HKEY_LOCAL_MACHINE, KeyName, 0&, %KEY_READ, lpHKey)
lRet = RegQueryValueEx(lpHKey, ValueName, 0, lValueType, sMsg, 2048)
IF lRet = %ERROR_SUCCESS THEN
MSGBOX sMsg
END IF
lRet = RegCloseKey(lpHKey)
'=============================================================================='' Registry routines for 32-bit PB/DLL' Copyright (c) 1997,98 by PowerBASIC, Inc. All Rights Reserved.'' Note: Requires WIN32API.INC file''==============================================================================%HK = %HKEY_CURRENT_USER 'Class where entries are saved and retrieved'------------------------------------------------------------------------------'' SaveSetting - Saves or creates an application entry in the Windows registry.'' Syntax:' Result = SaveSetting(AppName, Section, Key, Setting)'' SaveSetting AppName, Section, Key, Setting'' Where:'' AppName = String expression containing the name of the application.' Section = String expression containing the name of the section where the' key setting is being saved.' Key = String expression containing the name of the key setting being' saved.' Setting = String expression containing the value that 'key' is being set' to.'FUNCTION SaveSetting(BYVAL AppName AS STRING, BYVAL Section AS STRING, _ BYVAL Key AS STRING, BYVAL Setting AS STRING) AS LONG LOCAL hKey AS LONG LOCAL Result AS LONG LOCAL zText AS ASCIIZ * 2048' ** Exit is AppName, Section or Key are null IF (LEN(AppName) * LEN(Section) * LEN(Key)) = 0 THEN EXIT FUNCTION END IF' ** Create the section IF RegCreateKeyEx(%HK, "SOFTWARE\"+AppName+"\"+Section, 0, "", _ %REG_OPTION_NON_VOLATILE, %KEY_ALL_ACCESS, BYVAL %NULL, _ hKey, Result) <> %ERROR_SUCCESS THEN EXIT FUNCTION END IF' ** Save the value for the key IF LEN(Setting) THEN zText = Setting RegSetValueEx hKey, Key+CHR$(0), 0, %REG_SZ, zText, LEN(Setting)+1 ELSE RegSetValueEx hKey, Key+CHR$(0), 0, %REG_SZ, BYVAL %NULL, 0 END IF' ** Close the key RegCloseKey hKey FUNCTION = %TRUEEND FUNCTION'------------------------------------------------------------------------------'' SaveSettingDW - Saves or creates an application entry in the Windows registry.'' Syntax:' Result = SaveSettingDW(AppName, Section, Key, Setting)'' SaveSettingDW AppName, Section, Key, Setting'' Where:'' AppName = String expression containing the name of the application.' Section = String expression containing the name of the section where the' key setting is being saved.' Key = String expression containing the name of the key setting being' saved.' Setting = Numeric expression containing the value that 'key' is being set' to.'FUNCTION SaveSettingDW(BYVAL AppName AS STRING, BYVAL Section AS STRING, _ BYVAL Key AS STRING, BYVAL Setting AS LONG) AS LONG LOCAL hKey AS LONG LOCAL Result AS LONG' ** Exit is AppName, Section or Key are null IF (LEN(AppName) * LEN(Section) * LEN(Key)) = 0 THEN EXIT FUNCTION END IF' ** Create the section IF RegCreateKeyEx(%HK, "SOFTWARE\"+AppName+"\"+Section, 0, "", _ %REG_OPTION_NON_VOLATILE, %KEY_ALL_ACCESS, BYVAL %NULL, _ hKey, Result) <> %ERROR_SUCCESS THEN EXIT FUNCTION END IF' ** Save the value for the key RegSetValueEx hKey, Key+CHR$(0), 0, %REG_DWORD, Setting, 4' ** Close the key RegCloseKey hKey FUNCTION = %TRUEEND FUNCTION'------------------------------------------------------------------------------'' SaveSettingB - Saves or creates an application entry in the Windows registry.'' Syntax:' Result = SaveSettingB(AppName, Section, Key, Setting, Length)'' SaveSettingB AppName, Section, Key, Setting, Length'' Where:'' AppName = String expression containing the name of the application.' Section = String expression containing the name of the section where the' key setting is being saved.' Key = String expression containing the name of the key setting being' saved.' Setting = Binary expression containing the value that 'key' is being set' to.' Length = Length of binary data in Setting'FUNCTION SaveSettingB(BYVAL AppName AS STRING, BYVAL Section AS STRING, _ BYVAL Key AS STRING, ANY, BYVAL Length AS LONG) AS LONG LOCAL hKey AS LONG LOCAL Result AS LONG LOCAL pSetting AS DWORD' ** Exit is AppName, Section or Key are null IF (LEN(AppName) * LEN(Section) * LEN(Key)) = 0 THEN EXIT FUNCTION END IF' ** Get a pointer to the setting value ! mov EAX, [EBP+20] ! mov pSetting, EAX' ** Create the section IF RegCreateKeyEx(%HK, "SOFTWARE\"+AppName+"\"+Section, 0, "", _ %REG_OPTION_NON_VOLATILE, %KEY_ALL_ACCESS, BYVAL %NULL, _ hKey, Result) <> %ERROR_SUCCESS THEN EXIT FUNCTION END IF' ** Save the value for the key RegSetValueEx hKey, Key+CHR$(0), 0, %REG_BINARY, BYVAL pSetting, Length' ** Close the key RegCloseKey hKey FUNCTION = %TRUEEND FUNCTION'------------------------------------------------------------------------------'' GetSetting - Retrieves application entry in the Windows registry.'' Syntax:' Value = GetSetting(AppName, Section, Key)'' Where:'' AppName = String expression containing the name of the application.' Section = String expression containing the name of the section where the' key setting is being saved.' Key = String expression containing the name of the key setting being' saved.'  efault = Default value if no registry value is found.'' Returns:'' Value = Value from the registry. If the value is DWORD, use the CVL()' or CVDWD() functions to convert from string to numeric.'FUNCTION GetSetting(BYVAL AppName AS STRING, BYVAL Section AS STRING, _ BYVAL Key AS STRING, BYVAL Default AS STRING) AS STRING LOCAL hKey AS LONG LOCAL Result AS LONG LOCAL KeyType AS LONG LOCAL Buffer AS STRING * 2048 LOCAL Size AS LONG' ** Exit is AppName, Section or Key are null IF (LEN(AppName) * LEN(Section) * LEN(Key)) = 0 THEN FUNCTION = Default EXIT FUNCTION END IF' ** Open the section IF RegOpenKeyEx(%HK, "SOFTWARE\"+AppName+"\"+Section, 0, %KEY_ALL_ACCESS, _ hKey) <> %ERROR_SUCCESS THEN FUNCTION = Default EXIT FUNCTION END IF' ** Get the key value Size = SIZEOF(Buffer) Result = RegQueryValueEx(hKey, Key+CHR$(0), 0, KeyType, Buffer, Size)' ** Close the registry RegCloseKey hKey' ** Exit if not successful or nothing there IF (Result <> %ERROR_SUCCESS) OR (Size = 0) THEN FUNCTION = Default EXIT FUNCTION END IF' ** Return the data IF KeyType = %REG_SZ THEN FUNCTION = LEFT$(Buffer, Size - 1) ELSE FUNCTION = LEFT$(Buffer, Size) END IFEND FUNCTION
'==============================================================================
'
' Registry routines for 32-bit PB/DLL
' Copyright (c) 1997,98 by PowerBASIC, Inc. All Rights Reserved.
'
' Note: Requires WIN32API.INC file
'
'==============================================================================<p>%HK = %HKEY_CURRENT_USER 'Class where entries are saved and retrieved<p>'------------------------------------------------------------------------------
'
' SaveSetting - Saves or creates an application entry in the Windows registry.
'
' Syntax:
' Result = SaveSetting(AppName, Section, Key, Setting)
'
' SaveSetting AppName, Section, Key, Setting
'
' Where:
'
' AppName = String expression containing the name of the application.
' Section = String expression containing the name of the section where the
' key setting is being saved.
' Key = String expression containing the name of the key setting being
' saved.
' Setting = String expression containing the value that 'key' is being set
' to.
'
FUNCTION SaveSetting(BYVAL AppName AS STRING, BYVAL Section AS STRING, _
BYVAL Key AS STRING, BYVAL Setting AS STRING) AS LONG<p> LOCAL hKey AS LONG
LOCAL Result AS LONG
LOCAL zText AS ASCIIZ * 2048<p>' ** Exit is AppName, Section or Key are null
IF (LEN(AppName) * LEN(Section) * LEN(Key)) = 0 THEN
EXIT FUNCTION
END IF<p>' ** Create the section
IF RegCreateKeyEx(%HK, "SOFTWARE\"+AppName+"\"+Section, 0, "", _
%REG_OPTION_NON_VOLATILE, %KEY_ALL_ACCESS, BYVAL %NULL, _
hKey, Result) <> %ERROR_SUCCESS THEN
EXIT FUNCTION
END IF<p>' ** Save the value for the key
IF LEN(Setting) THEN
zText = Setting
RegSetValueEx hKey, Key+CHR$(0), 0, %REG_SZ, zText, LEN(Setting)+1
ELSE
RegSetValueEx hKey, Key+CHR$(0), 0, %REG_SZ, BYVAL %NULL, 0
END IF<p>' ** Close the key
RegCloseKey hKey<p> FUNCTION = %TRUE<p>END FUNCTION<p>
'------------------------------------------------------------------------------
'
' SaveSettingDW - Saves or creates an application entry in the Windows registry.
'
' Syntax:
' Result = SaveSettingDW(AppName, Section, Key, Setting)
'
' SaveSettingDW AppName, Section, Key, Setting
'
' Where:
'
' AppName = String expression containing the name of the application.
' Section = String expression containing the name of the section where the
' key setting is being saved.
' Key = String expression containing the name of the key setting being
' saved.
' Setting = Numeric expression containing the value that 'key' is being set
' to.
'
FUNCTION SaveSettingDW(BYVAL AppName AS STRING, BYVAL Section AS STRING, _
BYVAL Key AS STRING, BYVAL Setting AS LONG) AS LONG<p> LOCAL hKey AS LONG
LOCAL Result AS LONG<p>' ** Exit is AppName, Section or Key are null
IF (LEN(AppName) * LEN(Section) * LEN(Key)) = 0 THEN
EXIT FUNCTION
END IF<p>' ** Create the section
IF RegCreateKeyEx(%HK, "SOFTWARE\"+AppName+"\"+Section, 0, "", _
%REG_OPTION_NON_VOLATILE, %KEY_ALL_ACCESS, BYVAL %NULL, _
hKey, Result) <> %ERROR_SUCCESS THEN
EXIT FUNCTION
END IF<p>' ** Save the value for the key
RegSetValueEx hKey, Key+CHR$(0), 0, %REG_DWORD, Setting, 4<p>' ** Close the key
RegCloseKey hKey<p> FUNCTION = %TRUE<p>END FUNCTION<p>
'------------------------------------------------------------------------------
'
' SaveSettingB - Saves or creates an application entry in the Windows registry.
'
' Syntax:
' Result = SaveSettingB(AppName, Section, Key, Setting, Length)
'
' SaveSettingB AppName, Section, Key, Setting, Length
'
' Where:
'
' AppName = String expression containing the name of the application.
' Section = String expression containing the name of the section where the
' key setting is being saved.
' Key = String expression containing the name of the key setting being
' saved.
' Setting = Binary expression containing the value that 'key' is being set
' to.
' Length = Length of binary data in Setting
'
FUNCTION SaveSettingB(BYVAL AppName AS STRING, BYVAL Section AS STRING, _
BYVAL Key AS STRING, ANY, BYVAL Length AS LONG) AS LONG<p> LOCAL hKey AS LONG
LOCAL Result AS LONG
LOCAL pSetting AS DWORD<p>' ** Exit is AppName, Section or Key are null
IF (LEN(AppName) * LEN(Section) * LEN(Key)) = 0 THEN
EXIT FUNCTION
END IF<p>' ** Get a pointer to the setting value
! mov EAX, [EBP+20]
! mov pSetting, EAX<p>' ** Create the section
IF RegCreateKeyEx(%HK, "SOFTWARE\"+AppName+"\"+Section, 0, "", _
%REG_OPTION_NON_VOLATILE, %KEY_ALL_ACCESS, BYVAL %NULL, _
hKey, Result) <> %ERROR_SUCCESS THEN
EXIT FUNCTION
END IF<p>' ** Save the value for the key
RegSetValueEx hKey, Key+CHR$(0), 0, %REG_BINARY, BYVAL pSetting, Length<p>' ** Close the key
RegCloseKey hKey<p> FUNCTION = %TRUE<p>END FUNCTION<p>
'------------------------------------------------------------------------------
'
' GetSetting - Retrieves application entry in the Windows registry.
'
' Syntax:
' Value = GetSetting(AppName, Section, Key)
'
' Where:
'
' AppName = String expression containing the name of the application.
' Section = String expression containing the name of the section where the
' key setting is being saved.
' Key = String expression containing the name of the key setting being
' saved.
'  efault = Default value if no registry value is found.
'
' Returns:
'
' Value = Value from the registry. If the value is DWORD, use the CVL()
' or CVDWD() functions to convert from string to numeric.
'
FUNCTION GetSetting(BYVAL AppName AS STRING, BYVAL Section AS STRING, _
BYVAL Key AS STRING, BYVAL Default AS STRING) AS STRING<p> LOCAL hKey AS LONG
LOCAL Result AS LONG
LOCAL KeyType AS LONG
LOCAL Buffer AS STRING * 2048
LOCAL Size AS LONG<p>' ** Exit is AppName, Section or Key are null
IF (LEN(AppName) * LEN(Section) * LEN(Key)) = 0 THEN
FUNCTION = Default
EXIT FUNCTION
END IF<p>' ** Open the section
IF RegOpenKeyEx(%HK, "SOFTWARE\"+AppName+"\"+Section, 0, %KEY_ALL_ACCESS, _
hKey) <> %ERROR_SUCCESS THEN
FUNCTION = Default
EXIT FUNCTION
END IF<p>' ** Get the key value
Size = SIZEOF(Buffer)
Result = RegQueryValueEx(hKey, Key+CHR$(0), 0, KeyType, Buffer, Size)<p>' ** Close the registry
RegCloseKey hKey<p>' ** Exit if not successful or nothing there
IF (Result <> %ERROR_SUCCESS) OR (Size = 0) THEN
FUNCTION = Default
EXIT FUNCTION
END IF<p>' ** Return the data
IF KeyType = %REG_SZ THEN
FUNCTION = LEFT$(Buffer, Size - 1)
ELSE
FUNCTION = LEFT$(Buffer, Size)
END IF<p>END FUNCTION