Announcement

Collapse
No announcement yet.

Need some code help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need some code help

    Scenario:
    I need to check parts of the registry for a installed file (MyApp.exe), if the registry entry exists, then get the exact Path of installation of MyApp.exe also from the registry.
    I have code that works perfectly. Here it is
    Code:
    --Check registry for an MyApp key is there or not (Does key Exist)
    
    X = Registry.DoesKeyExist(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\MyApp");
    
    if X then
    
    Install_Path = Registry.GetValue(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\MyApp", "Path", true);
    SessionVar.Set("%AppFolder%",Install_Path);
    
    else
    	
    end

    I released an build to customers that placed the registry entries of installed location in different parts of the registry, so some customers have the location of MyApp.exe installed location in one part of the registry and some customers have MyApp.exe installed location showing in a different registry entry.

    That means for my addon packs to work, I have to check registry in multiple locations and based on where the entry is, then set a Path Installed location also from registry.

    Here is my non-working code:

    Code:
    --Check registry for an MyApp key is there or not (Does key Exist)
    X = Registry.DoesKeyExist(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\MyApp");
    Y = Registry.DoesKeyExist(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\MyCompany\\MyApp");
    
    if X  then
    
    Install_Path = Registry.GetValue(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\MyApp", "Path", true);
    SessionVar.Set("%AppFolder%",Install_Path);
    
    else
    
    if Y  then
    
    Install_Path = Registry.GetValue(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\MyCompany\\MyApp", "Path", true);
    SessionVar.Set("%AppFolder%",Install_Path);
    
    else
    	
    end
    So if someone could help, that would be great!

    thanks
    DC

  • #2
    What error you are getting? Just looking at your code, I don't immediately see a syntax error in addition to the obvious missing end to close the first if.

    If you don't have code for an "else" branch, then there is no need to have it in the first place, so your code could be rewritten as this, with indentation making it more readable:

    Code:
    X [COLOR="#FF0000"]=[/COLOR] Registry[COLOR="#FF0000"].[/COLOR]DoesKeyExist(HKEY_LOCAL_MACHINE[COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Software[COLOR="#800080"]\\[/COLOR]Microsoft[COLOR="#800080"]\\[/COLOR]Windows[COLOR="#800080"]\\[/COLOR]CurrentVersion[COLOR="#800080"]\\[/COLOR]App Paths[COLOR="#800080"]\\[/COLOR]MyApp"[/COLOR])[COLOR="#FF0000"];[/COLOR]
    Y [COLOR="#FF0000"]=[/COLOR] Registry[COLOR="#FF0000"].[/COLOR]DoesKeyExist(HKEY_LOCAL_MACHINE[COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Software[COLOR="#800080"]\\[/COLOR]Microsoft[COLOR="#800080"]\\[/COLOR]MyCompany[COLOR="#800080"]\\[/COLOR]MyApp"[/COLOR])[COLOR="#FF0000"];[/COLOR]
    
    [COLOR="#0000FF"]if[/COLOR] X [COLOR="#0000FF"]then[/COLOR]
       Install_Path [COLOR="#FF0000"]=[/COLOR] Registry[COLOR="#FF0000"].[/COLOR]GetValue(HKEY_LOCAL_MACHINE[COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Software[COLOR="#800080"]\\[/COLOR]Microsoft[COLOR="#800080"]\\[/COLOR]Windows[COLOR="#800080"]\\[/COLOR]CurrentVersion[COLOR="#800080"]\\[/COLOR]App Paths[COLOR="#800080"]\\[/COLOR]MyApp"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Path"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#0000FF"]true[/COLOR])[COLOR="#FF0000"];[/COLOR]
       SessionVar[COLOR="#FF0000"].[/COLOR]Set([COLOR="#800080"]"%AppFolder%"[/COLOR][COLOR="#FF0000"],[/COLOR]Install_Path)[COLOR="#FF0000"];[/COLOR]
    [COLOR="#0000FF"]else[/COLOR]
       [COLOR="#0000FF"]if[/COLOR] Y [COLOR="#0000FF"]then[/COLOR]
          Install_Path [COLOR="#FF0000"]=[/COLOR] Registry[COLOR="#FF0000"].[/COLOR]GetValue(HKEY_LOCAL_MACHINE[COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Software[COLOR="#800080"]\\[/COLOR]Microsoft[COLOR="#800080"]\\[/COLOR]MyCompany[COLOR="#800080"]\\[/COLOR]MyApp"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Path"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#0000FF"]true[/COLOR])[COLOR="#FF0000"];[/COLOR]
          SessionVar[COLOR="#FF0000"].[/COLOR]Set([COLOR="#800080"]"%AppFolder%"[/COLOR][COLOR="#FF0000"],[/COLOR] Install_Path)[COLOR="#FF0000"];[/COLOR]
       [COLOR="#0000FF"]end[/COLOR]
    [COLOR="#0000FF"]end[/COLOR]
    Or better yet:

    Code:
    X [COLOR="#FF0000"]=[/COLOR] Registry[COLOR="#FF0000"].[/COLOR]DoesKeyExist(HKEY_LOCAL_MACHINE[COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Software[COLOR="#800080"]\\[/COLOR]Microsoft[COLOR="#800080"]\\[/COLOR]Windows[COLOR="#800080"]\\[/COLOR]CurrentVersion[COLOR="#800080"]\\[/COLOR]App Paths[COLOR="#800080"]\\[/COLOR]MyApp"[/COLOR])[COLOR="#FF0000"];[/COLOR]
    Y [COLOR="#FF0000"]=[/COLOR] Registry[COLOR="#FF0000"].[/COLOR]DoesKeyExist(HKEY_LOCAL_MACHINE[COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Software[COLOR="#800080"]\\[/COLOR]Microsoft[COLOR="#800080"]\\[/COLOR]MyCompany[COLOR="#800080"]\\[/COLOR]MyApp"[/COLOR])[COLOR="#FF0000"];[/COLOR]
    
    [COLOR="#0000FF"]if[/COLOR] X [COLOR="#0000FF"]then[/COLOR]
       Install_Path [COLOR="#FF0000"]=[/COLOR] Registry[COLOR="#FF0000"].[/COLOR]GetValue(HKEY_LOCAL_MACHINE[COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Software[COLOR="#800080"]\\[/COLOR]Microsoft[COLOR="#800080"]\\[/COLOR]Windows[COLOR="#800080"]\\[/COLOR]CurrentVersion[COLOR="#800080"]\\[/COLOR]App Paths[COLOR="#800080"]\\[/COLOR]MyApp"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Path"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#0000FF"]true[/COLOR])[COLOR="#FF0000"];[/COLOR]
       SessionVar[COLOR="#FF0000"].[/COLOR]Set([COLOR="#800080"]"%AppFolder%"[/COLOR][COLOR="#FF0000"],[/COLOR]Install_Path)[COLOR="#FF0000"];[/COLOR]
    [COLOR="#0000FF"]elseif[/COLOR] Y [COLOR="#0000FF"]then[/COLOR]
       Install_Path [COLOR="#FF0000"]=[/COLOR] Registry[COLOR="#FF0000"].[/COLOR]GetValue(HKEY_LOCAL_MACHINE[COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Software[COLOR="#800080"]\\[/COLOR]Microsoft[COLOR="#800080"]\\[/COLOR]MyCompany[COLOR="#800080"]\\[/COLOR]MyApp"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Path"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#0000FF"]true[/COLOR])[COLOR="#FF0000"];[/COLOR]
       SessionVar[COLOR="#FF0000"].[/COLOR]Set([COLOR="#800080"]"%AppFolder%"[/COLOR][COLOR="#FF0000"],[/COLOR] Install_Path)[COLOR="#FF0000"];[/COLOR]
    [COLOR="#0000FF"]end[/COLOR]
    So, if you are getting an error, please tell us what it is. If it just doesn't work as expected, you may want to review what you are actually doing and where you are looking for the registry keys. For example, "Software\\Microsoft\\MyCompany\\MyApp" looks very suspicous. I don't see why somebody would need to place a registry key for his own company under the key reserved to Microsoft. The logical location would be "Software\\MyCompany\\MyApp" instead.

    Ulrich

    Comment


    • #3
      Hello...
      Many thanks!
      your cleaned up code worked !

      thanks you

      PS: Due to the nature of a Public Forum, I placed it under Microsoft. thats not truly where it goes :lol

      thanks again
      DC

      Comment

      Working...
      X