Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WMI Win32_Process in VC ... how?

210 views
Skip to first unread message

Vincent Fatica

unread,
Feb 3, 2003, 9:37:11 PM2/3/03
to
I can't even get off the ground. I have .NET/VC7 and the WMI SDK
installed and I can't even find a declaration of Win32_Process.
Thanks.

- Vince

Wade Hasbrouck [MSFT]

unread,
Feb 3, 2003, 11:06:47 PM2/3/03
to
Not quite sure what you are trying to do, or what you are looking for... If
you can clarify that, I would be willing to try and give more specific help.

"Win32_Process" should be listed in the Platform SDK as well as the WMI
SDK... also listed at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
win32_process.asp

To interact with "Win32_Process", you do NOT delcare an object in your code
as type "Win32_Process". You use an IWbemServices (COM Interface) pointer
to retrieve a Win32_Process object from from the WMI repository, which to
interact with the object that is returned to is another COM object that you
use the IWbemClassObject interface. IWbemServices supports many different
methods that do Enumerations as well as getting single objects. There are
a few other things you need to do to get an IWbemServices pointer (i.e.
CoInitialize COM, CoCreate an IWbemLocator, and call
IWbemLocator::ConnectServer() )

The class definition you see in the documention, is a "MOF" representation
of the class, as the WMI repository is an Object Oriented Repository. MOFs
do look very much like C++ definitions, but they are not.

You can use WbemTest.EXE to view classes and instances that are in the WMI
repository... Win32_Process is located in the "root\cimv2" namespace.

--
Wade Hasbrouck
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Vincent Fatica" <vefa...@syr.edu> wrote in message
news:rl9u3vgtgr1jf8m2n...@4ax.com...

Vincent Fatica

unread,
Feb 3, 2003, 11:27:25 PM2/3/03
to
On Mon, 3 Feb 2003 20:06:47 -0800, "Wade Hasbrouck [MSFT]"
<wad...@online.microsoft.com> wrote:

>To interact with "Win32_Process", you do NOT delcare an object in your code
>as type "Win32_Process". You use an IWbemServices (COM Interface) pointer
>to retrieve a Win32_Process object from from the WMI repository, which to
>interact with the object that is returned to is another COM object that you
>use the IWbemClassObject interface.

Thanks, Wade. I could use more help. Ultimately I want to use
Win32_Process.Create to create a process on a remote machine (for
starters, I'll settle for the local machine). I have used the stuff
you mention below (plus CoInitializeSecurity) to dig some info out of
a Win32_OperatingSystem (both local and remote), but to me (++ not
being my forte) that was relatively straightforward. So, some detail
about what to do after getting a IWbemServices pointer would be
appreciated.

- Vince

Gwyn Cole

unread,
Feb 4, 2003, 7:15:08 AM2/4/03
to
Hello Vincent,

We have a sample chapter (a bit of ch4 and ch7 merged) on our web site
http://www.wbem.co.uk/.

Page 48 starts talking about "Making Method Calls"... Hopefully that will
answer some of your questions?

--
Gwyn Cole
co-author of "Developing WMI Solutions"


Vincent Fatica

unread,
Feb 4, 2003, 4:14:57 PM2/4/03
to
On Tue, 4 Feb 2003 12:15:08 -0000, "Gwyn Cole"
<gwyn...@btinternet.com> wrote:

>We have a sample chapter (a bit of ch4 and ch7 merged) on our web site
>http://www.wbem.co.uk/.

Thanks, Gwyn. That helps a lot. In fact I managed to create a
process, both on the local machine and on a remote machine. The code
snippet appears below; comments would be welcomed. I was at a loss
providing Win32_Process.Create's other two in-parameters,
("ProcessStartupInformation", and a "ProcessId" pointer). Some help
there would be appreciated. ... Vince

/* after initiializing COM and Security */
CComPtr<IWbemLocator> pLocator;
hr = CoCreateInstance(CLSID_WbemLocator,
NULL, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *)&pLocator);
if ( FAILED(hr) ) OutOfHere(1, hr);

CComPtr<IWbemServices> pServices;
hr = pLocator->ConnectServer(bstrRoot, NULL, NULL,
NULL, 0, NULL, NULL, &pServices);
if ( FAILED(hr) ) OutOfHere(3, hr);
SysFreeString(bstrRoot);

CComPtr<IWbemClassObject> pProcess;
hr = pServices->GetObject( CComBSTR("Win32_Process"),
WBEM_FLAG_RETURN_WBEM_COMPLETE, NULL, &pProcess, NULL);
if ( FAILED(hr) ) OutOfHere(4, hr);

CComPtr<IWbemClassObject> pInParamsDef;
hr = pProcess->GetMethod(_T("Create"), 0, &pInParamsDef, NULL);
if ( FAILED(hr) ) OutOfHere(5, hr);

CComPtr<IWbemClassObject> pInParams;
hr = pInParamsDef->SpawnInstance(0, &pInParams);
if ( FAILED(hr) ) OutOfHere(6, hr);

CComVariant Command("d:\\4nt\\4nt.exe");
hr = pInParams->Put(L"CommandLine", 0, &Command, 0);
if ( FAILED(hr) ) OutOfHere(7, hr);

CComVariant Cwd("e:\\Workplace");
hr = pInParams->Put(L"CurrentDirectory", 0, &Cwd, 0);
if ( FAILED(hr) ) OutOfHere(8, hr);

