Bagikan melalui


Daftarkan program

Setelah mesin debug memperoleh port, yang diwakili oleh antarmuka IDebugPort2 , langkah selanjutnya dalam memungkinkan program untuk di-debug adalah mendaftarkannya dengan port. Setelah terdaftar, program tersedia untuk debugging dengan salah satu cara berikut:

  • Proses melampirkan, yang memungkinkan debugger untuk mendapatkan kontrol penelusuran kesalahan lengkap dari aplikasi yang sedang berjalan.

  • Penelusuran kesalahan just-in-time (JIT), yang memungkinkan penelusuran kesalahan setelah program berjalan secara independen dari debugger. Ketika arsitektur run-time menangkap kesalahan, debugger diberi tahu sebelum sistem operasi atau lingkungan runtime merilis memori dan sumber daya program yang salah.

Prosedur pendaftaran

Untuk mendaftarkan program Anda

  1. Panggil metode AddProgramNode yang diterapkan oleh port.

    IDebugPortNotify2::AddProgramNode memerlukan penunjuk ke antarmuka IDebugProgramNode2 .

    Biasanya, ketika sistem operasi atau lingkungan run-time memuat program, ia membuat node program. Jika mesin debug (DE) diminta untuk memuat program, DE membuat dan mendaftarkan simpul program.

    Contoh berikut menunjukkan mesin debug yang meluncurkan program dan mendaftarkannya dengan port.

    Nota

    Sampel kode ini bukan satu-satunya cara untuk meluncurkan dan melanjutkan proses; kode ini terutama merupakan contoh mendaftarkan program dengan port.

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