|
Thanks Aamir,
I think I´ve found the problem. Now it works, but I don´t know if it´s correct. I´m going to explain myself again:
I extract the information in 5 steps:
Step 1: COM initialization
Step 2: Set COM Security level
Step 3: Create IWbemLocator interface
Step 4: Create IWbemServices interface using this Locator (ConnectServer)
Step 5: Set Proxy Security
Extract the desired information using one of the available methods (ExecQuery, GetObject ...)
I want to read information from root/default, root/CIMv2 and root/CIMv2/Applications/MicrosoftIE namespaces. It´s necessary to use the ConnectServer method for every namespace. At this point I got the error I refered in my first post.
To solve this I have tried the following :
When you want to change to another namespace you only have to repeat two steps, 4 and 5:
Step 4:(IWbemLocator::ConnectServer)
hres = pLoc->ConnectServer(bstrNamespace,
bstrUser,
bstrPasswd,
NULL,
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices
);
Step 5: ( IWbemLocator::CoSetProxyBlanket)
Both using the same IWbemLocator* interface created previously.
The problem appears in the step 4 (ConnectServer). You can´t send the bstrUser/bstrPasswd again. It´s necesary for the first time (root/default), but the second (root/cimv2) and subsequent you have to change these parameters to NULL/NULL:
hres = pLoc->ConnectServer(bstrNamespace,
NULL,
NULL,
NULL,
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices
);
It works fine in this way.
Do you know a correct way to do this?
Thanks in advance for your time
AngelM
|
|
|
|
|
Hi I am using VC6 with WMISDK installed. However, when I am trying to compile the program, it happends to show the following error message
Using WMIDlg.cpp
C:\Documents and Settings\tikcire\Desktop\WMI\Using_WMI\Using WMIDlg.cpp(184) : error C2065: 'CoInitializeSecurity' : undeclared identifier
C:\Documents and Settings\tikcire\Desktop\WMI\Using_WMI\Using WMIDlg.cpp(191) : error C2065: 'EOAC_NONE' : undeclared identifier
Error executing cl.exe.
I know CoInitializeSecurity is being defined at ObjBase.h, which is already included in SDK\include folder. Do you guys know why is that? And what's wrong with my project setting?
Thanks.
tikcireviva
|
|
|
|
|
Have u included wbemuuid.lib in linking libs. I dont know whether WMISDK puts default paths for these libs in the VC environment or not. So, please check it.
Regards,
Aamir
|
|
|
|
|
hi
hi
i have the same problem as the user above whom you replied to. i am using vc++6 and the required SDK.
i have other examples of code that have CoInitializeSecurity and i dont get any compile errors. in "using wmidlg.cpp" i have added two lines as below to try and resolve the problem but no luck. i cant see an option to add additional dependecies in vc++6 so below should do the same?
#define _WIN32_DCOM
#pragma comment(lib, "wbemuuid.lib")
please help
|
|
|
|
|
_WIN32_DCOM should be define in "Project Setting >> Preprocessor definitions"
wewqewqe
|
|
|
|
|
That sorted it out for me thanks xiaozhijian!
|
|
|
|
|
Thanks you so much...when i put _WIN32_DCOM to project setting it compiled with zero error zero warning...
|
|
|
|
|
use your code can not connect to remote machine(win2K),why? please help
wesley
|
|
|
|
|
You need rights to connect to the remote host. Give yourself enough rights and you will be through. e.g., Just try to put yourself in the Administrators group on the other machine and you will be able to browse that machine's WMI Info.
Regards,
Aamir
|
|
|
|
|
hi,
i am working with the code given by aamir butt.
i have vc6
and i have microsoft sdk xp sp2
i selected win32 console application that has mfc support.
My problem is while compiling the code,the following error is coming
Linking...
Wbemuuid.lib(wbemcli_i.obj) : fatal error LNK1103: debugging information corrupt; recompile module
Error executing link.exe.
i have written only a fraction of code given by butt
i only included the functions
- coinitialize
- coinitializesecurity and
- cocreateinstance
when i removed 'cocreateinstance' and compiled, it is not giving errors
i read the articles given here
and tried the first argument with 'CLSID_WbemLocator' and
'CLSID_WbemAdministrativeLocator'.
but no use
i need it for my final sem project
please help me.
following is my code
::CoInitialize(NULL);
if(CoInitializeSecurity( NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,0) != S_OK)
return -1;
IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;
BSTR bstrNamespace = (L"root\\cimv2");
HRESULT hRes = CoCreateInstance (CLSID_WbemLocator,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER , IID_IUnknown ,
( void ** ) & pIWbemLocator ) ;
|
|
|
|
|
Had the same problem last weekend.
Looks like the DDK having a problem.
I found an older version of the library which seems to work better:
24-Jun-04 15:37 32,100 WbemUuid.Lib
Sorry, but can't remember where I downloaded it from.
If you can't find that version, let me know (beecken@image.dk) and I'll send you a copy of my version.
--> Don't like this work around as I don't know what side effects it has.
Regards
Peter Beecken
|
|
|
|
|
I realize I'm responding to an OLD message, but apparently the problem has still not been fixed. Does anyone know of a fix for this? Does anyone know where an older, non-corrupted copy of this library may be obtained.
Thanks to all for any help or advice you can offer.
Jeff Nygren
|
|
|
|
|
Jeff Nygren:
I have fixed this problem by copy wbemuuid.lib from VC.net(VC7) as the first reply said. You can send me a E_mail (pilixuanke@gmail.com) to request this file if u can't get it by yourself.
|
|
|
|
|
i have vc++ 6.0 and sdk xp sp2
when i copied and run the code given by Aamir Butt,
i got the link error saying "object file is corrupted, recompile the code"
compilation is ok
but linking is the problem
any suggestions?
bye
visu
|
|
|
|
|
The following is a copy of my post in COM message board. Please, help ....
Has anyone been successful in executing the
Win32_OperatingSystem class method "Reboot" or "Shutdown" from a call to the ExecMethod routine in C++. This method works fine using VBScript. But in C++, the method returns an error in ExecMethod - WBEM_E_INVALID_METHOD_PARAMETERS.
Why ?
Here is my code :
...
HRESULT hr;
CComPtr<IWbemLocator> pLocator;
CComPtr<IWbemServices> pServices;
CComBSTR bstrPath = L"//";
bstrPath.AppendBSTR(bstrComputer);
bstrPath.Append(L"/root/cimv2");
CComBSTR bstrObject = L"Win32_OperatingSystem";
CComBSTR bstrMethod = L"Win32Shutdown";
CComBSTR bstrLanguage = SysAllocString(L"WQL");
CComBSTR bstrQuery = SysAllocString(L"select * from Win32_OperatingSystem where Primary=true");
CComBSTR bstrFlags = L"Flags";
CComBSTR bstrReserved = L"Reserved";
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
{
cout << "Failed in CoInitializeEx" << endl;
return hr;
}
hr = CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hr))
{
cout << "Failed in CoInitializeSecurity" << endl;
CoUninitialize();
return hr;
}
//get the initial namespace of the CIM Object Manager
hr = pLocator.CoCreateInstance(__uuidof(WbemLocator), 0, CLSCTX_INPROC_SERVER);
if (FAILED(hr)){
cout << "Failed in CoCreateInstance" << endl;
return hr;
}
//create an instance for the interface that connects WBEM services
hr = pLocator->ConnectServer(bstrPath, NULL, NULL, NULL, 0, NULL, NULL, &pServices);
if (FAILED(hr)){
cout << "Failed in ConnectServer" << endl;
return hr;
}
//authenticate to the service, using current credentials
hr = CoSetProxyBlanket(pServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
if (FAILED(hr)){
cout << "Failed in CoSetProxyBlanket" << endl;
return hr;
}
//query WMI for objects: get the Win32_OperatingSystem class using WQL
CComPtr<IEnumWbemClassObject> pEnumWbem;
hr = pServices->ExecQuery(bstrLanguage, bstrQuery, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumWbem);
if (FAILED(hr)){
cout << "Failed in ExecQuery" << endl;
return hr;
}
//for each returned object call Win32Shutdown
IWbemClassObject* pWbemObject;
DWORD nRetObjects;
pEnumWbem->Reset();
while (pEnumWbem->Next(WBEM_INFINITE, 1, &pWbemObject, &nRetObjects) == S_OK)
{
CComPtr<IWbemClassObject> pClass;
CComPtr<IWbemClassObject> pMethod;
CComPtr<IWbemClassObject> pInParams;
CComPtr<IWbemClassObject> pOutParams;
//get the class definition of Win32_OperatingSystem
hr = pServices->GetObject(bstrObject, 0, NULL, &pClass, NULL);
if (FAILED(hr))
{
cout << "Failed in GetObject" << endl;
pWbemObject->Release();
return hr;
}
//get our method
hr = pClass->GetMethod(bstrMethod, 0, &pMethod, NULL);
if (FAILED(hr))
{
cout << "Failed in GetMethod" << endl;
pWbemObject->Release();
return hr;
}
//create a new object for in parameters
hr = pMethod->SpawnInstance(0, &pInParams);
if (FAILED(hr))
{
cout << "Failed in SpawnInstance" << endl;
pWbemObject->Release();
return hr;
}
//add parameters
CComVariant var;
V_VT(&var) = VT_I4;
V_I4(&var) = (short)nAction;
hr = pInParams->Put(bstrFlags, 0, &var, CIM_SINT32);
if (FAILED(hr))
{
cout << "Failed in Put" << endl;
pWbemObject->Release();
return hr;
}
//call Win32Shutdown with our action
hr = pServices->ExecMethod(bstrObject, bstrMethod, 0L, NULL, pInParams, &pOutParams, NULL);
if(hr == WBEM_E_INVALID_METHOD_PARAMETERS){
cout << "That's it" << endl;
}
if (FAILED(hr)){
cout << "Failed in ExecMethod" << endl;
pWbemObject->Release();
return hr;
}
pWbemObject->Release();
}
Code Red
|
|
|
|
|
???? mark
Any automation language, such as Visual Basic, VBScript, JScript, or Perl, can call a WMI method. Some languages can use direct access but others must use SWbemServices.ExecMethod to execute the provider method indirectly.
refer to MSDN ,I think
smart
|
|
|
|
|
Hi,
I have the same problem. Did you solved it? Please reply with the solution. Thanks!
|
|
|
|
|
hi...
i have problem...
dev env win2k .net(vc7.1)
CoInitializeSecurity(...) return error RPC_E_TOO_LATE
why...
|
|
|
|
|
You can go away without this method call in Win2K. So, u can safely delete this method call. However, there is an alternate as well:
Add _WIN32_DCOM define flag to preprocessor options
Add to the Using WMIDlg.cpp the following couple of include statements:
#include <ObjIdl.h>
#include <ObjBase.h>
You can find the answer to a similar question down in another thread by otchenash
Hope this will work
I hated going to weddings. All the grandmas would poke me saying "You're next". They stopped that when I started doing it to them at funerals.
My Articles
|
|
|
|
|
Hi,
everything with "Getting Information from WMI in Visual C++" works OK in my environment (Windows XP), but when I execute it under Windows 2000, I can only press the button "Get Processor Load ..." once in order to get the small dialog telling me the CPU load; the second and subsequent times I press this button, nothing happens.
This already happens with the executable that I downloaded, so I assume this to be a general problem.
What is wrong? Grateful for any hint, Axel.
|
|
|
|
|
My project doesnt compile because I dont have it file..
Anybody will help me
Thanks!
Alfredo Salvador
|
|
|
|
|
I think it is in the WMI SDK, which is part of the platform SDK. I had the same problem when I installed just the core SDK files.
|
|
|
|
|
when you install MS.net ,this file will be installed on
your conputer,the folder like:C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\
|
|
|
|
|
I often encountered followed problems
HRESULT hRes = CoCreateInstance (
CLSID_WbemAdministrativeLocator,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
IID_IUnknown ,
( void ** ) & pIWbemLocator
) ;
hRes sometimes does not equal to S_OK ,so can you tell me why
the result of SUCCEEDED(hRes)) sometimes <1
ttt
|
|
|
|
|
win2000 and vc6.0:
Using WMIDlg.cpp
C:\Documents and Settings\Administrator\桌面\Using_WMI\Using WMIDlg.cpp(185) : error C2065: 'CoInitializeSecurity' : undeclared identifier
C:\Documents and Settings\Administrator\桌面\Using_WMI\Using WMIDlg.cpp(192) : error C2065: 'EOAC_NONE' : undeclared identifier
Error executing cl.exe.
Using WMI.exe - 2 error(s), 0 warning(s)
who can help me ?
|
|
|
|
|