ポートを取得する
ポートは、プロセスが実行されているコンピューターへの接続を表します。 このコンピューターは、ローカル コンピューターまたはリモート コンピューターである可能性があります (Windows ベース以外のオペレーティング システムを実行している可能性があります。詳細については、「ポート」を参照してください)。
ポートは、IDebugPort2 インターフェイスによって表されます。 これは、ポートが接続されているコンピューターで実行中のプロセスに関する情報を取得するために使用されます。
デバッグ エンジンでは、プログラム ノードをポートに登録し、プロセス情報の要求を満たすために、ポートにアクセスできる必要があります。 たとえば、デバッグ エンジンに IDebugProgramProvider2 インターフェイスが実装されている場合は、GetProviderProcessData メソッドの実装で、必要なプロセス情報を返すようにポートに要求できます。
Visual Studio によって、必要なポートがデバッグ エンジンに提供され、ポート サプライヤーからこのポートが取得されます。 (デバッガー内から、または Just-in-time (JIT) ダイアログ ボックスをトリガーする例外がスローされたために) プログラムがアタッチされた場合、ユーザーは使用するトランスポート (ポート サプライヤーの別名) を選択できます。 それ以外の場合 (ユーザーがデバッガー内からプログラムを起動した場合)、使用するポート サプライヤーはプロジェクト システムによって指定されます。 どちらの場合も、Visual Studio では、IDebugPortSupplier2 インターフェイスによって表されるポート サプライヤーをインスタンス化し、新しいポートを要求するために IDebugPortRequest2 インターフェイスを使用して AddPort を呼び出します。 このポートは、その後、さまざまな形式でデバッグ エンジンに渡されます。
例
このコード フラグメントでは、LaunchSuspended に指定されたポートを使用して、ResumeProcess でプログラム ノードを登録する方法を示します。 この概念に直接関係のないパラメーターは、わかりやすくするために省略されています。
Note
この例では、ポートを使用してプロセスを起動および再開し、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 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);
}