MDM bridge WMI: How to allow camera MDM_Policy_Config01_Camera02 - c++

I would like to enable and disable windows devices as camera using WMI in C++. For example I'm able to access to table MDM_Policy_Result01_Camera02 and get the property AllowCamera quite easily where on the documentation is specify "Access type: Read/write".
So in my opinion I should be able to modify it.
But seems like WQL doesn't work with UPDATE.
This is my code to access to table MDM_Policy_Result01_Camera02 :
#include <iostream>
#define _WIN32_DCOM
#include <windows.h>
#include <Wbemidl.h>
#include <comdef.h>
# pragma comment(lib, "wbemuuid.lib")
bool initializeCom(){
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
std::cout << "Failed to initialize COM library. Error code = 0x"
<< std::hex << hres << std::endl;
return false; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(
nullptr,
-1, // COM authentication
nullptr, // Authentication services
nullptr, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
nullptr, // Authentication info
EOAC_NONE, // Additional capabilities
nullptr // Reserved
);
if (FAILED(hres))
{
std::cout << "Failed to initialize security. Error code = 0x"
<< std::hex << hres << std::endl;
CoUninitialize();
return false; // Program has failed.
}
return true;
}
bool setUpWBEM(IWbemLocator*& wbemLocator, IWbemServices*& wbemServices){
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
HRESULT hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &wbemLocator);
if (FAILED(hres))
{
std::cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< std::hex << hres << std::endl;
CoUninitialize();
return false; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer wbemServices
// to make IWbemServices calls.
hres = wbemLocator->ConnectServer(
_bstr_t(L"Root\\CIMv2\\MDM\\DMMap"), // Object path of WMI namespace
nullptr, // User name. NULL = current user
nullptr, // User password. NULL = current
0, // Locale. NULL indicates current
0, // Security flags.
0, // Authority (for example, Kerberos)
0, // Context object
&wbemServices // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
std::cout << "Could not connect. Error code = 0x" << std::hex << hres << std::endl;
wbemLocator->Release();
CoUninitialize();
return false; // Program has failed.
}
std::cout << "Connected to ROOT\\CIMV2 WMI namespace" << std::endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
wbemServices, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
nullptr, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
nullptr, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
std::cout << "Could not set proxy blanket. Error code = 0x"
<< std::hex << hres << std::endl;
wbemServices->Release();
wbemLocator->Release();
CoUninitialize();
return false; // Program has failed.
}
return true;
}
int main() {
std::cout << "HelloWorld" << std::endl;
IWbemLocator* wbemLocator{nullptr};
IWbemServices* wbemServices{nullptr};
try{
if(!initializeCom())
throw "initializeCom failed";
if(!setUpWBEM(wbemLocator,wbemServices))
throw "setUpWBEM failed";
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
BSTR bstr_wql = SysAllocString(L"WQL" );
BSTR bstr_sql = SysAllocString(L"SELECT AllowCamera FROM MDM_Policy_Result01_Camera02" );
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator{nullptr};
HRESULT hres = wbemServices->ExecQuery(
bstr_wql,
bstr_sql,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
nullptr,
&pEnumerator);
if (FAILED(hres))
{
std::cout << "Query for operating system name failed."
<< " Error code = 0x"
<< std::hex << hres << std::endl;
wbemServices->Release();
wbemLocator->Release();
CoUninitialize();
throw "ExecQuery failed";; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj{nullptr};
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hr = pclsObj->Get(L"AllowCamera", 0, &vtProp, 0, 0);
if(FAILED(hr))
std::cout << "Failed to get name " << std::endl;
std::cout << "Camera allow : " << vtProp.intVal << std::endl;
VariantClear(&vtProp);
pclsObj->Release();
}
// Cleanup
// ========
wbemServices->Release();
wbemLocator->Release();
pEnumerator->Release();
CoUninitialize();
} catch(const std::string& error){
std::cout << error << std::endl;
}
return 0;
}
Changing SELECT AllowCamera FROM MDM_Policy_Result01_Camera02 with UPDATE MDM_Policy_Result01_Camera02 SET AllowCamera=0 doesn't work ...
If you have any idea, let me know !

Finally came out the MDM brigde WSI C++ example :
https://learn.microsoft.com/fr-fr/windows/win32/wmisdk/example--calling-a-provider-method

Related

WMI: Getting weird value from Win32_OperatingSystem table

