Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Done When WMIQUERY finds no instances ...

May
13,584
201
Could it be made more friendly?

Compare these.

Code:
v:\> wmic process where name='notepad.exe' get ProcessID
No Instance(s) Available.

v:\> wmiquery /L /a . "Select ProcessId from Win32_Process where Name='notepad.exe'"
Error (SYS): Incorrect function.
 
Note that @WMI handles it better.

Code:
v:\> echo %@wmi[.,"select processid from win32_process where name='notepad.exe'"]
ECHO is OFF
 
Doesn't anything tell you that no handle/data/? (whatever you asked for) was returned?
 
Hmmm! I found some code from 2006, when I (and you, IIRC) first looked at WMI stuff. With a bit of effort, I included it (as WMIQ) in a plugin. It handles no_class_instances by just doing nothing.

Code:
v:\> wmiq . "Select ProcessId from Win32_process where name='tcc.exe'"
9924

v:\> wmiq . "Select ProcessId from Win32_process where name='garbage'"

v:\>

Here's some of the code (with an added test). When no class instance exists, no error is generated, the body of the "while" loop isn't executed (I don't see the test message), and there's no output. It must be that pEnum->Next did not return WBEM_S_NO_ERROR. Where does your "invalid query" error come from?

Code:
        else
        {
            hr = pNameSpace->ExecQuery(L"WQL", bstrQuery, WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum);
            if ( FAILED(hr) ) throw (DWORD) 5;

            hr = WBEM_S_NO_ERROR;
            if ( index > 1 ) hr = pEnum->Skip(WBEM_INFINITE, index - 1);

            if ( hr == WBEM_S_NO_ERROR )
            {
                /* print queried properties of one or all class instances */
                while ( pEnum->Next(WBEM_INFINITE, 1, &pWbemObject, &dwReturned)
                    == WBEM_S_NO_ERROR )
                {
                    Printf(L"Test: OK\r\n"); // added
                    if ( bAllInstances && bHeaders ) Printf(L"CLASS INSTANCE %lu\r\n", index++); /* numbered header */
                    PrintProperties(pWbemObject);  // this does the output
                    if ( bAllInstances && bSeparatorLine ) Printf(L"\r\n"); /* separator line */
                    if ( !bAllInstances ) break;
                }
            }
            else throw (DWORD) 6;
        }
 
That ExecQuery docs say (of pEnum) (don't know if they did 18 years ago) ...

If no error occurs, this receives the enumerator that allows the caller to retrieve the instances in the result set of the query. It is not an error for the query to have a result set with 0 instances. This is determined only by attempting to iterate through the instances.
 
The WMIQUERY code has become much more complex over the last 18 years, to handle various WMI bugs, error returns, error returns that look like success, and success that looks like error returns. In the case of Skip, it does not return WBEM_S_NO_ERROR, it returns S_FALSE. That return is handled by a bunch of additional code to test for some specific WMI bugs and to massage the result accordingly. I will have to add some more code to handle this specific error / not error.
 
Back
Top