Click to See Complete Forum and Search --> : WMI Blues
newbee17
February 24th, 2005, 10:57 AM
WMI Blues
I am trying to set the NIC properties on a local machine using WMI. The code to do this is written in C++. I have a windows service and the code to set the NIC settings is handled in that service.
Problem:
I can get two of the properties always (does not matter which one like ip, default gateway or DNS) to set. However, on the third attempt to set any property, the connectServer () function fails (connect server function is required to get the namespace). I tried another experiment, when I restart my service after setting a property it will always work. The CoInitializeSecurity settings are exactly the same as the one in my MFC app. I am clueless at this point and I have a hunch that the code to set NIC settings residing in a service has got something to do with my program not running properly.
Note: I created a test MFC application that uses all my above functions and it works well all the time.
Any help will be appreciated
Thanks
Mick
February 24th, 2005, 11:11 AM
Please do not cross post your question to the forums.
What is the HRESULT returned from ConnectServer(...)
newbee17
February 28th, 2005, 09:49 AM
Hi Mick,
Sorry for the delayed response. I beleive my Exec Query fails.
HRESULT hr = pNamespace->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = 'Public'"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
The hr that I get from the above function is "The object invoked has disconnected from its clients.
I tried making a console application whose command line parameters would be the IP, subnet mask, default gateway, pref and alt DNS. This application as a stand alone works perfectly well. I then created another console based application to spawn the stand alone command line application . The spawner sends the console based applictaion the parameters. I see the same problem that is "The object invoked has disconnected from its clients". Could it be a security issue ? But both the applications are executables so I am really puzzled. Any help will be appreciated.
thanks
Mick
February 28th, 2005, 10:47 AM
What OS are you running and sp level.
Do I understand that if you mix up the sets you will always get two to succeed but the last will fail?
newbee17
February 28th, 2005, 11:42 AM
Hi Mick,
I am running Windows XP Professional SPI1.
The following is the order in which I call my functions
1) Set IP and Subnet mask
2) Set Default gateway
3) Set DNS
I always get the first one to succeed. The second function always returns "The object invoked has disconnected from its clients" as my HRESULT and the third one may or may not succeed. I tried changing the order in which I make my calls i.e: Set DNS first then IP and then gateway and it still returns me the "The object invoked has disconnected from its clients" in my second function call.
thanks
Mick
February 28th, 2005, 11:48 AM
Is the console app standalone enough that you could upload the source so I can take a look.
newbee17
February 28th, 2005, 11:57 AM
Here you go... I have uploaded both the app that spawns and the console app.
Thanks
Mick
March 1st, 2005, 07:40 AM
Not enough time in the day...
First let me comment on the code. If this was just you cranking out code for a test sample then ignore my comments [though the test sample would have been easier to write as a class]...otherwise.
You need to move the code into a class and implement RAII. You only need these methods:
CoInitializeEx
CoCreateInstance
ConnectServer
CoSetProxyBlanket
CoUninitialize
Called once for the lifetime of your object.
the 'goto's need to get gone...enuff said :)
Consider using CComPtr/CComBSTR.
Your problem steems from corrupt the stack/heap. You are freeing your safearrays and then calling VariantClear(...) which will again free your safearrays. You should also be calling VariantInit(...).
VARIANT manipulation API (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/automat/htm/chap7_0rn7.asp)
newbee17
March 1st, 2005, 11:26 AM
Thank you so much Mick... I will implement the changes recommended by you and let you know. Once again I really appreciate your help.
newbee17
March 1st, 2005, 11:33 AM
What does RAII stand for? Thanks
Mick
March 1st, 2005, 11:41 AM
What does RAII stand for? Thanks
resource acquisition is initialization. The idea is to represent a resource [your WMI interfaces etc] as an object and allow for the destructor to automatically cleanup the resource.
newbee17
March 2nd, 2005, 07:57 PM
Hi Mick,
Tahnk you very much for your help. i did all the changes you rcommended and it works like a charm. It is so much easier making this into a class. Once again I really appreciate your help.
Thanks
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.