I faced an issue using WMI with C++.
It is the first time I've used it so I took the basic documentation example. But running it doesn't work for me.
I tried to extract the OS Name from Win32_OperatingSystem but, when I display it, I got 0x9a2fec.
I tried then with MaxNumberOfProcesses, and I got "-1".
I am on Windows 10, and I have no idea what's going on.
My code:
#include <iostream>
#define _WIN32_DCOM
#include <windows.h>
#include <Wbemidl.h>
#include <comdef.h>
# pragma comment(lib, "wbemuuid.lib")
bool initializeCom(){
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
std::cout << "Failed to initialize COM library. Error code = 0x"
<< std::hex << hres << std::endl;
return false; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(
nullptr,
-1, // COM authentication
nullptr, // Authentication services
nullptr, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
nullptr, // Authentication info
EOAC_NONE, // Additional capabilities
nullptr // Reserved
);
if (FAILED(hres))
{
std::cout << "Failed to initialize security. Error code = 0x"
<< std::hex << hres << std::endl;
CoUninitialize();
return false; // Program has failed.
}
return true;
}
bool setUpWBEM(IWbemLocator*& wbemLocator, IWbemServices*& wbemServices){
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
HRESULT hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &wbemLocator);
if (FAILED(hres))
{
std::cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< std::hex << hres << std::endl;
CoUninitialize();
return false; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer wbemServices
// to make IWbemServices calls.
hres = wbemLocator->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
nullptr, // User name. NULL = current user
nullptr, // User password. NULL = current
0, // Locale. NULL indicates current
0, // Security flags.
0, // Authority (for example, Kerberos)
0, // Context object
&wbemServices // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
std::cout << "Could not connect. Error code = 0x" << std::hex << hres << std::endl;
wbemLocator->Release();
CoUninitialize();
return false; // Program has failed.
}
std::cout << "Connected to ROOT\\CIMV2 WMI namespace" << std::endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
wbemServices, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
nullptr, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
nullptr, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
std::cout << "Could not set proxy blanket. Error code = 0x"
<< std::hex << hres << std::endl;
wbemServices->Release();
wbemLocator->Release();
CoUninitialize();
return false; // Program has failed.
}
return true;
}
int main() {
std::cout << "HelloWorld" << std::endl;
IWbemLocator* wbemLocator{nullptr};
IWbemServices* wbemServices{nullptr};
try{
if(!initializeCom())
throw "initializeCom failed";
if(!setUpWBEM(wbemLocator,wbemServices))
throw "setUpWBEM failed";
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
BSTR bstr_wql = SysAllocString(L"WQL" );
BSTR bstr_sql = SysAllocString(L"SELECT * FROM Win32_OperatingSystem" );
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator{nullptr};
HRESULT hres = wbemServices->ExecQuery(
bstr_wql,
bstr_sql,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
nullptr,
&pEnumerator);
if (FAILED(hres))
{
std::cout << "Query for operating system name failed."
<< " Error code = 0x"
<< std::hex << hres << std::endl;
wbemServices->Release();
wbemLocator->Release();
CoUninitialize();
throw "ExecQuery failed";; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj{nullptr};
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
if(FAILED(hr))
std::cout << "Failed to get name " << std::endl;
std::cout << " OS Name : " << vtProp.bstrVal << std::endl;
VariantClear(&vtProp);
pclsObj->Release();
}
// Cleanup
// ========
wbemServices->Release();
wbemLocator->Release();
pEnumerator->Release();
CoUninitialize();
} catch(const std::string& error){
std::cout << error << std::endl;
}
return 0;
}
If you have any idea, please tell me.
The issue is in the (almost) last stage of your program: the code that displays the retrieved "OS Name":
std::cout << " OS Name : " << vtProp.bstrVal << std::endl; // Prints pointer address
The problem here is that the BSTR type uses a wchar_t* for its buffer1 and the std::cout stream (an object of the std::ostream class) doesn't have an << overload that takes a wchar_t* argument (it has one for char*) – so it uses the version that takes a const void* (as the best match), which prints the address itself, rather than the pointed-to string.
In your case, there is a simple fix: Use the std::wcout stream (a std::wostream object), instead2, which does have an << overload that takes a wchar_t* argument:
std::wcout << L" OS Name : " << vtProp.bstrVal << std::endl; // Prints the wchar_t* string
Simply replacing your output line with the above, on my platform, changed the displayed output from a pointer value (as you are seeing) to the following:
OS Name : Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk0\Partition3
I'm not sure if that's exactly what you would like but it seems like a great improvement. I'm sure you can readily add code to edit that string into the exact format you want.
1 Note that, although the BSTR type is defined as a wchar_t*, it's not really quite the same thing, in some ways. See this answer for more information.
2 On re-reading the document you linked, I see that wcout is used there, for the display of the OS Name.

