Jump to content

Help! WMI query gets "generic error" on a 64-bit system, but works fine on 32 bit system or in compatibility mode.

Recommended Posts

I'm trying to write an AutoIt script that detects when an Arduino device is plugged into the computer (and later, once I get the detection working, I will be sending Serial Port commands to it). I need the code to run identically on XP, Vista, and Win7, on both 32-bit and 64-bit flavors. I need the code to run natively, i.e., I can't resort to "compatibility mode".

Here is my code:

; Set up custom defined error handler for COM objects, incase we get a COM error.
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")    ; Initialize a COM error handler

$objWMI = ObjGet("winmgmts:\\.\root\cimv2")
If IsObj($objWMI) then
    ; Everything is fine.
; Everything is not fine. Make an error message.
   MsgBox(0, "Error", "Internal program error. Unable to get handle to WMI object.")
$objItems = $objWMI.ExecQuery("SELECT * from WIN32_SerialPort WHERE Caption LIKE '%Arduino%'","WQL",0x10+0x20)

If IsObj($objItems) Then
 For $objItem In $objItems
MsgBox(0, "Located Arudino", "Found Arduino at: " & $objItem.DeviceID )

 MsgBox(0, "Did not locate Arudino", "Did not locate Arduino")

; -------------------------------------------------------
; -------------------------------------------------------
Func MyErrFunc()
  Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"    & @CRLF  & @CRLF & _
             "err.description is: " & @TAB & $oMyError.description  & @CRLF & _
             "err.windescription:"   & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "       & @TAB & hex($oMyError.number,8)  & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "   & @TAB & $oMyError.scriptline   & @CRLF & _
             "err.source is: "       & @TAB & $oMyError.source       & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile     & @CRLF & _
             "err.helpcontext is: " & @TAB & $oMyError.helpcontext _

The code runs fine on a 32-bit Windows7 computer.

When I try to run it on a 64-bit Windows7 computer, here are the behaviors:

- Sometimes I might get "The requested action with this object has failed." without the code even hitting my error handler.

- Sometimes I might hit the error handler, which returns a "Generic Error" of 80020009.

I tried the following things:

- If I compile the script into an EXE, the EXE gets either one of those two same errors.

- If I make a shortcut to that EXE, and set the shortcut properties to run in Compatibility Mode for Windows XP, then it works fine.

I am concerned about:

- The fact that it says "Win32" in the SELECT statement. I am worried that maybe that's the wrong SELECT statement for 64 bit systems? I don't know.

- That SELECT statement works fine on a 64 bit system if I put it into a WMIC command line batch file. So maybe that statement is actually fine, and the "Win32" is actually just fine?

- Googling is not helping, I can't seem to find out how to properly word that SELECT statement for a 64-bit system. So maybe that's a red herring.

Can anyone else please reproduce this problem, using the above example code? (Remember: must be on 64-bit system to repro)

Can anyone please help me find the right way to write this code so that it runs natively on Win7-64?

Link to post
Share on other sites

Updated information:

When I reported the issue, I was running my script from within the AutoIt Script Editor with the F5 key. When I said that I tried compiling it, I was compiling the script from within the AutoIt script editor using the Tools/Compile function. I got the errors in those cases.

I just discoverd that, If I use the external "compile script to EXE" utility, and, if I deliberately use the X86 version, and deliberately not check the "64 bit" checkbox, then it creates a Win32 executable that runs correctly.

HOWEVER: This means that I cannot interactively develop and debug my script from within the script editor. (i.e., I have to compile it every time, run the compiled version, find its errors, then go back to the script editor).

So does anyone know if there is a way I can make this run from withing the AutoIt Script Editor so that I can develop and debug more easily?

Link to post
Share on other sites

If you go into the Tools menu and select Compile, and you unselect the x64 and select x86, and then use the Save Only button, it will write the wrapper directive to the script, and then hitting F5 will run it in x86 mode, without having to compile it first.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to post
Share on other sites

Okay, that seems to have done it, but, there is one slight modification to the instructions: Selecting "Tools/Compile" gives me no checkboxes to unselect. I have to run the separate compile application and make sure x64 is unselected. But after that, yes, it seems to behave as you say.

Problem solved! Thank you!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...