hr = pServices->ExecMethod(_T("Win32_Process"),_T("Create"),
0, NULL, pInParams, NULL, NULL);
if ( FAILED(hr) ) OutOfHere(9, hr);

Vincent Fatica

unread,
Feb 4, 2003, 8:15:21 PM2/4/03
to
On Tue, 04 Feb 2003 16:14:57 -0500, Vincent Fatica <vefa...@syr.edu>
wrote:

> I was at a loss
>providing Win32_Process.Create's other two in-parameters,
>("ProcessStartupInformation", and a "ProcessId" pointer).

I figured out that "ProcessId" was actually an out-param, and managed
to get Create's "ProcessStartupInformation" parameter set (as below).
It's a little long (setting only one startup option), and it would be
tedious to do the same for several members of the
"ProcessStartupInformation" object. Is there any way to abbreviate
code like that below? Thanks. ... Vince

CComPtr<IWbemClassObject> pStartInfo;
hr = pServices->GetObject(_T("Win32_ProcessStartup"),
WBEM_FLAG_RETURN_WBEM_COMPLETE, NULL, &pStartInfo, NULL);
if ( FAILED(hr) ) OutOfHere(25, hr);

CComPtr<IWbemClassObject> pSI;
hr = pStartInfo->SpawnInstance(0, &pSI);
if ( FAILED(hr) ) OutOfHere(26, hr);

CComVariant ShowCmd(SW_SHOWMINIMIZED);
pSI->Put(L"ShowWindow", 0, &ShowCmd, 0);
if ( FAILED(hr) ) OutOfHere(27, hr);

CComVariant SI(pSI);
hr = pInParams->Put(L"ProcessStartupInformation", 0, &SI, 0);
if ( FAILED(hr) ) OutOfHere(28, hr);

Wade Hasbrouck [MSFT]

unread,
Feb 4, 2003, 8:50:40 PM2/4/03
to
I don't know of a way to abbreviate it... that is about as short it gets...

It is the whole In and Out Parmeters that that make method execution fairly
long... When executing a method that has no in params and it has no out
params (or you don't care about them), Method execution can be simply
reduced to:

pServices->ExecMethod (bstrMyObjectPath, bstrMyMethod, 0, NULL,
NULL,&pMyOutParams, NULL);

If the Method doesn't require In or Out Params, then you can skip the whole,
"Get Class/Instance definition, GetMethod (), SpawnInstance (InParams), Set
In Params" stuff....


--
Wade Hasbrouck
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Vincent Fatica" <vefa...@syr.edu> wrote in message

news:5ro04vobp8p1oq5kd...@4ax.com...

Vincent Fatica

unread,
Feb 4, 2003, 9:37:13 PM2/4/03
to
On Tue, 4 Feb 2003 17:50:40 -0800, "Wade Hasbrouck [MSFT]"
<wad...@online.microsoft.com> wrote:

>I don't know of a way to abbreviate it... that is about as short it gets...

Thanks, Wade, Gwyn. It isn't so bad once you get used to it. :-) And
Win32_Process.Create doesn't require the ProcessStartupInformation in
general.

Another question ... about credentials. I'm using:

BSTR bstrUser = SysAllocString(L"foo");
BSTR bstrPass = SysAllocString(L"bar");
CComPtr<IWbemServices> pServices;
hr = pLocator->ConnectServer(bstrRoot, bstrUser, bstrPass,


NULL, 0, NULL, NULL, &pServices);

/* later */


hr = pServices->ExecMethod(_T("Win32_Process"),_T("Create"),

0, NULL, pInParams, &pOutParams, NULL);

By default, I am "vefatica", with identical credentials on both
machines. So if I use NULL for user/pass above, it works. If I
specify faulty credentials (in ConnectServer), it fails (access
denied). But if I specify the valid credentials of a different user,
it still works, but the remote machine's TaskMgr shows the process
owned by vefatica. Furthermore, if I logon locally as a (admin) user
with no credentials on the remote machine, and specify valid
credentials in ConnectServer, it fails. There doesn't seem to be any
other opportunity to provide credentials. So ... How do I get the
remote process to run as the remote user of my choice; and how do I
create it while I am a local user with no credentials on the remote
machine? There doesn't seem to be (I may have missed it) an
opportunity to specify credentials for the Win32_process itself, or
for the ExecMethod call. If it matters, I am not in a domain. Thanks
again.
... Vince

Vincent Fatica

unread,
Feb 4, 2003, 11:50:26 PM2/4/03
to
Ha! Got that one too (with CoSetSecurityBlanket). This is fun (when
it works). Thanks again, Wade and Gwyn.

- Vince

On Tue, 04 Feb 2003 21:37:13 -0500, Vincent Fatica <vefa...@syr.edu>
wrote:

>Another question ... about credentials. I'm using:

Gwyn Cole

unread,
Feb 5, 2003, 5:57:10 AM2/5/03
to
Vincent,

If you want to, you can try out the thin WMI client C++ classes on our web
site. Check the PDF document on Page 3. Here's a code snip-it calling a
method. It's quite a bit shorter...

CWbemMethod method(wmi, L"Win32_Share", L"Create");

method.SetInParam(L"Name", CComBSTR(L"Temp"));
method.SetInParam(L"Path", CComBSTR(L"C:\\temp"));
method.SetInParam(L"Type", int(0));

method.Execute();

int nCreateRetVal = method.GetReturnValue();


--
Gwyn Cole
co-author of "Developing WMI Solutions"

> It's a little long (setting only one startup option), and it would be

0 new messages