WAD WMIQUERY

#1
Did you miss this one? In a thread of a couple weeks ago there's a possible explanation.

If I ask for exactly the number of items available, I get them. If I ask for more than is available, I don't get an error message for the first failure. Starting at index 0 doesn't help (and causes the first item to be returned twice).
Code:
v:\> do i=1 to 7 ( echo %i & wmiquery . "SELECT StartTime from Win32_logonsession" %i )
1
20140611115217.443054-240
2
20140719191920.059415-240
3
20140715105813.866117-240
4
20140611115217.427453-240
5
6
Error (SYS): Incorrect function.
7
Error (SYS): Incorrect function.
 

rconn

Administrator
Staff member
May 14, 2008
10,533
94
#2
It's not a TCC bug.

Not sure whether to characterize this as a WMI bug or feature. TCC queries WMI, gets an S_OK result, then calls Skip to get the next entry. That also succeeds (i.e., WMI says it's a valid entry), so TCC asks for the data and WMI returns ... nothing. (But no error, either.) TCC dutifully displays nothing, then when you try to skip to the next entry WMI returns an error, which TCC displays.

It would be a lot easier (and actually work) if you used the /A option for WMIQUERY.
 
#3
It's not a TCC bug.

Not sure whether to characterize this as a WMI bug or feature. TCC queries WMI, gets an S_OK result, then calls Skip to get the next entry. That also succeeds (i.e., WMI says it's a valid entry), so TCC asks for the data and WMI returns ... nothing. (But no error, either.) TCC dutifully displays nothing, then when you try to skip to the next entry WMI returns an error, which TCC displays.

It would be a lot easier (and actually work) if you used the /A option for WMIQUERY.
I don't know about "S_OK", but there are other COM HRESULTs (applicable here) which (being >= 0) satisfy SUCCEEDED() and don't satisfy FAILED(), namely
WBEM_S_FALSE (1) ... no more objects available; number returned less than requested
WBEM_S_NO_MORE_DATA (262149) ... no more data available from an enumeration
 

rconn

Administrator
Staff member
May 14, 2008
10,533
94
#4
I don't know about "S_OK", but there are other COM HRESULTs (applicable here) which (being >= 0) satisfy SUCCEEDED() and don't satisfy FAILED(), namely
WBEM_S_FALSE (1) ... no more objects available; number returned less than requested
WBEM_S_NO_MORE_DATA (262149) ... no more data available from an enumeration
That's all true, but WMI isn't returning any of those errors.
 
#6
This is a bit kludgy (a slight redesign would be more intellectually satisfying). I added testing dwReturned to my version of WMIQUERY and it does catch the condition.
Code:
    if ( index > 1 )
       hr = pEnum->Skip(5000, index - 1);

     if ( hr == WBEM_S_NO_ERROR )
     {
       /* print queried properties of one or all class instances */
       while ( (hr=pEnum->Next(5000, 1, &pWbemObject, &dwReturned)) == WBEM_S_NO_ERROR || dwReturned == 0 )
       {
         if ( dwReturned == 0 )
         {
           Qprintf(STD_ERR, L"Error (TCC) Index out of range: %lu\r\n", index );
           break;
         }
// SNIP
Code:
l:\projects\4sysutils\release> wmiquery . "SELECT * from Win32_logonsession" 2
AuthenticationPackage = NTLM
LogonId = 307528
LogonType = 2
StartTime = 20140902234359.525132-240

l:\projects\4sysutils\release> wmiquery . "SELECT * from Win32_logonsession" 3
Error (TCC) Index out of range: 3