取得連接埠
如需 Visual Studio 2017 的最新文件請參閱 Visual Studio 2017 文件。
連接埠代表處理程序執行所在電腦的連接。 該電腦可能會在本機電腦或遠端電腦 (其可能可能執行非 windows 作業系統,請參閱連接埠如需詳細資訊)。
連接埠由IDebugPort2介面。 它用來取得的連接埠連接到電腦上執行的處理程序的相關資訊。
偵錯引擎需要存取連接埠,以便與連接埠登錄程式節點,並滿足要求的處理程序資訊。 例如,如果偵錯引擎會實作IDebugProgramProvider2介面的實作GetProviderProcessData方法可能會要求要傳回的必要程序資訊的連接埠。
Visual Studio 會提供必要的連接埠至偵錯引擎,它會從連接埠提供者中取得此連接埠。 如果程式附加至 (中或偵錯工具因為例外狀況擲回,而觸發的時間恰好 [JIT] 對話方塊中),使用者指定為使用所選擇的傳輸 (通訊埠供應商的另一個名稱)。 否則,如果使用者啟動的偵錯工具內的程式時,專案系統會指定要使用的連接埠供應商。 Visual Studio 在可能情況下,具現化所代表的連接埠供應商IDebugPortSupplier2介面,並要求新的連接埠,藉由呼叫下列與IDebugPortRequest2介面。 此連接埠會接著傳遞給偵錯引擎,在表單或另一個。
範例
此程式碼片段示範如何使用提供的連接埠LaunchSuspended登錄中的程式節點ResumeProcess。 為了清楚起見省略了這個概念不是直接相關聯的參數。
注意
此範例中使用的連接埠來啟動和繼續的程序,並假設IDebugPortEx2連接埠上實作介面。 這絕不是唯一的方式來執行這些工作,而且很可能的連接埠可能不甚至會涉及以外的其他程式IDebugProgramNode2提供給它。
// 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 paramters */,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);
}