Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...

C++
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
Updated 6-Dec-12 1:58am
v2
Comments
[no name] 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
[no name] 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...

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.
 
Share this answer
 
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:

C#
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);
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900