Bagikan melalui


Mendapatkan port

Port mewakili koneksi ke komputer tempat proses berjalan. Mesin itu bisa menjadi komputer lokal atau komputer jarak jauh (yang mungkin dapat menjalankan sistem operasi berbasis non-Windows; lihat Port untuk informasi selengkapnya).

Port diwakili oleh antarmuka IDebugPort2 . Ini digunakan untuk mendapatkan informasi tentang proses yang berjalan pada komputer yang terhubung dengan port.

Mesin debug memerlukan akses ke port untuk mendaftarkan simpul program dengan port dan untuk memenuhi permintaan informasi proses. Misalnya, jika mesin debug mengimplementasikan antarmuka IDebugProgramProvider2 , implementasi untuk metode GetProviderProcessData dapat meminta port untuk informasi proses yang diperlukan untuk dikembalikan.

Visual Studio menyediakan port yang diperlukan ke mesin debug, dan mendapatkan port ini dari pemasok port. Jika sebuah program terlampir (baik dari dalam debugger atau karena ada pengecualian yang dilemparkan yang memicu kotak dialog Just-in-Time [JIT]), pengguna diberi pilihan untuk memilih transportasi (istilah lain dari pemasok port) yang akan digunakan. Jika tidak, jika pengguna meluncurkan program dari dalam debugger, sistem proyek menentukan pemasok port yang akan digunakan. Dalam kedua peristiwa, Visual Studio membuat instans pemasok port, yang diwakili oleh antarmuka IDebugPortSupplier2 , dan meminta port baru dengan memanggil AddPort dengan antarmuka IDebugPortRequest2 . Port ini kemudian diteruskan ke mesin debug dalam satu bentuk atau bentuk lainnya.

Example

Fragmen kode ini menunjukkan cara menggunakan port yang disediakan untuk LaunchSuspended untuk mendaftarkan simpul program di ResumeProcess. Parameter yang tidak terkait langsung dengan konsep ini telah dihilangkan untuk kejelasan.

Nota

Contoh ini menggunakan port untuk meluncurkan dan melanjutkan proses dan mengasumsikan bahwa antarmuka IDebugPortEx2 diterapkan pada port. Ini bukan berarti satu-satunya cara untuk melakukan tugas-tugas ini, dan ada kemungkinan bahwa port bahkan mungkin tidak terlibat selain memiliki IDebugProgramNode2 program yang diberikan kepadanya.

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