WshShellL.RegRead if name or path does not exists

beno.pulverbeno.pulver Member Posts: 31
Hello everybody,

I would like to check, if a software is installed at a client/PC. To do so, I try to read the registry as follows: WshShellL.RegRead(Key).
The key looks like that: 'HKEY_LOCAL_MACHINE\SOFTWARE\A.I.S. GmbH\windream\'
It works perfect as long as the software is existing and the registry entry has been found. But if the software is not installed, I get the error message "Invalid root...".

Is there any possibility to get a return code and to avoid an error, if the registry entry is not found?
I haven't found an answer so far in the forum (and I did a lot of searches...).

Thank you very much, Beno.

Answers

  • micheldoggermicheldogger Member Posts: 21
    Hi Beno,

    I think I understand what you mean because I ran into the same problem.
    If the software isn't installed on that computer and you try to read the
    key from the registry (and the key doesn't exist) you will get an error.

    Solution:

    I've made the following solution. I've created a codeunit and added some methods to it
    wich helps me to read the registry and avoiding an error if it can't read the key. Here's
    some info to make it more clear for you.

    I've used a codeunit because if you call a codeunit within a IF ... THEN statement
    you will not see any errors that might occur. e.g.

    IF MyCodeunit.RUN THEN; You won't see any errors.
    MyCodeunit.RUN; You will be noticed on any errors.


    Beno, I've noticed that you only want to read a path: HKEY_LOCAL_MACHINE\SOFTWARE\A.I.S. GmbH\windream\
    and not asking a key or value from that path. I my code I want to read a path + key from the registry. So you might
    have to make some modifications to the code below.


    Step 1: Create a new codeunit or use an existing.

    Codeunit: 50000 - MyCodeunit.

    Step 2: Create a method in the codeunit wich will read the registry for you:

    fReadRegistry(pTxtKey : Text[100]) Retval : Text[250]
    CREATE(lAutWSH);
    Retval := lAutWSH.RegRead('HKEY_LOCAL_MACHINE\SOFTWARE\A.I.S. GmbH\windream\' + pTxtKey);
    CLEAR(lAutWSH);
    EXIT(Retval);
    

    Step 3: Then create a method in the codeunit wich an be called from outside the codeunit.
    This method is used to tell the codeunit wich key it has to read from the registry. The value
    in the parameter is assigned to a Global Variable wich you'll have to define gTxtKey Text[100]


    fSetReadRegistry(pTxtKey : Text[100])
    gTxtKey := pTxtKey;
    

    Step 4: Then create a method in the codeunit wich an be called from outside the codeunit.
    This method will return the value wich is in the Global Variable gTxtValue.

    fRetReadRegistry() : Text[250]
    EXIT(gTxtValue);
    

    Step 5: Then you have to add the following code to the OnRun trigger. Here the codeunit
    will call the method fReadRegistry and will assign the key from the registry into
    the Global Variable gTxtValue. It will be blank if the codeunit can't read the key and runs
    into an error.

    OnRun
    IF (gTxtKey = '') THEN   //USE THIS IF YOU WANT TO READ A KEY
      EXIT;
    
    gTxtValue := '';
    gTxtValue := fReadRegistry(gTxtKey)
    

    Step 6: The final step is to add a method into the codeunit wich you have call from outside the codeunit.
    And here comes the trick. Because the OnRun trigger cannot have any parameters or a return
    value the supporting methods, as mentioned above, are added to the codeunit. The method
    below will use this methods to set and read from the codeunit and it will call it's own codeunit
    with IF MyCodeunit.RUN THEN; to avoid any errors. This method will return the key.

    fReturnRegistryKey(pTxtKey : Text[100]) : Text[250]
    LocalVar: MyCodeunit Codeunit 50000.
    CLEAR(MyCodeunit);
    MyCodeunit.fReadRegistry(pTxtKey);
    IF MyCodeunit.RUN THEN
      EXIT(MyCodeunit.fRetReadRegistry)
    

    I hope this one helpes you,
    Michel
  • beno.pulverbeno.pulver Member Posts: 31
    Good morning Michel,

    great, this is exactly what I was looking for!!!!

    I already created something similar and it was working. But your solution is (unfortunately...) smarter.
    So I copied most of your code into mine.

    Thank you very much and have a nice, sunny day (at least it is like that in Switzerland), Beno
Sign In or Register to comment.