Read the windows log files using C++ and WMI(Windows Management Instrumentation)

I have been going through the windows docs to access the windows log files using C++ and WMI. I saw a sample code for creating a WMI application. But it shows an error when I compile it using the g++ compiler.
code:
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
int main(int argc, char **argv)
{
HRESULT hres;
// Initialize COM.
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. "
<< "Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Initialize
hres = CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. "
<< "Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Obtain the initial locator to Windows Management
// on a particular host computer.
IWbemLocator *pLoc = 0;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object. "
<< "Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
IWbemServices *pSvc = 0;
// Connect to the root\cimv2 namespace with the
// current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // WMI namespace
NULL, // User name
NULL, // User password
0, // Locale
NULL, // Security flags
0, // Authority
0, // Context object
&pSvc // IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
// Set the IWbemServices proxy so that impersonation
// of the user (client) occurs.
hres = CoSetProxyBlanket(
pSvc, // the proxy to set
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Use the IWbemServices pointer to make requests of WMI.
// Make requests here:
// For example, query for all the running processes
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_Process"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for processes failed. "
<< "Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
else
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hres = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << "Process Name : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
pclsObj->Release();
pclsObj = NULL;
}
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
return 0; // Program successfully completed.
}
If I saved it in a file named sample.cpp I compiled it suing the command
g++ -o sample sample.cpp
Is that a mistake? Should I be compiling it in a different way?
I'm new to this. Can somebody please guide me on how to learn the WMI using C++ in order to read the windows log files?
Kindly respond
Thank you.
It seems like you have just copy pasted the code from a website, due to which & has become & (because of HTML) as mentioned in the comments by #Vlad274.
If you're compiling in the command prompt, you have to link your libraries before compilation, probably ole32 and oleaut.
If you're using Visual Studio, make sure they are included in the linker settings.(Which they are by default in VS2017)
EXTRA: Before using the Get method, you could check for property names using the GetNames method to ensure that the property 'Name' exists.

WMI C++ get data from object

