title: QT QFileDialog create unknown COM Object and security
body: <p>I need help for this issue. i want to uses WMI in my project, so that i can use WQL to get some data fom Windows OS.
  Here is my code</p>
  
  <pre><code>QList&lt;Drive&gt; SystemDrive::getSystemDrive()
  {
      QList&lt;Drive&gt; list;
  
      #ifdef _WIN64
      HRESULT hRes;
      hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED );
      if(hRes == RPC_E_CHANGED_MODE)
      {
          //qDebug() &lt;&lt; "Unable to launch COM: 0x" &lt;&lt; QString::number(hRes, 16) &lt;&lt; endl;
          //OleUninitialize();
          CoUninitialize();
          hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
          if(FAILED(hRes))
          {
              qDebug() &lt;&lt; "Unable to launch COM: 0x" &lt;&lt; QString::number(hRes, 16);
          }
          //return 1;
      }
      qDebug() &lt;&lt; "CoInitializeEx result 0x" &lt;&lt; QString::number(hRes, 16);
  
      if((FAILED(hRes = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0))))
      {
          qDebug() &lt;&lt; "Unable to initialize security: 0x" &lt;&lt; QString::number(hRes, 16);
      }
      IWbemLocator* pLocator = NULL;
      if(FAILED(hRes = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_ALL, IID_PPV_ARGS(&amp;pLocator))))
      {
          qDebug() &lt;&lt; "Unable to create a WbemLocator: " &lt;&lt; std::hex &lt;&lt; hRes &lt;&lt; endl;
          //return 1;
      }
  
      IWbemServices* pService = NULL;
      if(FAILED(hRes = pLocator-&gt;ConnectServer(L"root\\CIMV2", NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &amp;pService)))
      {
          pLocator-&gt;Release();
          qDebug() &lt;&lt; "Unable to connect to \"CIMV2\": " &lt;&lt; std::hex &lt;&lt; hRes &lt;&lt; endl;
          //return 1;
      }
  
      IEnumWbemClassObject* pEnumerator = NULL;
      if(FAILED(hRes = pService-&gt;ExecQuery(L"WQL", L"SELECT * FROM Win32_LogicalDisk", WBEM_FLAG_FORWARD_ONLY, NULL, &amp;pEnumerator)))
      {
          pLocator-&gt;Release();
          pService-&gt;Release();
          qDebug() &lt;&lt; "Unable to retrive Logical Disk: " &lt;&lt; QString::number(hRes, 16) &lt;&lt; endl;
          //return 1;
      }
  
      IWbemClassObject* clsObj = NULL;
      int numElems;
      while((hRes = pEnumerator-&gt;Next(WBEM_INFINITE, 1, &amp;clsObj, (ULONG*)&amp;numElems)) != WBEM_S_FALSE)
      {
          if(FAILED(hRes))
          break;
  
          Drive *tmpDrive;
  
          VARIANT vRet;
          VariantInit(&amp;vRet);
          if(SUCCEEDED(clsObj-&gt;Get(L"Caption", 0, &amp;vRet, NULL, NULL)) &amp;&amp; vRet.vt == VT_BSTR)
          {
              tmpDrive = new Drive(QString((QChar*) vRet.bstrVal, SysStringLen(vRet.bstrVal)));
              tmpDrive-&gt;setType(Drive::DRIVE_TYPE::DRIVE_LOGICAL);
              VariantClear(&amp;vRet);
          }
          VariantInit(&amp;vRet);
          if(SUCCEEDED(clsObj-&gt;Get(L"DeviceID", 0, &amp;vRet, NULL, NULL)) &amp;&amp; vRet.vt == VT_BSTR)
          {
              QString id = "\\\\.\\";
  
              id.append(QString((QChar*) vRet.bstrVal, SysStringLen(vRet.bstrVal)));
              tmpDrive-&gt;setId(id);
              VariantClear(&amp;vRet);
          }
  
          clsObj-&gt;Release();
          list.append(*tmpDrive);
      }
  
       pEnumerator-&gt;Release();
       pService-&gt;Release();
       pLocator-&gt;Release();
       CoUninitialize();
  #endif
  
       return list;
  }
  </code></pre>
  
  <p>that code run perfectly every time i call it. I can get what i need (List of Logical drive). The problem is when i call <code>QFileDialog</code> object and then call that function, that code will not work. I get error for <code>coInitializeSecurity</code> with error <code>RPC_E_TOO_LATE</code> and as result my <code>ExecQuery</code> will fail with error code <code>0x80041003</code> (WBEM_E_ACCESS_DENIED). </p>
  
  <p>I think <code>QFileDialog</code> create COM object because there is mesage output in <code>Aplication Output Tab</code> (stdout) when I call <code>QFileDialog</code> object.</p>
  
  <pre><code>CoCreateInstance failed ()
  CoCreateInstance failed ()
  </code></pre>
  
  <p>If I Call <code>QFileDialog</code> object first before that function, these message will not occur, and that function still not work. I need that function run normally without any interuption from any COM objects.</p>
  
  <p>My questions :</p>
  
  <ol>
  <li><p>Are there any way to detect what COM object that created by QT?</p></li>
  <li><p>How can I know which security that initialized (CoInitializeSecurity) by QT?</p></li>
  </ol>
  
  <p><strong>EDITED</strong>
  After read answers from <a href="https://stackoverflow.com/users/17034/hans-passant">Hans</a> and <a href="https://stackoverflow.com/users/8666197/daniel-s%C4%99k">Daniel</a>, I change my code. So I put <code>CoInitializeEx</code> in first time my app running with <code>COINIT_APARTMENTTHREADED</code>, after that I initialize <code>CoInitializeSecurity</code>, and then erase all <code>CoInitializeEx</code>, <code>CoInitializeSecurity</code>, and 'CoUninitialize' in that function. It works, i can call that function after opening <code>QFileDialog</code>.</p>
  
  <p>Now, </p>
  
  <ol start="3">
  <li>Where should I call 'CoUninitialize'? should I call it before my app closed? </li>
  <li>how can I assure that all COM interfaces that I have created (<code>WMI</code> and <code>QFileDialog</code>) destroyed properly? asume that I release all interface and enumeration from WMI like in my code, and for <code>QFileDialog</code> I call it in stack memory.</li>
  </ol>