Click here to Skip to main content
13,195,564 members (66,726 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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
Updated 6-Dec-12 1:58am
v2
Comments
armagedescu 6-Dec-12 8:01am
   
What is exactly the value of hres, result returned by CoCreateInstance?
debdipta_ghosh 6-Dec-12 8:23am
   
For win 7 it is returnong S_OK....for win xp is returning NULL....
Jochen Arndt 6-Dec-12 9:02am
   
There is no difference between S_OK and NULL. Both are defined as 0 (zero).
ThatsAlok 6-Dec-12 21:32pm
   
thats why MS recommend FAILED or SUCCESS macro to check for validation
debdipta_ghosh 6-Dec-12 23:22pm
   
Actually my dev PC is Win7...so I don't got any error....but during testing in WinXP this prob revealed through log.....It is very interesting problem...
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<br />
This function supports the standard return values E_INVALIDARG, E_OUTOFMEMORY, and E_UNEXPECTED, as well as the following: <br />
S_OK <br />
The COM library was initialized successfully on the calling thread. <br />
S_FALSE <br />
The COM library is already initialized on the calling thread. <br />
RPC_E_CHANGED_MODE<br />
<br />
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. <br />


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
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171019.1 | Last Updated 20 Nov 2013
Copyright © CodeProject, 1999-2017
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