Results 1 to 11 of 11

Thread: QCroreApplication QApplication with WMI

  1. #1
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QCroreApplication QApplication with WMI

    Dear all,

    I have serached for using WMI with Qt.

    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/libr...=VS.85%29.aspx

    when i copied and run the application as win32 console application it worked well.

    the same code i ut into the Qt application ,

    When i use QApplication anApplication ( argc, argv ); in my code it is not working

    But if i use QCroreApplication anApplication ( argc, argv ); it is working

    Any suggestions or help

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QCroreApplication QApplication with WMI

    Post your source code

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCroreApplication QApplication with WMI

    and please use [ code ] tags this time

  4. #4
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCroreApplication QApplication with WMI

    when i change QCroreApplication to QApplication The below source code not works and gives me errror like " Failed to initialize COM library. Error code = 0x80010106 "

    Qt Code:
    1. #define _WIN32_DCOM
    2. #include <iostream>
    3. using namespace std;
    4. #include <comdef.h>
    5. #include <Wbemidl.h>
    6.  
    7. # pragma comment(lib, "wbemuuid.lib")
    8.  
    9. // Qt Includes
    10. #include <QtCore>
    11. #include <QtGui>
    12.  
    13.  
    14. //----------------------------------------------------------------------------
    15.  
    16. // Main Function
    17. int main( int a_argc, char *a_argv[] )
    18. //************************************
    19. {
    20. // initialize the application
    21. QApplication anapplication( a_argc, a_argv );
    22.  
    23. HRESULT hres;
    24.  
    25. // Step 1: --------------------------------------------------
    26. // Initialize COM. ------------------------------------------
    27.  
    28. hres = CoInitializeEx(0, COINIT_MULTITHREADED);
    29. if (FAILED(hres))
    30. {
    31. cout << "Failed to initialize COM library. Error code = 0x"
    32. << hex << hres << endl;
    33. return 1; // Program has failed.
    34. }
    35.  
    36. // Step 2: --------------------------------------------------
    37. // Set general COM security levels --------------------------
    38. // Note: If you are using Windows 2000, you need to specify -
    39. // the default authentication credentials for a user by using
    40. // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
    41. // parameter of CoInitializeSecurity ------------------------
    42.  
    43. hres = CoInitializeSecurity(
    44. NULL,
    45. -1, // COM authentication
    46. NULL, // Authentication services
    47. NULL, // Reserved
    48. RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
    49. RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
    50. NULL, // Authentication info
    51. EOAC_NONE, // Additional capabilities
    52. NULL // Reserved
    53. );
    54.  
    55.  
    56. if (FAILED(hres))
    57. {
    58. cout << "Failed to initialize security. Error code = 0x"
    59. << hex << hres << endl;
    60. CoUninitialize();
    61. return 1; // Program has failed.
    62. }
    63.  
    64. // Step 3: ---------------------------------------------------
    65. // Obtain the initial locator to WMI -------------------------
    66.  
    67. IWbemLocator *pLoc = NULL;
    68.  
    69. hres = CoCreateInstance(
    70. CLSID_WbemLocator,
    71. 0,
    72. CLSCTX_INPROC_SERVER,
    73. IID_IWbemLocator, (LPVOID *) &pLoc);
    74.  
    75. if (FAILED(hres))
    76. {
    77. cout << "Failed to create IWbemLocator object."
    78. << " Err code = 0x"
    79. << hex << hres << endl;
    80. CoUninitialize();
    81. return 1; // Program has failed.
    82. }
    83.  
    84. // Step 4: -----------------------------------------------------
    85. // Connect to WMI through the IWbemLocator::ConnectServer method
    86.  
    87. IWbemServices *pSvc = NULL;
    88.  
    89. // Connect to the root\cimv2 namespace with
    90. // the current user and obtain pointer pSvc
    91. // to make IWbemServices calls.
    92. hres = pLoc->ConnectServer(
    93. _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
    94. NULL, // User name. NULL = current user
    95. NULL, // User password. NULL = current
    96. 0, // Locale. NULL indicates current
    97. NULL, // Security flags.
    98. 0, // Authority (e.g. Kerberos)
    99. 0, // Context object
    100. &pSvc // pointer to IWbemServices proxy
    101. );
    102.  
    103. if (FAILED(hres))
    104. {
    105. cout << "Could not connect. Error code = 0x"
    106. << hex << hres << endl;
    107. pLoc->Release();
    108. CoUninitialize();
    109. return 1; // Program has failed.
    110. }
    111.  
    112. cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
    113.  
    114.  
    115. // Step 5: --------------------------------------------------
    116. // Set security levels on the proxy -------------------------
    117.  
    118. hres = CoSetProxyBlanket(
    119. pSvc, // Indicates the proxy to set
    120. RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
    121. RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
    122. NULL, // Server principal name
    123. RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
    124. RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
    125. NULL, // client identity
    126. EOAC_NONE // proxy capabilities
    127. );
    128.  
    129. if (FAILED(hres))
    130. {
    131. cout << "Could not set proxy blanket. Error code = 0x"
    132. << hex << hres << endl;
    133. pSvc->Release();
    134. pLoc->Release();
    135. CoUninitialize();
    136. return 1; // Program has failed.
    137. }
    138.  
    139. // Step 6: --------------------------------------------------
    140. // Use the IWbemServices pointer to make requests of WMI ----
    141.  
    142. // For example, get the name of the operating system
    143. IEnumWbemClassObject* pEnumerator = NULL;
    144. hres = pSvc->ExecQuery(
    145. bstr_t("WQL"),
    146. bstr_t("SELECT * FROM Win32_USBControllerDevice"),
    147. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    148. NULL,
    149. &pEnumerator);
    150.  
    151. //ExecQuery(L"WQL", L"SELECT * FROM Win32_LogicalDisk", WBEM_FLAG_FORWARD_ONLY, NULL, &iter);
    152.  
    153. if (FAILED(hres))
    154. {
    155. cout << "Query for operating system name failed."
    156. << " Error code = 0x"
    157. << hex << hres << endl;
    158. pSvc->Release();
    159. pLoc->Release();
    160. CoUninitialize();
    161. return 1; // Program has failed.
    162. }
    163.  
    164. // Step 7: -------------------------------------------------
    165. // Get the data from the query in step 6 -------------------
    166.  
    167. IWbemClassObject *pclsObj;
    168. ULONG uReturn = 0;
    169.  
    170. while (pEnumerator)
    171. {
    172. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
    173. &pclsObj, &uReturn);
    174.  
    175. if(FAILED(hr)||0 == uReturn)
    176. {
    177. break;
    178. }
    179.  
    180. VARIANT vtProp;
    181.  
    182. // Get the value of the Name property
    183. /*hr = pclsObj->Get(L"VolumeName", 0, &vtProp, 0, 0);
    184.   wcout << " VolumeName : " << vtProp.bstrVal << endl;
    185.   VariantClear(&vtProp);*/
    186.  
    187.  
    188. if (FAILED(pclsObj->Get(L"Antecedent", 0, &vtProp, 0, 0)))
    189. {
    190. cout<<"The specified property is not found."<<endl;
    191. }
    192. else
    193. {
    194. wcout <<vtProp.bstrVal << endl;
    195. }
    196.  
    197. pclsObj->Release();
    198. }
    199.  
    200. // Cleanup
    201. // ========
    202.  
    203. pSvc->Release();
    204. pLoc->Release();
    205. pEnumerator->Release();
    206. // pclsObj->Release();
    207. CoUninitialize();
    208.  
    209.  
    210. // return 0; // Program successfully completed.
    211.  
    212. //execute the application
    213. int anInt = anapplication.exec();
    214.  
    215. return anInt;
    216. }
    217. //-----------------------------------------------------------------------------
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QCroreApplication QApplication with WMI

    Do you have the code for CoInitializeEx()?
    What happens if you initialize your QApplication object after CoInitializeEx()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. The following user says thank you to high_flyer for this useful post:

    newb (11th June 2010)

  7. #6
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCroreApplication QApplication with WMI

    Thank you

    now i don get this error

    but in my console it shows like Qt: Could not initialize OLE (error 80010106)

    Any help

  8. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QCroreApplication QApplication with WMI

    Which libs do you link to in your project?
    EDIT:
    I see now, at the top.
    Try delay loading wbemuuid.dll.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #8
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCroreApplication QApplication with WMI

    Thnaks for the reply..

    i get the vid and pid of the usb devices plugged into my system using WMI.

    Now i want to get the drive letter corresponding to the device vid and pid i retrievd.

    code

    hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT * FROM Win32_USBControllerDevice"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator);

    if (FAILED(pclsObj->Get(L"Dependent", 0, &vtProp, 0, 0)))
    {
    cout<<"The specified property is not found."<<endl;

    }
    else
    {
    wcout <<vtProp.bstrVal << endl;

    }

    i get like below

    \NJ96\root\cimv2:Win32_PnPEntity.DeviceID="USB\VID _1A8D&PID_1000\35809402 0874450"

    how can i map DRIVE LETTER to my above output using the WMI

    please help me out

  10. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QCroreApplication QApplication with WMI

    This is not a Qt question, and I am not really familiar with WMI.
    You might get more help with this issue in forums dedicated to WMI development.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #10
    Join Date
    Apr 2010
    Posts
    48
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCroreApplication QApplication with WMI

    Can any body help me out

  12. #11
    Join Date
    Jul 2009
    Location
    Sao Paulo / Brazil
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: QCroreApplication QApplication with WMI

    Hi.
    Using WMI and WMI Query Language is not an easy thing, but if you read carefully and look at the examples on MSDN I'm sure you'll get the picture, for I also use WMI to get PIDs and VIDs for plugged devices.
    http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx

    But , about the CoInitializeEx , I had the very same problem as you... CoInitializeEx is not returning S_OK.. But's that an OK thing, according to MSDN
    As long as CoInitializeEx doesn't return RPC_E_CHANGED_MODE you should be ok, and you should call CoUninitialize in both cases where CoInitializeEx return S_OK or S_FALSE

    Now, the issue with the QApplication object, that I also ran into, is that somewhere very deep in the initialization of an QApp object a call to CoInitialize is being made with parameters that we can't control.. I know that coz I used an API spy that tell me wich api calls a process is making.

    The problem with CoInitialize is that is the same as CoInitializeEx(0, COINIT_APARTMENTTHREADED) and a later call (after the QApplication's instantiation) fails if we try CoInitializeEx(0, COINIT_APARTMENTTHREADED) with a RPC_E_CHANGED_MODE error.

    So it's ok if you call CoInitializeEx(0, COINIT_APARTMENTTHREADED) with a S_FALSE returned.. and then even with an S_FALSE returned you should call CoUninitialize;

    I know that QAxBase calls CoInitializeEx but, does anybody out there know why CoInitialize is called by the QApplication's constructor?

    Tks

    Hernan


    Added after 6 minutes:


    Lol, I just found the villain myself looking for the Could not initialize OLE string within the Qt source
    Lines 754-758 on qapplication_win.cpp... OleInitialize() is called but then we are stuck with a Single-Threaded model for using COM.. that's bad news.
    If one reads the remarks on http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx one can guess that OleInitialize is called for Drag'n'Drop to work.
    Last edited by chesterx; 2nd February 2011 at 21:15.

Similar Threads

  1. Qt without QApplication
    By jakebman in forum Newbie
    Replies: 6
    Last Post: 25th July 2008, 00:38
  2. QApplication::setStyleSheet()
    By Ryhel in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2007, 13:09
  3. regarding QApplication::focusWidget ()
    By sar_van81 in forum Qt Programming
    Replies: 5
    Last Post: 21st December 2006, 10:00
  4. why we have need of QApplication
    By Krishnacins in forum Qt Programming
    Replies: 11
    Last Post: 12th April 2006, 16:37
  5. <QtGui/QApplication> vs. <QApplication>
    By seneca in forum Qt Programming
    Replies: 5
    Last Post: 25th January 2006, 10:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.