Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the Registry API 2

Status
Not open for further replies.

WP

Programmer
Nov 30, 1999
463
CH
I need to find the loaction of MSACCESS.<br>
<br>
I know the reg key is <br>
<br>
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\8.0\BinDirPath<br>
<br>
Can anyone provide sample code for this API.<br>
<br>
Many thanks.<br>
<br>
WP <p>Bill Paton<br><a href=mailto:wpaton@neptune400.co.uk>wpaton@neptune400.co.uk</a><br><a href=
 
Below is a VB module for reg stuff<br>
<br>
look for &quot;Sub Main()&quot; for the example code<br>
<br>
-ml<br>
<br>
<br>
Attribute VB_Name = &quot;RegistryCalls&quot;<br>
'API Function and Constant Declarations<br>
'--------------------------------------<br>
<br>
Option Explicit<br>
<br>
'***Declare the value data types<br>
Global Const REG_SZ As Long = 1 '***Registry string<br>
Global Const REG_DWORD As Long = 4 '***Registry number (32-bit number)<br>
<br>
'***Declare the keys that should exist.<br>
'***Typically applications will put information under HKEY_CURRENT_USER<br>
Global Const HKEY_CLASSES_ROOT = &H80000000<br>
Global Const HKEY_CURRENT_USER = &H80000001<br>
Global Const HKEY_LOCAL_MACHINE = &H80000002<br>
Global Const HKEY_USERS = &H80000003<br>
<br>
'***Errors<br>
Global Const ERROR_NONE = 0<br>
Global Const ERROR_BADDB = 1<br>
Global Const ERROR_BADKEY = 2<br>
Global Const ERROR_CANTOPEN = 3<br>
Global Const ERROR_CANTREAD = 4<br>
Global Const ERROR_CANTWRITE = 5<br>
Global Const ERROR_OUTOFMEMORY = 6<br>
Global Const ERROR_INVALID_PARAMETER = 7<br>
Global Const ERROR_ACCESS_DENIED = 8<br>
Global Const ERROR_INVALID_PARAMETERS = 87<br>
Global Const ERROR_NO_MORE_ITEMS = 259<br>
<br>
'***Gives all users full access to the key<br>
Global Const KEY_ALL_ACCESS = &H3F<br>
<br>
'***Creates a key that is persistent<br>
Global Const REG_OPTION_NON_VOLATILE = 0<br>
<br>
Global gstrAppVersion As String<br>
<br>
'***Registry API declarations<br>
Declare Function RegCloseKey Lib &quot;advapi32.dll&quot; (ByVal hKey As Long) As Long<br>
<br>
Declare Function RegCreateKeyEx Lib &quot;advapi32.dll&quot; Alias _<br>
&quot;RegCreateKeyExA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, _<br>
ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions _<br>
As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes _<br>
As Long, phkResult As Long, lpdwDisposition As Long) As Long<br>
<br>
Declare Function RegOpenKeyEx Lib &quot;advapi32.dll&quot; Alias _<br>
&quot;RegOpenKeyExA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, _<br>
ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _<br>
Long) As Long<br>
<br>
Declare Function RegQueryValueExString Lib &quot;advapi32.dll&quot; Alias _<br>
&quot;RegQueryValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As _<br>
String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _<br>
As String, lpcbData As Long) As Long<br>
<br>
Declare Function RegQueryValueExLong Lib &quot;advapi32.dll&quot; Alias _<br>
&quot;RegQueryValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As _<br>
String, ByVal lpReserved As Long, lpType As Long, lpData As _<br>
Long, lpcbData As Long) As Long<br>
<br>
Declare Function RegQueryValueExNULL Lib &quot;advapi32.dll&quot; Alias _<br>
&quot;RegQueryValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As _<br>
String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _<br>
As Long, lpcbData As Long) As Long<br>
<br>
Declare Function RegSetValueExString Lib &quot;advapi32.dll&quot; Alias _<br>
&quot;RegSetValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As String, _<br>
ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _<br>
String, ByVal cbData As Long) As Long<br>
<br>
Declare Function RegSetValueExLong Lib &quot;advapi32.dll&quot; Alias _<br>
&quot;RegSetValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As String, _<br>
ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _<br>
ByVal cbData As Long) As Long<br>
<br>
<br>
Public Function SetValueEx(ByVal hKey As Long, sValueName As String, _<br>
lType As Long, vValue As Variant) As Long<br>
'*** Called By: SetKeyValue<br>
'*** Description: Wrapper function around the registry API calls<br>
'*** RegSetValueExString/Long. Determines if the value<br>
'*** is a string or a long and calls the appropriate API.<br>
'*** Return Value: Returns the API call's return value, which is its<br>
'*** status (successful, error).<br>
<br>
Dim lValue As Long<br>
Dim sValue As String<br>
<br>
Select Case lType<br>
'***String?<br>
Case REG_SZ<br>
sValue = vValue<br>
SetValueEx = RegSetValueExString(hKey, sValueName, 0&, _<br>
lType, sValue, Len(sValue))<br>
'***32-bit number?<br>
Case REG_DWORD<br>
lValue = vValue<br>
SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, _<br>
lType, lValue, 4)<br>
End Select<br>
End Function<br>
Private Function QueryValueEx( _<br>
ByVal lhKey As Long, _<br>
ByVal szValueName As String, _<br>
vValue As Variant _<br>
) As Long<br>
'*** Called By: QueryValue<br>
'*** Description: Wrapper function around the registry API calls to<br>
'*** RegQueryValueExLong and RegQueryValueExString.<br>
'*** Determines size and type of data to be read.<br>
'*** Determines if the value is a string or a long<br>
'*** and calls the appropriate API.<br>
'*** Return Value: Returns the API call's return value, which is its<br>
'*** status (successful, error). The parameter vValue<br>
'*** contains the value queried.<br>
<br>
Dim cch As Long<br>
Dim lrc As Long<br>
Dim lType As Long<br>
Dim lValue As Long<br>
Dim sValue As String<br>
<br>
On Error GoTo QueryValueExError<br>
<br>
' Determine the size and type of data to be read<br>
lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)<br>
<br>
If lrc &lt;&gt; ERROR_NONE Then Error 5<br>
<br>
Select Case lType<br>
' For strings<br>
Case REG_SZ:<br>
sValue = String(cch, 0)<br>
lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)<br>
If lrc = ERROR_NONE Then<br>
If Mid(sValue, cch, 1) = Chr(0) Then<br>
vValue = Left$(sValue, cch - 1) ' get rid of trailing AsciiZ<br>
Else<br>
vValue = Left$(sValue, cch)<br>
End If<br>
Else<br>
vValue = Empty<br>
End If<br>
' For DWORDS<br>
Case REG_DWORD:<br>
lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)<br>
If lrc = ERROR_NONE Then vValue = lValue<br>
Case Else<br>
'all other data types not supported<br>
lrc = -1<br>
End Select<br>
<br>
QueryValueExExit:<br>
QueryValueEx = lrc<br>
Exit Function<br>
QueryValueExError:<br>
Resume QueryValueExExit<br>
End Function<br>
<br>
Public Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)<br>
'***With this procedure a call of<br>
'*** CreateNewKey &quot;TestKey&quot;, HKEY_CURRENT_USER<br>
'***will create a key called TestKey immediately under HKEY_CURRENT_USER.<br>
'***Calling CreateNewKey like this<br>
'*** CreateNewKey &quot;TestKey\SubKey1\SubKey2&quot;, HKEY_CURRENT_USER<br>
'***will create a three-nested keys beginning with TestKey immediately under<br>
'***HKEY_CURRENT_USER, Subkey1 subordinate to TestKey, and SubKey3 under<br>
'***SubKey2.<br>
<br>
'*** Called by: your own code to create keys<br>
'*** Description: Wrapper around the RegCreateKeyEx API call.<br>
<br>
Dim hNewKey As Long 'handle to the new key<br>
Dim lRetVal As Long 'result of the RegCreateKeyEx function<br>
<br>
lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, _<br>
vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _<br>
0&, hNewKey, lRetVal)<br>
<br>
RegCloseKey (hNewKey)<br>
<br>
End Sub<br>
Public Sub SetKeyValue( _<br>
ByVal lpParentKey As Long, _<br>
sKeyName As String, _<br>
sValueName As String, _<br>
vValueSetting As Variant, lValueType As Long _<br>
)<br>
'*** Called By: Your code when you want to set a KeyValue<br>
'*** Description: Opens the key you want to set, calls the wrapper<br>
'*** function SetValueEx, and closes key.<br>
'*** ADD ERROR HANDLING!!<br>
<br>
Dim lRetVal As Long 'result of the SetValueEx function<br>
Dim hKey As Long 'handle of open key<br>
<br>
'open the specified key<br>
lRetVal = RegOpenKeyEx(lpParentKey, sKeyName, 0, _<br>
KEY_ALL_ACCESS, hKey)<br>
lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)<br>
RegCloseKey (hKey)<br>
End Sub<br>
Public Function QueryValue( _<br>
ByVal lpParentKey As Long, _<br>
sKeyName As String, _<br>
sValueName As String _<br>
) As Variant<br>
<br>
'*** Called By: Your code when you want to set a read a KeyValue<br>
'*** Description: Opens the key you want to set, calls the wrapper<br>
'*** function QueryValueEx, closes key.<br>
'*** Return Value: The value you are querying<br>
'*** ADD ERROR HANDLING!!<br>
<br>
Dim lRetVal As Long 'result of the API functions<br>
Dim hKey As Long 'handle of opened key<br>
Dim vValue As Variant 'setting of queried value<br>
<br>
lRetVal = RegOpenKeyEx(lpParentKey, sKeyName, 0, _<br>
KEY_ALL_ACCESS, hKey)<br>
lRetVal = QueryValueEx(hKey, sValueName, vValue)<br>
RegCloseKey (hKey)<br>
QueryValue = vValue<br>
End Function<br>
<br>
Sub Main()<br>
'This is the main procedure. This is where the appliation<br>
'starts.<br>
<br>
CreateKeyDriver<br>
Debug.Print &quot;Created key Cosmic Example and TestVals&quot;<br>
<br>
SetStringValueDriver<br>
Debug.Print &quot;Created the TestStringVal&quot;<br>
<br>
SetNumberValueDriver<br>
Debug.Print &quot;Created the TestNumVal&quot;<br>
<br>
ReadValueDriver<br>
<br>
End<br>
End Sub<br>
<br>
Sub CreateKeyDriver()<br>
'*** Calls the CreateNewKey procedure<br>
'*** Description: Creates Cosmic Example key and TestVals subkey under<br>
'*** HKEY_CURRENT_USER\Software\VB and VBA Program Settings<br>
'*** If Software and/or VB and VBA Program Settings do not<br>
'*** exist, they are created.<br>
'*** Usage: Use this as an example of how you would use the CreateNewKey<br>
'*** procedure.<br>
<br>
Dim sNewKey As String<br>
Dim lPredefinedKeyValue As Long<br>
<br>
sNewKey = &quot;Software\VB and VBA Program Settings\Cosmic Example\TestVals&quot;<br>
lPredefinedKeyValue = HKEY_CURRENT_USER<br>
<br>
CreateNewKey sNewKey, lPredefinedKeyValue<br>
End Sub<br>
<br>
Sub SetStringValueDriver()<br>
'*** Calls the SetKeyValue procedure<br>
'*** Description: Sets the value TestStringVal under the<br>
'*** Cosmic Example\TestVals key and sets it to<br>
'*** VB App Created. If it doesn't exist, it creates it.<br>
'*** Usage: Use this as an example of how you would use the SetKeyValue<br>
'*** procedure.<br>
<br>
Dim sKey As String '***Key under which to create the value<br>
Dim sValue As String '***Value name to set<br>
Dim vSetting As Variant '***What to set the Value to<br>
Dim sType As Long '***Value type -- string or number<br>
<br>
sKey = &quot;Software\VB and VBA Program Settings\Cosmic Example\TestVals&quot;<br>
sValue = &quot;TestStringVal&quot;<br>
vSetting = &quot;VB App Created&quot;<br>
sType = REG_SZ<br>
<br>
SetKeyValue HKEY_CURRENT_USER, sKey, sValue, vSetting, sType<br>
<br>
End Sub<br>
Sub SetNumberValueDriver()<br>
'*** Calls the SetKeyValue procedure<br>
'*** Description: Sets the value TestNumVal under the<br>
'*** Cosmic Example\TestVals key and sets it to 5.<br>
'*** If it doesn't exist, it creates it.<br>
'*** Usage: Use this as an example of how you would use the SetKeyValue<br>
'*** procedure.<br>
<br>
Dim sKey As String '***Key under which to create the value<br>
Dim sValue As String '***Value name to set<br>
Dim vSetting As Variant '***Wht to set the Value to<br>
Dim sType As Long '***Value type -- string or number<br>
<br>
sKey = &quot;Software\VB and VBA Program Settings\Cosmic Example\TestVals&quot;<br>
sValue = &quot;TestNumVal&quot;<br>
vSetting = 5<br>
sType = REG_DWORD<br>
<br>
SetKeyValue HKEY_CURRENT_USER, sKey, sValue, vSetting, sType<br>
<br>
End Sub<br>
Sub ReadValueDriver()<br>
'*** Calls the QueryValue function<br>
'*** Description: Reads the value TestNumVal and TestStringVal under<br>
'*** the Cosmic Example\TestVals key.<br>
'*** Usage: Use this as an example of how you would use the QueryValue<br>
'*** procedure.<br>
<br>
Dim sKey As String '***Key under which to create the value<br>
Dim sValue As String '***Value name to set<br>
Dim vSetting As Variant<br>
<br>
sKey = &quot;Software\VB and VBA Program Settings\Cosmic Example\TestVals&quot;<br>
sValue = &quot;TestStringVal&quot;<br>
<br>
'***Read the String value<br>
vSetting = QueryValue(HKEY_CURRENT_USER, sKey, sValue)<br>
Debug.Print &quot;TestStringVal is &quot; & vSetting<br>
<br>
sValue = &quot;TestNumVal&quot;<br>
'***Read the number value<br>
vSetting = QueryValue(HKEY_CURRENT_USER, sKey, sValue)<br>
Debug.Print &quot;TestNumVal is &quot; & vSetting<br>
<br>
End Sub<br>
<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Try downloading the &quot;regobj.dll&quot; from the microsoft web site, it may not be as precise but works.
 
Sounds interesting but can't find it :-(<br>
<br>
Do you know exactly where it is?<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top