共用方式為


註冊程式

偵錯引擎取得以 IDebugPort2 介面表示的埠之後,啟用程式偵錯的下一個步驟是向埠註冊它。 註冊後,程式可以透過以下方法之一進行偵錯:

  • 附加程式,可讓偵錯工具取得執行中應用程式的完整偵錯控制權。

  • Just-In-Time (JIT) 偵錯,可對獨立於偵錯工具執行的程式進行事後偵錯。 當執行階段架構攔截錯誤時,在作業系統或執行階段環境釋放錯誤程式的記憶體和資源之前,會通知偵錯工具。

註冊程序

若要註冊您的程式

  1. 呼叫埠所實作的 AddProgramNode 方法。

    IDebugPortNotify2::AddProgramNode 需要具有指向 IDebugProgramNode2 介面的指標。

    通常,當作業系統或執行階段環境載入程式時,它會建立程式節點。 如果要求除錯引擎 (DE) 載入程式,DE 會建立並登錄程式節點。

    下列範例顯示偵錯引擎啟動程式,並將其註冊至埠。

    備註

    此程式碼範例不是啟動和繼續進程的唯一方法;這段程式碼主要是用連接埠註冊程式的範例。

    // This is an IDebugEngineLaunch2 method.
    HRESULT CDebugEngine::LaunchSuspended(/* omitted parameters */,
                                          IDebugPort2 *pPort,
                                          /* omitted parameters */,
                                          IDebugProcess2**ppDebugProcess)
    {
        // do stuff here to set up for a launch (such as handling the other parameters)
        ...
    
        // Now get the IPortNotify2 interface so we can register a program node
        // in CDebugEngine::ResumeProcess.
        CComPtr<IDebugDefaultPort2> spDefaultPort;
        HRESULT hr = pPort->QueryInterface(&spDefaultPort);
        if (SUCCEEDED(hr))
        {
            CComPtr<IDebugPortNotify2> spPortNotify;
            hr = spDefaultPort->GetPortNotify(&spPortNotify);
            if (SUCCEEDED(hr))
            {
                // Remember the port notify so we can use it in ResumeProcess.
                m_spPortNotify = spPortNotify;
    
                // Now launch the process in a suspended state and return the
                // IDebugProcess2 interface
                CComPtr<IDebugPortEx2> spPortEx;
                hr = pPort->QueryInterface(&spPortEx);
                if (SUCCEEDED(hr))
                {
                    // pass on the parameters we were given (omitted here)
                    hr = spPortEx->LaunchSuspended(/* omitted parameters */,ppDebugProcess)
                }
            }
        }
        return(hr);
    }
    
    HRESULT CDebugEngine::ResumeProcess(IDebugProcess2 *pDebugProcess)
    {
        // Make a program node for this process
        HRESULT hr;
        CComPtr<IDebugProgramNode2> spProgramNode;
        hr = this->GetProgramNodeForProcess(pProcess, &spProgramNode);
        if (SUCCEEDED(hr))
        {
            hr = m_spPortNotify->AddProgramNode(spProgramNode);
            if (SUCCEEDED(hr))
            {
                // resume execution of the process using the port given to us earlier.
               // (Querying for the IDebugPortEx2 interface is valid here since
               // that's how we got the IDebugPortNotify2 interface in the first place.)
                CComPtr<IDebugPortEx2> spPortEx;
                hr = m_spPortNotify->QueryInterface(&spPortEx);
                if (SUCCEEDED(hr))
                {
                    hr  = spPortEx->ResumeProcess(pDebugProcess);
                }
            }
        }
        return(hr);
    }