本文介绍了CoCreateInstance无法为Windows XP进行COM初始化...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.
    }
........





正如你所看到它没有CoInitializeEx .. ..我的假设是它应该失败CoCreateInstance,但它没有在 WINDOWS 7 中失败,它失败了 Windows XP ...





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?

推荐答案


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 />





因此,您可能会错误地将已初始化的返回代码误认为是错误返回代码。 />






这是一个从HRESULT中检索错误消息的函数:





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);
}


这篇关于CoCreateInstance无法为Windows XP进行COM初始化...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:07