I implemented 1-5 steps from :
https://msdn.microsoft.com/en-us/library/aa390423(v=vs.85).aspx
Everyting works, but i have problem with getting data from object which i retreive with query.
My code:
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
//cout << "Failed to initialize COM library. Error code = 0x"
// << hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
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(hres))
{
//cout << "Failed to initialize security. Error code = 0x"
// << hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
//cout << "Failed to create IWbemLocator object."
// << " Err code = 0x"
// << hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (for example, Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
//cout << "Could not connect. Error code = 0x"
// << hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
//OutputDebugString("Connected to ROOT\\CIMV2 WMI namespace");
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
code above is the same as msdn example.
Problem is here:
// Get the currently inserted USB devices
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_USBControllerDevice"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
//cout << "Query for USB devices at startup failed."
// << " Error code = 0x"
// << hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Get the data from the query
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
ostringstream os;
while (pEnumerator){
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn){
break;
}
VARIANT vtProp;
VariantInit(&vtProp);
// Get the value of the accessstate property
hr = pclsObj->Get(L"Dependent", 0, &vtProp, 0, 0);
if (FAILED(hr)){
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
os.str("");
os.clear();
os << vtProp.bstrVal;
OutputDebugString(os.str().c_str());
AddDevice(os.str());//Add device to devices vector
VariantClear(&vtProp);
pclsObj->Release();
}
Exactly in this place:
hr = pclsObj->Get(L"Dependent", 0, &vtProp, 0, 0);
This link describes table elements:
https://msdn.microsoft.com/en-us/library/aa394505(v=vs.85).aspx
So i got Dependent value. My problem is how to get this Dependent properties:
https://msdn.microsoft.com/en-us/library/aa387884(v=vs.85).aspx
for example : DeviceID
If anyone can help i will be very very grateful!

QCoreApplication QApplication with WMI

I found some WMI C++ Application Examples in MSDN website.
I have tried the code form the below link
http://msdn.microsoft.com/en-us/library/aa390423%28v=VS.85%29.aspx
when I copied and run the application as win32 console application it worked well.
the same code I put into the Qt application ,
When I use QApplication anApplication ( argc, argv ); in my code it is not working
But if I use QCoreApplication anApplication ( argc, argv ); it is working
shows me error like "when i change QCoreApplication to QApplication The below source code not works and gives me error like " Failed to initialize COM library. Error code = 0x80010106 "
Any suggestions or help
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
// Qt Includes
#include <QtCore>
#include <QtGui>
//----------------------------------------------------------------------------
// Main Function
int main( int a_argc, char *a_argv[] )
//************************************
{
// initialize the application
QApplication anapplication( a_argc, a_argv );
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
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(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_USBControllerDevice"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
//ExecQuery(L"WQL", L"SELECT * FROM Win32_LogicalDisk", WBEM_FLAG_FORWARD_ONLY, NULL, &iter);
if (FAILED(hres))
{
cout << "Query for operating system name failed."
<< " Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(FAILED(hr)||0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
/*hr = pclsObj->Get(L"VolumeName", 0, &vtProp, 0, 0);
wcout << " VolumeName : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);*/
if (FAILED(pclsObj->Get(L"Antecedent", 0, &vtProp, 0, 0)))
{
cout<<"The specified property is not found."<<endl;
}
else
{
wcout <<vtProp.bstrVal << endl;
}
pclsObj->Release();
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
// pclsObj->Release();
CoUninitialize();
// return 0; // Program successfully completed.
//execute the application
int anInt = anapplication.exec();
return anInt;
}
//-----------------------------------------------------------------------------
The error you are receiving is due to the fact that COM is already initialized. QApplication calls OleInitialize in its constructor, but QCoreApplication does not, so that's why you get the error 0x80010106 (RPC_E_CHANGED_MODE) when you use QApplication. For further reading, see the documentation for CoInitializeEx. The following article should provide more insight.
At first I have used this code
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (hres == RPC_E_CHANGED_MODE) {
OleUninitialize();
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
}
When I get error "Failed to initialize COM library(0x80010106)
I just call OleUninitialize to deal with OleInitialize() in QApplication, but that could break some qt functionality.
As I found the best is to use flag COINIT_APARTMENTTHREADED for CoInitializeEx
MSDN
Generally, a thread that creates a window should use the COINIT_APARTMENTTHREADED flag, and other threads should use COINIT_MULTITHREADED. However, some COM components require a particular threading model. The MSDN documentation should tell you when that is the case.
For me it`s a black magic, but it helped me

Set Fan Speed on Windows in C++

I have this copied code from Example: Calling a Provider Method, it modified it now should it change CPU fan speed:
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
int main(int iArgCnt, char ** argv)
{
if(IsDebuggerPresent())cout<<"Debugee?\n";
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you must specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = 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(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object. "
<< "Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: ---------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the local root\cimv2 namespace
// and obtain pointer pSvc to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// Set security levels for the proxy ------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
BSTR MethodName = SysAllocString(L"SetSpeed");
BSTR ClassName = SysAllocString(L"CIM_Fan");
IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);
IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0,
&pInParamsDefinition, NULL);
IWbemClassObject* pClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
// Create the values for the in parameters
VARIANT varCommand;
varCommand.vt = VT_BSTR;
varCommand.bstrVal = L"10";
// Store the value for the in parameters
hres = pClassInstance->Put(L"CommandLine", 0,
&varCommand, 0);
wprintf(L"The command is: %s\n", V_BSTR(&varCommand));
// Execute Method
IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(ClassName, MethodName, 0,
NULL, pClassInstance, &pOutParams, NULL);
if (FAILED(hres))
{
cout << "Could not execute method. Error code = 0x"
<< hex << hres << endl;
try{
VariantClear(&varCommand);
SysFreeString(ClassName);
SysFreeString(MethodName);
pClass->Release();
pInParamsDefinition->Release();
// pOutParams->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
}
catch(...){
cout<<""; // NOTHING
};
system("pause");
return 1; // Program has failed.
}
// To see what the method returned,
// use the following code. The return value will
// be in &varReturnValue
VARIANT varReturnValue;
hres = pOutParams->Get(_bstr_t(L"ReturnValue"), 0,
&varReturnValue, NULL, 0);
// Clean up
//--------------------------
VariantClear(&varCommand);
VariantClear(&varReturnValue);
SysFreeString(ClassName);
SysFreeString(MethodName);
pClass->Release();
pInParamsDefinition->Release();
pOutParams->Release();
pLoc->Release();
pSvc->Release();
CoUninitialize();
system("pause");
return 0;
}
When I run this, it not work. What I should change?
Fan speed control via WMI is going to be pretty vendor-specific; there are no general controls for CPU/mainboard fans (though WMI is what people should be doing). Also, this would be way easier in PowerShell or even C# than trying to use C++ to play with WMI stuff. At least figure out how to do it in PSh first, then transcribe it to C++