作成日 2015.06.06
最終更新日 2015.06.06
原文
VSTA Sample Host Integration.docxは、SDKと一緒にダウンロードできます。
ホスト統合のVSTAサンプル
[VSTA Sample Host Integration]
プロジェクトをデバッグする
Debugging a Project
IDEでプロジェクトをデバッグするには、ホストは、インターフェースVSTA.IExternalDebugHostを実装している必要があります。 このインターフェースは、add-inをロードし、そして、アンロードするための、チェックポイントのデバッグについて、ホストに知らせるために、IDEを提供します。
次に示すコードは、VSTA.IExternalDebugHostのサンプル・ホストの実装を具体的に説明します。:
internal class ExternalDebugHostImpl: VSTA.IExternalDebugHost
{
private Process _loaderProcess;
private static ExternalDebugHostImpl _instance = null;
public static ExternalDebugHostImpl Instance
{
get
{
if (_instance == null)
{
_instance = new ExternalDebugHostImpl();
}
return _instance;
}
}
private Process CreateHostingProcess(string projectId)
{
Process hostingProcess = null;
// Set process properties, and create a process.
// 処理プロパティを設定し、そして、処理を作成します。
// Return the created process.
// 作成された処理を返します。
return hostingProcess;
}
//Interface methods implementation
// インターフェース・メソッドの実装
uint VSTA.IExternalDebugHost.OnBeforeDebugStarting(string projectId)
{
//Check if the projectId is valid.
// プロジェクトIDが有効かどうか、確認します。
//Create an external host process, which will host the Addin during debugging.
// デバッグの間、Addinを格納する、外部のホスト処理を作成します。
_loaderProcess = CreateHostingProcess(projectId);
//If the external debug host process is created successfully, return it
// to host.
// 外部のデバッグ・ホスト処理がうまく作成される場合、ホストに、それを返します。
if (_loaderProcess != null)
{
return (uint)_loaderProcess.Id;
}
else
{
//In case of any error, throw an exception.
// エラーの場合には、例外を投げます。
throw new InvalidProgramException();
}
}
void VSTA.IExternalDebugHost.OnDebugStarting(string projectId)
{
//Ensure the projectId is same that is being debugged.
// プロジェクトIDが、デバッグされているものと同じであることを確認します。
//Tell the external debug host process to start running the addin code.
// アドイン・コードの実行を開始するために、外部のデバッグ・ホスト処理を伝えます。
}
void VSTA.IExternalDebugHost.OnDebugStopping(string projectId)
{
//Perform the needed cleanup when debugging is stopped/completed.
// デバッグが、停止/完了するとき、必要なクリーンアップを実行します。
try
{
if (!_loaderProcess.HasExited)
{
_loaderProcess.Close();
}
if (_loaderProcess.ExitCode != 0)
{
// Perform the appropriate action.
// 適切な動作を実行します。
}
//Perform other cleanup needed to stop debugging.
// デバッグを停止するために、他のクリーンアップの実行が必要です。
}
catch
{
}
}
void VSTA.IExternalDebugHost.OnDebugAttachFailed(string projectId)
{
//Perform the needed cleanup when debugging attach fails.
// デバッグ・アタッチメントが失敗するとき、必要なクリーンアップを実行します。
try
{
if (!_loaderProcess.HasExited)
{
_loaderProcess.Close();
}
if (_loaderProcess.ExitCode != 0)
{
//Perform the appropriate action.
// 適切な動作を実行します。
}
//Perform other cleanup needed for failed attach.
// 失敗したアタッチのために必要な、他のクリーンアップを実行します。
}
catch
{
}
}
}
デバッグのために、子のセッションと対応するIDEインスタンスを適切に設定するために、ホストは、SessionManagerの作成の間、 ExternalDebugHostImplのインスタンスを渡す必要があります。
_sessionManager = VSTA.SessionManager.Create(
"MyHostApp",
new Dictionary<string, object>()
{
{ VSTA.SessionManagerOptions.Names.ExternalDebugHost, ExternalDebugHostImpl.Instance }
});
SessionManagerが、IExternalDebugHostを実装することによって作成されたあと、 ホストは、デバッグを開始、あるいは、停止するために、ProjectクラスでAPIを使用することができます。
- あなたのフォ-ムに「StartDebugging」という名前のボタンを追加します。そして、ボタンに、イベントハンドラを追加します。
- フォ-ムに、次に示す命令を追加します。:
- イベントハンドラに、次に示すコードを追加します。:
- あなたのフォ-ムに「StartDebugging」という名前のボタンを追加します。そして、ボタンに、イベントハンドラを追加します。
- イベントハンドラに、次に示すコードを追加します。:
using System.IO; using System.Reflection;
private void button4_Click(object sender, EventArgs e)
{
if (_project == null)
{
//Error handling...
// エラー処理...
return;
}
//Request the IDE to start debugging
// デバッグを起動するために、IDEを要求します
_project.StartDebugging();
}
private void button5_Click(object sender, EventArgs e)
{
if (_project == null)
{
//Error handling...
// エラー処理...
return;
}
//Request the IDE to stop debugging
// デバッグを停止するために、IDEを要求します
_project.StopDebugging();
}
備考:
- どのように、add-inの、その記号とアセンブリを読み込むか、この文書のコンパイルと実行の例は、すでに取り扱われています。 その結果、コードは、デバッグ・セクションで取り扱われません。アドインを、どのように作成するかの詳細については、例を参照してください。
- SessionManagerを作成すると、また、あなたは、ユーザーが、IDEからデバッグを開始することができるかどうか、設定することができます。:
//To enable debugging from IDE_
// IDEからデバッグを使用可能にします
_sessionManager = VSTA.SessionManager.Create(
"MyHostApp",
new Dictionary<string, object>()
{
{ VSTA.SessionManagerOptions.Names.IdeDebuggingEnabled, true}
});
//To disable debugging from IDE_
// IDEからデバッグを使用可能にします
_sessionManager = VSTA.SessionManager.Create(
"MyHostApp",
new Dictionary<string, object>()
{
{ VSTA.SessionManagerOptions.Names.IdeDebuggingEnabled, false}
});