Link to home
Start Free TrialLog in
Avatar of Kennedy
Kennedy

asked on

Reading the Registry from a (VB) COM dll

I am tring to read the Registry from a COM dll and having some problems.

The idea is as follows...

When I want to read a user defined registry variable I put the following line of code in my vb com dll.
myvar = GetSetting(appname:="myappname", section:="mysection", key:="myvarname", Default:="failure")

This key is located in HKCU/software/VB and VBA program settings/myappname/mysection/....

The package the dll is loaded into runs under the user (Identity tab in Package properties) which has the registry key set.

This works for me most of the time - However, the read from the registry fails after a server reboot.  The actual read from GetSetting fails,  I don't even get the "Default" string.  At that stage I have to do all sorts of thigs to get it working again.
Deleting the package and re-setting up sometimes works.  But as you can imagine this is not really good enough.

Has anybody seen this problem before and can offer advice or a solution?

I dont want to have to resort to ini files or hard coded variables.  The registry is a good solution (when it works!)



PS I Hope my question is clear - I hate unclear questions!


Avatar of newjack
newjack


This is a tip I got from ASP-today about reading registry values in ASP (and this should work in VB as well)

Maybe this helps:

Tip of the Day!

        By Srinivasa Sivakumar
        ================================================================
        How to read registry from ASP?

        With the help of Windows Scripting objects, RegRead
        method we can read information from the registry.

        <%
        Set objShell = CreateObject("WScript.Shell")
        Response.Write ?Registry Value: ? & objShell.RegRead

("RegistryPath")
        %>

        The Registry path should be delimited by a back-slash (\).
        The registry name should begin with the root. The valid root
        names are;

        HKCU - HKEY_CURRENT_USER
        HKLM - HKEY_LOCAL_MACHINE
        HKCR - HKEY_CLASSES_ROOT

listening and learning
You should not store any registry under HKCU. You should store in HKLM.

The reason the getsetting failed is when the machine is reboot and no user logged in, the HKCU doesn't exist.
ASKER CERTIFIED SOLUTION
Avatar of John844
John844

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kennedy

ASKER

newjack,  
I am currently pursuing your suggestion,  need to test few reboots first.

EDDYKT,
Even if the server is not logged in, HKCU should be available if the dll is run under a particular account (In the identity tab of the components package)

John844,
You're suggestion looks really impressive :)
But, I don't want to resort to it unless I really need to...
eddykt is right about not using HKEY_CURRENT_USER.  when the machine reboots, there is no current user.  if you put the item into HKEY_LOCAL_MACHINE it is always accessable.  
Avatar of Kennedy

ASKER

This solution works well enough for me to use.  I will not use newJacks solution because it does not seem to work from an Active X dll (Works from a VB exe though?)  I'm probably missing something here...

EDDYKT you were actually correct HKCU does not seem to be available directly after a reboot, however my dll did pull the data from the appropiate place when the server was logged out which is why I argued my point.  Anyway MSDN backed up what you said...

Thanks everyone for your help...