RegQueryDWord and autocad

Started by naholt, September 27, 2016, 08:35:47 AM

Previous topic - Next topic

naholt

i am trying to read a dword key on a remote machine, its windows 7 64 bit. autocad 2016 is installed and I need to know if its a stand alone or network license.  this should be a snap but I am missing  something. here is the script:

:TOP
ComputerName = "RemoteComputer"

ComputerName = StrUpper(ComputerName)

LastError()
ErrorMode(@OFF)
  RegOpen = RegConnect(ComputerName, @REGMACHINE)

    If LastError() == "1503"
Message("Error", "Cannot connect to %ComputerName%")
Goto TOP
    EndIf

  RegKey = RegOpenKey(RegOpen, "SOFTWARE\Autodesk\AutoCAD\R21.0\ACAD-F001\AdLM")

  AcadType = RegQueryDWord(regkey, "[Type]",0)

message(regkey, AcadType)

this key is currently set to 2 but I get 0 as a return. what am I doing wrong?

thanks
Nicholas

td

Debugging a script with ErrorMode @Off is what you are doing wrong.  Your script assumes that RegConnect will only return one possible error message but that is not the case.  You need to comment out the ErrorMode business and see what the error actually is.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

snowsnowsnow

There are at least two serious programming errors in your code, which probably are hiding what's really going on in your use case.

QuoteErrorMode(@OFF)

This line is a serious error.  As Marty used to say, including this erroneous code generally turns a 15 minute debugging job into a 6 month job.

Quote
    If LastError() == "1503"
Message("Error", "Cannot connect to %ComputerName%")
Goto TOP
    EndIf

This is a stack management problem.  Eventually, this will error out with an error message like "Too many nested levels" or something like that.

Change it to:

If LastError() == "1503" THEN Message("Error", "Cannot connect to %ComputerName%")
THEN Goto TOP

Even this looks wrong, but I don't know enough about LastError() to say conclusively how to fix it.  Basically, it just looks wrong to be checking only for one possible return code and to ignore all the others.

td

Quote from: snowsnowsnow on September 27, 2016, 09:13:21 AM
This is a stack management problem.  Eventually, this will error out with an error message like "Too many nested levels" or something like that.

Change it to:

If LastError() == "1503" THEN Message("Error", "Cannot connect to %ComputerName%")
THEN Goto TOP

Even this looks wrong, but I don't know enough about LastError() to say conclusively how to fix it.  Basically, it just looks wrong to be checking only for one possible return code and to ignore all the others.

While you can get away with it sometimes, crossing a structure boundary (if/endif in this case) with a 'goto' is, as you say, problematic.  Since LastError returns 0 when no error exists and clears the existing error when one does exist, testing for a non-zero return value is one approach. 

Code (winbatch) Select
If LastError() then Pause("Error", "Cannot connect to %ComputerName%")
then Goto TOP     


Since newer versions of retail Windows default to not running the remote registry service, the script may be an exercise in index finger fatigue.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Should also mention that RegConnect may return error 1502 when the remote registry service is not running on the targeted computer.  However, getting  or not getting that error does not guarantee the state of the remote registry service because RegConnect relies on an  error return value of a win32 functions to set the WIL error value.  Win32 function return values are not always reliable so the remote system would need to be checked to confirm or eliminate the state of the remote service as the problem.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade