Solved

Using a variable filter in WQUERY for WMI

Posted on 2008-10-16
8
449 Views
Last Modified: 2013-11-27
How can you create in C++ a WQL that allows you to use a variable instead of a fixed expression as the filter in WMI
0
Comment
Question by:cotosl
  • 4
  • 4
8 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 22744768
I don't undertand the question:  With WMI, one uses a scripting language to, for instance, execute a SQL SELECT statement.  The statement that you pass may include string literals as well as text held in variables.  
Perhaps if you provide some more details about what you are trying to acccomplish, the question will be clarified.
0
 

Author Comment

by:cotosl
ID: 22747010
DanRollins:

Attached is an example of an ExecQuery(). What I want to know is how you can do an ExecQuery for WMI with a filter variable for the "Where" instead of the fixed 'anExecutableName.exe'. The idea is to be able to receive a list of processes and then use the ExecQuery() to return the process information for only the processes in the list.
hres = pSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t("SELECT * FROM Win32_Process Where Name = 'anExecutableName.exe'"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);

Open in new window

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 22750465
You could skip the WHERE clause and just use the enumerator to examine each process.  There is an example here:
   Example: Getting WMI Data from the Local Computer
   http://msdn.microsoft.com/en-us/library/aa390423.aspx
(though it uses the Win32_OperatingSystem table, the process is similar)
You can focus the result set by using inequality operators in the SQL query syntax, such as:
    ...WHERE Name < 'S"
http://msdn.microsoft.com/en-us/library/aa394054(VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa394605(VS.85).aspx
Perhaps the operator you really want is the LIKE operator which allows you to match character patterns.  For instance:

    ...WHERE Name LIKE "%ABC%"
Which will return process that contain th letters ABC anywhere in the name.  See
   LIKE Operator
   http://msdn.microsoft.com/en-us/library/aa392263(VS.85).aspx
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:cotosl
ID: 22751244
The solutions proposed are valid but from them the one closer to what I was looking for is the one using the iterator. What I dont like about it is going over all results when they can be filtered from the very begining. Maybe the question should be how to construct the query using a variable outside of _bstr_t()and then plug it in the _bstr-t().
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 150 total points
ID: 22751437
It is possible to build the SELECT statement by concatening strings. For instance:
CString sStmt;
sStmt="SELECT * FROM Win32_Process Where Name LIKE '%";
sStmt += sVarToMatch; // eg, ABC or .EXE
sStmt += "%' ";
hres = pSvc->ExecQuery( bstr_t("WQL"),
bstr_t( sStmt ),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL, &pEnumerator
);
If that does not help you, then please provide some examples of what variables you would like to plug into the SELECT statement, and I'll show you how to do it.
0
 

Author Comment

by:cotosl
ID: 22753082
One thing I noticed was that if you try to initialize the variable when you define it the compile fails. But if you define it and then you set its value it will work (see code).  Do you have any idea why this happen.



// This will generate an error
CString sVarToMatch = "ABC" 
 
// This will work fine
CString sVarToMatch;
aVarToMatch = "ABC";

Open in new window

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 22753735
It's a quirk of the C++ programming language that each statement must be terminated with a semicolon (;).
0
 

Author Comment

by:cotosl
ID: 22753810
Sorry for the mistake. The error with the semicolon was only on the message. The actual code does have the semicolon and you get the error.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
no display on laptop 5 83
Add values of each row in an array 3 64
cloning computer 13 95
reboot server with scheduled time and week base 4 36
Step by step guide to Clean and Sort your windows registry! Introduction: Always remember: A Clean registry = Better performance = Save your invaluable time In this article we're going to clear our registry manually! Yes, manually! The e…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question