473,488 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Check trusted path in registry

tuxalot
200 New Member
I have a function tied to my main forms on open event that adds the current db path as a trusted location to avoid the macro warnings each time the db is opened:
Expand|Select|Wrap|Line Numbers
  1. Function MakeTrustedLocation()
  2. On Error GoTo ET
  3.  
  4.         Dim strFile As String, strLocation As String, strKey As String, strSql As String, strMsg As String
  5.  
  6.         strKey = "[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Access\Security\Trusted Locations\MyTrustedPath]"
  7.  
  8.         strLocation = Application.CurrentProject.Path & "\"
  9.         strLocation = Replace(strLocation, "\", "\\")
  10.         strFile = "C:\TEMP\TrustMyApplication.reg"
  11.         Open strFile For Output As #1
  12.         Print #1, "Windows Registry Editor Version 5.00"
  13.         Print #1, ""
  14.         Print #1, strKey
  15.         Print #1, """Path""=""" & strLocation & """"
  16.         Print #1, """AllowSubfolders""=dword:00000001"
  17.         Close #1
  18.         Shell ("regedit /s " & strFile)
  19.         Kill (strFile)
  20.         Exit Function
  21. ET:
  22.         strMsg = Err.Number & " - " & Err.Description
  23.         Debug.Print strMsg
  24.  
  25. End Function
  26.  
No need to execute this code each time the main form opens. So I would like to check if the registry entry exists and is equal to the current db path. If so, then exit function.

Thanks for the look,

Tux
Mar 16 '09 #1
9 10229
DonRayner
489 Recognized Expert Contributor
Hey Tux;

There is a class module that you can download and add to your project, called cRegistry. This module will allow you to do just about whatever you want with the registry.

Instructions for it's use and a link to download are here.
Mar 17 '09 #2
tuxalot
200 New Member
Thanks Don. How does this class module integrate into Access VBA? In the meantime, here is my solution:
Expand|Select|Wrap|Line Numbers
  1. '--- reads the value for the registry key strChkKey.  if the key cannot be found, the return value is ""
  2.  
  3. Function RegKeyRead()
  4. 'On Error GoTo ET
  5.  
  6. Dim myWS As Object
  7. Dim RegKeyMe As String
  8. Dim strAppPath As String
  9. Dim strChkKey As String
  10.  
  11. '--- store the path of the current registry key in variable if key is present
  12. strChkKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Access\Security\Trusted Locations\HyattInjuryDb\Path"
  13.  
  14. '--- try to read the reg key
  15. On Error GoTo NewKey
  16. '--- use Windows scripting to read the registry key
  17. Set myWS = CreateObject("WScript.Shell")
  18. myWS.RegRead (strChkKey) ' if key does not exist, an error will result.  in this case, create a new key
  19.  
  20. '--- store the full path to our current project
  21. strAppPath = CurrentProject.Path & "\"
  22.  
  23. '--- read key from registry and store in variable RegKeyMe
  24.   RegKeyMe = myWS.RegRead(strChkKey)
  25.  
  26. '--- MsgBox RegKeyMe, , "RegKeyMe"
  27. '--- MsgBox strAppPath, , "strAppPath"
  28.  
  29. If RegKeyMe <> strAppPath Then
  30.  
  31. NewKey:
  32.  
  33. '--- MsgBox "need to add new reg key"
  34.  
  35.         Dim strFile As String, strLocation As String, strKey As String, strSql As String, strMsg As String
  36.  
  37.         strKey = "[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Access\Security\Trusted Locations\HyattInjuryDb]"
  38.  
  39.         strLocation = Application.CurrentProject.Path & "\"
  40.         strLocation = Replace(strLocation, "\", "\\")
  41.         strFile = "C:\TEMP\TrustMyApplication.reg"
  42.         Open strFile For Output As #1
  43.         Print #1, "Windows Registry Editor Version 5.00"
  44.         Print #1, ""
  45.         Print #1, strKey
  46.         Print #1, """Path""=""" & strLocation & """"
  47.         Print #1, """AllowSubfolders""=dword:00000001"
  48.         Close #1
  49.         Shell ("regedit /s " & strFile)
  50.         Kill (strFile)
  51.         Exit Function
  52.  
  53.     Else
  54.  
  55. '--- MsgBox "key is good to go"
  56.  
  57.     End If
  58.  
  59. ET:
  60.         strMsg = Err.Number & " - " & Err.Description
  61.         Debug.Print strMsg
  62.         Exit Function
  63.  
  64. End Function
  65.  
Thanks
Mar 17 '09 #3
DonRayner
489 Recognized Expert Contributor
Hey Tux,

On the page link that I posted you can copy the text from the module. In your access project create a new class module with the name cRegistry and paste the copied code into it.

The page has all the examples that you need on using the module.
Mar 17 '09 #4
ADezii
8,834 Recognized Expert Expert
Just subscribing, will return later.
Mar 17 '09 #5
ADezii
8,834 Recognized Expert Expert
Oh well, here I am again. You can use this code to specifically check and see if a Registry Key exists:
  1. First, the API Declaration:
    Expand|Select|Wrap|Line Numbers
    1. Public Declare Function RegOpenKeyEx _
    2. Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    3. (ByVal hKey As Long, ByVal lpSubKey As String, _
    4. ByVal ulOptions As Long, ByVal samDesired As Long, _
    5. phkResult As Long) As Long
  2. In your specific case:
    Expand|Select|Wrap|Line Numbers
    1. Dim hKey As Long
    2. Const conKEY_NAME = "Software\Microsoft\Ofvvfice\12.0\Access\Security\Trusted Locations\MyTrustedPath"
    3. Const conHKEY_CURRENT_USER = &H80000001
    4. Const READ_CONTROL = &H20000
    5. Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    6. Const KEY_QUERY_VALUE = &H1
    7. Const KEY_ENUMERATE_SUB_KEYS = &H8
    8. Const KEY_NOTIFY = &H10
    9. Const SYNCHRONIZE = &H100000
    10.  
    11. Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _
    12.                    KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And _
    13.                    (Not SYNCHRONIZE))
    14.  
    15. 'Attempt to Open the requested Key
    16. If RegOpenKeyEx(conHKEY_CURRENT_USER, conKEY_NAME, 0, KEY_READ, hKey) <> 0 Then
    17.   MsgBox "The Registry Key [" & conKEY_NAME & "] does not exist!"
    18. Else
    19.   MsgBox "The Registry Key [" & conKEY_NAME & "] does exist!"
    20. End If
Mar 17 '09 #6
tuxalot
200 New Member
Thanks again Adezii. I had a thought on this:

I'm only interested in knowing if the project is opening from a trusted location. If the current project path has not been added as a trusted location, the key -

Software\Microsoft\Office\12.0\Access\Security\Tru sted Locations\MyTrustedPath\

would not exist (...\MyTrustedPath is added by my function). However, if the end user has previously set the db path as trusted, then move the db, the directory 'MyTrustedPath' will exist but the Path key will not match the current project path.

So would I need to approach this differently, and perform (2) checks, one to check for the existence of -

Software\Microsoft\Office\12.0\Access\Security\Tru sted Locations\MyTrustedPath\

and next, to check if the reg path key

Software\Microsoft\Office\12.0\Access\Security\Tru sted Locations\MyTrustedPath\ Path

matches the current project path?

Thanks again,

Tux
Mar 17 '09 #7
ADezii
8,834 Recognized Expert Expert
@tuxalot
It would appear to me to be a 2-Step process, at least you should be able to isolate the problem should something go wrong.
Mar 17 '09 #8
tuxalot
200 New Member
Hi ADezii,

I'm getting an "invalid outside procedure" when compiling using this code. The error is at
Expand|Select|Wrap|Line Numbers
  1.     If RegOpenKeyEx(conHKEY_CURRENT_USER, conKEY_NAME, 0, KEY_READ, hKey) <> 0 Then
  2.  
Ideas?
Mar 18 '09 #9
ADezii
8,834 Recognized Expert Expert
@tuxalot
The code is sound, I need to see the entire code context (the Procedure in which the Registry Key is Opened), as well as the API Declaration and where it is Declared RegOpenKeyEX().
Mar 18 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6844
by: build | last post by:
G'day All, I'm using code below to check a path selected by a user. Can anyone pick holes in it please? The more critical you are the better. tia build 'GET SOURCE PATH...
7
14808
by: Bob | last post by:
I'm working on a Windows app that needs to write to the Registry HKLM. I keep getting a "System.UnauthorizedAccessException: Cannot write to the registry key." error when running the app. I'm...
2
2461
by: Dan Sikorsky | last post by:
Should application data be read and written to the Registry to persist state, or should the App.config file be used? If the Registry should be used, what .NET class reads and writes the...
3
3529
by: Yogi_Bear_79 | last post by:
Been trying to do an IF statement to check for a registry key. I understand to RegistryKey class in the Microsoft.Win32 namespace. But just can't get the syntax correct. What I want is to test for...
2
1959
by: zoneal | last post by:
my program it's just tools for my little home network.. Only thing I have left to do is reading the registy key to check the path to the shared folder.. this path is always updated so I will...
11
520
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. It is greatly appreciated. Is there a way to programmatically check for .NET Framework versions installed on the computer? -- Michael Bragg,...
0
3534
by: MIRRA | last post by:
Hi I have a UNIX script which creates a report. In that script I have to define the java classpath. Also I need to check if the java classpath specified exists or not. Below is the command i use ...
5
1417
jimpy
by: jimpy | last post by:
Greetings, When I open a terminal and enter :"echo $PATH", I receive this printout: I successfully add : But when I close the terminal, and later reopen and check the path, the...
1
2098
by: Floris Bruynooghe | last post by:
Hi We basically want the same as the OP in , i.e. when python starts up we don't want to load *any* sys.path entries from the registry, including subkeys of the PythonPath key. The result of...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.