共用方式為


ILanguageExceptionErrorInfo2::CapturePropagationContext 方法 (restrictederrorinfo.h)

擷取跨語言界限和線程的例外狀況內容。

語法

HRESULT CapturePropagationContext(
  [in] IUnknown *languageException
);

參數

[in] languageException

跨進程使用 Apartment-agile、in-proc 和 marshal-by-value 的錯誤物件。

傳回值

如果此方法成功,則會傳回 S_OK。 否則,它會傳回 HRESULT 錯誤碼。

備註

擷取PropagationContext 是由重新擲回錯誤時的語言投影所使用。 這包括在語言界限收到錯誤時。 因此,利用 CapturePropagationContext 有助於確保針對目前的重新擲回擷取例外狀況的回溯追蹤。 這也有助於確保當例外狀況跨越語言框線時,不會遺失相關的偵錯資訊。

一般而言,此方法會建立 IRestrictedErrorInfo 對象的連結清單,以提供例外狀況傳播方式的其他錯誤資訊。 這項資訊會在損毀傾印分析期間公開為例外狀況記錄所參考的存根例外狀況。 使用此連結清單,您可以觀察例外狀況傳播的所有語言界限和線程的回溯追蹤,包括錯誤源自何處。

範例

下列範例示範從另一個投影或 WRL 接收位於其語言界限的錯誤投影。 這是現有的案例,但如果先前的投影無法這麼做,則允許系統擷取其他內容。

HRESULT CreateFooExceptionFromLanguageBoundaryError(HRESULT errorReceived, IFooException** createdException)
{
    HRESULT hr = S_OK;
    ComPtr<IFooException> exception;
    // Get the current error
    ComPtr<IRestrictedErrorInfo> restrictedErrorInfo;
    *createdException = nullptr;
    if (SUCCEEDED(GetRestrictedErrorInfo(&restrictedErrorInfo)))
    {
        // Retrieve details regarding the error to determine if it is a stale error
        // or if it is the error we received at the boundary.
        BSTR description;
        HRESULT errorOriginated;
        BSTR restrictedDescription;
        BSTR capabilitySid;
        hr = restrictedErrorInfo->GetErrorDetails(
            &description,
            &errorOriginated,
            &restrictedDescription,
            &capabilitySid);
        if (SUCCEEDED(hr) && errorReceived == errorOriginated)
        {
            hr = CreateFooException(
                errorOriginated,
                restrictedDescription,
                restrictedErrorInfo.Get(),
                &exception);
            // Query for new interface to see if the new logic is there to
            // capture the current propagation context.
            ComPtr<ILanguageExceptionErrorInfo2> languageExceptionErrorInfo;
            if (SUCCEEDED(restrictedErrorInfo.As(&languageExceptionErrorInfo)))
            {
                languageExceptionErrorInfo->CapturePropagationContext(nullptr);
            }		
            *createdException = exception.Detach();
            SetRestrictedErrorInfo(restrictedErrorInfo.Get());
            SysFreeString(description);
            SysFreeString(restrictedDescription);
            SysFreeString(capabilitySid);
            return hr;
        }
        SysFreeString(description);
        SysFreeString(restrictedDescription);
        SysFreeString(capabilitySid);
    }

    // We are here if the error didn't match or we couldn't get error details.
    // So originate a new error.
    // OriginateErrorInfoForThrow will call RoOriginateLanguageException, which will      
    // capture the context
    hr = CreateFooException(errorReceived, nullptr, nullptr, &exception);
    if(SUCCEEDED(hr))
    {
        exception->OriginateErrorInfoForThrow();
        *createdException = exception.Detach();
    }
    return hr;
}


規格需求

需求
最低支援的用戶端 Windows 10 版本 1703 [僅限傳統型應用程式]
最低支援的伺服器 Windows Server 2016 [僅限傳統型應用程式]
目標平台 Windows
標頭 restrictederrorinfo.h

另請參閱

ILanguageExceptionErrorInfo2