Windows 10 x64 machines (tested on two different machines)
This may have been reported/discussed before but I couldn't find it.
I needed a function that derives the installation path (bin folder) for AutoCAD from Windows registry.
To get the install path is a bit cumbersome because one has to start in HKEY_CURRENT_USER and look up some values, to later continue in HKEY_LOCAL_MACHINE.
And it was when using KeyExists and OpenKey in the HKEY_LOCAL_MACHINE branch I run into trouble, as both of those functions always returned False although the registry keys clearly existed and the pfAccessRights had been set to KEY_READ.
Note that if the keys to search for is under the Wow6432Node it works just fine. But otherwise it does not. And the problem is that the pfAccessRights = KEY_READ is not enough under HKEY_LOCAL_MACHINE!
After some reading of Microsoft documentation I found and declared:
Define KEY_WOW64_64KEY for |CI$0100
For the function below, this is the Registry branch to search for AutoCAD:
HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\R23.1 \ACAD-3001:409
and the string value that holds the installaltion path is: AboutDirective
Thus the following function that uses a combination of KEY_READ ior KEY_WOW64_64KEY works. But try to remove the KEY_WOW64_64KEY and you will see it fail.
Code:
// Returns the install path for the AutoCAD program.
// The returned path will _not_ contain any trailing "\" character.
// Note: The path often contains spaces (" "), so make sure to surround
// expressions with the returned string with an extra pair of quotes ("").
Function AutoCADInstallPath Global Returns String
String sInstallPath sKey sValue sVersion1 sVersion2
Boolean bExists bOpened bX64
Handle hoRegistry
Move "" to sInstallPath
Get Create (RefClass(cRegistry)) to hoRegistry
Set phRootKey of hoRegistry to HKEY_CURRENT_USER
Set pfAccessRights of hoRegistry to KEY_READ
Move "Software\Autodesk\AutoCAD" to sKey
Get KeyExists of hoRegistry sKey to bExists
If bExists Begin
Get OpenKey of hoRegistry sKey to bOpened
If (bOpened = True) Begin
Get ReadString of hoRegistry "CurVer" to sVersion1
End
Else Begin
Send Destroy of hoRegistry
Function_Return ""
End
Send CloseKey of hoRegistry
Move (sKey + "\" + sVersion1) to sKey
Get OpenKey of hoRegistry sKey to bOpened
If (bOpened = True) Begin
Move "CurVer" to sValue
Get ValueExists of hoRegistry sValue to bExists
If (bExists = True) Begin
Get ReadString of hoRegistry sValue to sVersion2
Move (sKey + "\" + sVersion2) to sKey
End
End
Else Begin
Send Destroy of hoRegistry
Function_Return ""
End
Send CloseKey of hoRegistry
Set phRootKey of hoRegistry to HKEY_LOCAL_MACHINE
// Note: It is absolutely crucial to use: (KEY_READ ior KEY_WOW64_64KEY),
// else every KeyExists and OpenKey will return False (!).
Set pfAccessRights of hoRegistry to (KEY_READ ior KEY_WOW64_64KEY)
Get KeyExists of hoRegistry sKey to bExists
If (bExists = True) Begin
Get OpenKey of hoRegistry sKey to bOpened
If (bOpened = True) Begin
Move "AcadLocation" to sValue
Get ValueExists of hoRegistry sValue to bExists
If (bExists = True) Begin
Get ReadString of hoRegistry sValue to sInstallPath
End
End
End
Else Begin
Send Destroy of hoRegistry
Function_Return ""
End
End
Send Destroy of hoRegistry
If (Right(sInstallPath, 1) = "\") Begin
Move (Left(sInstallPath, Length(sInstallPath) - 1)) to sInstallPath
End
Function_Return sInstallPath
End_Function
Hope this will help somebody else tripping over the same problem.