Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC COM
I have a MFC application. It initializes COM through following code (part of that code):
BOOL WMIInterface::ConnectWMI()
{
    LOG_API(CommonLogger::cmpPrintChannelSerializer,"WMIInterface::ConnectWMI()");
 
    HRESULT hres;
    int PrJobsCount =0;
 
    hres = CoCreateInstance(
        CLSID_WbemLocator,             
        0, 
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID *) &pLocation_);
 
    if (FAILED(hres))
    {
		LOG_API_MSG("CoCreateInstance() failed..");
        return FALSE;       // Program has failed.
    }
........
 
As you can see it have no CoInitializeEx.... My assumption was it should fail CoCreateInstance, but it not failing in WINDOWS 7, it fails for Windows XP...
 
HRESULT hres;
// Initialize COM. ------------------------------------------
hres =  CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
    LOG_API(CommonLogger::cmpSmc, "CMultiplePrinterDlg::OnRun - CoInitializeEx Failed...Can't proceed..");
    return;
}
 
...
 

Any clue why?
Posted 6-Dec-12 0:31am
Edited 6-Dec-12 1:58am
v2
Comments
armagedescu at 6-Dec-12 8:01am
   
What is exactly the value of hres, result returned by CoCreateInstance?
Jochen Arndt at 6-Dec-12 9:02am
   
There is no difference between S_OK and NULL. Both are defined as 0 (zero).
ThatsAlok at 6-Dec-12 21:32pm
   
thats why MS recommend FAILED or SUCCESS macro to check for validation
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should look at the hres code. For example, assign the hres to the m_sc member of an COleException and then call GetErrorMessage on the exception object. This probably tells you more about the root cause.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I never have a problem with CoInitializeEx myself, after reading MSDN it says:
 
Return Values
This function supports the standard return values E_INVALIDARG, E_OUTOFMEMORY, and E_UNEXPECTED, as well as the following:
S_OK
The COM library was initialized successfully on the calling thread.
S_FALSE
The COM library is already initialized on the calling thread.
RPC_E_CHANGED_MODE
 
A previous call to CoInitializeEx specified a different concurrency model for the calling thread, or the thread that called CoInitializeEx currently belongs to the neutral threaded apartment.

 
so perhaps you are mistaking an "already initialized" return code for an "error" return code.
 

 
Here is a function to retrieve an error message from a HRESULT:
 
CString CApp::GetErrorText(HRESULT hr)
{
    if(FACILITY_WINDOWS == HRESULT_FACILITY(hr))
        hr = HRESULT_CODE(hr);
    TCHAR* szErrMsg;
 
    if(::FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
        NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR)&szErrMsg, 0, NULL) != 0)
    {
        LocalFree(szErrMsg);
    }
    else
    {
        _stprintf(szErrMsg, _T("Unkown Error: %#x"), hr);
    }
 
    return CString(szErrMsg);
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 359
1 Richard MacCutchan 320
2 OriginalGriff 280
3 Jared Sanow 145
4 Snehasish_Nandy 115
0 Sergey Alexandrovich Kryukov 5,939
1 OriginalGriff 4,885
2 CPallini 2,473
3 Abhinav S 1,507
4 Richard MacCutchan 1,437


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 20 Nov 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100