前回からのつづきです。
あれやこれや、調べていたら、ほぼそのものズバリな手がかりを見つけました。
これ→「VisualStudio2005でインストーラを作成したアプリケーション~」
リンク先にある、「カスタム動作」と「Installerクラス」を使うと、どうやら目的の処理ができそうです。
結局のところプログラムを書いて、インストーラーから呼び出して処理するってことですね。
しかし、この方法、.netが前提になるので、.net Frameworkのインストールから始まってしまうケースもありますね。
インストールするプログラム本体は通常のアンマネージドなバイナリなので、本来必要ないけど、インストーラーの都合でFrameworkが要るってことになる。なんだか不思議ですね。
カスタム動作については、ググると、ここ→「カスタム動作エディタの使い方」のサイトが大変参考になります。
以下、上記のサイトを参考に、実際に処理を追加して、インストールとアンインストール時に環境変数を書き換えた例です。
クラスライブラリを追加する
SetUpと同じソリューションへクラスライブラリのプロジェクトを追加する。
ここは、上記サイトの例に倣って、CustomActionという名前でクラスライブラリを作成します。

そして、作成したプロジェクトにインストーラークラスを追加する。

しかし、こいうテンプレートがあるとは思わなかった。便利なものですね。
SetUpへクラスライブラリ(DLL)を追加する
上記で追加したクラスライブラリを、どうやら一緒に配布しないといけないらしいので、SetUpの「ファイルシステム」へ追加します。
配布先のフォルダを選んで、右ボタンから[追加]-[プロジェクト出力]を選択。

先ほど作ったプロジェクト「CustomAction」の「プライマリ出力」を指定します。

メソッドを実装する
先ほどクラスライブラリに追加したInstallクラスに、インストール、アンインストールで環境変数を書き換えるためのメソッドを実装します。
以下、実際のコードです。参考にしたサイトの例から、インストール、アンインストールに必要なメソッドだけ処理を追加しています。
注:後述するカスタム動作の設定を行わないと、ちゃんと動かないです。
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
//System.Diagnostics.Debugger.Launch(); // .net 4.0
////System.Diagnostics.Debugger.Break(); // .net 3.5?
//System.Windows.Forms.MessageBox.Show(“Install”);
// 環境変数「path」を編集
string currentPath;
currentPath = System.Environment.GetEnvironmentVariable(“path”, System.EnvironmentVariableTarget.User);
string installPath = this.Context.Parameters[“InstallPath”];
string path = installPath + @”\bin;”;
string raypath = “.;” + installPath + @”\lib;”;
if (currentPath == null)
{
currentPath = path;
}
else if (currentPath.EndsWith(“;”))
{
currentPath += path;
}
else
{
currentPath += “;” + path;
}
// 環境変数を設定する
System.Environment.SetEnvironmentVariable(“path”, currentPath, System.EnvironmentVariableTarget.User);
System.Environment.SetEnvironmentVariable(“raypath”, raypath, System.EnvironmentVariableTarget.User);
}
//public override void Commit(System.Collections.IDictionary savedState)
//{
// base.Commit(savedState);
// System.Windows.Forms.MessageBox.Show(“Commit”);
//}
//public override void Rollback(System.Collections.IDictionary savedState)
//{
// base.Rollback(savedState);
// System.Windows.Forms.MessageBox.Show(“Rollback”);
//}
public override void Uninstall(System.Collections.IDictionary savedState)
{
base.Uninstall(savedState);
//System.Diagnostics.Debugger.Launch(); // .net 4.0
////System.Diagnostics.Debugger.Break(); // .net 3.5?
// 環境変数「path」を編集
string currentPath;
currentPath = System.Environment.GetEnvironmentVariable(“path”, System.EnvironmentVariableTarget.User);
string installPath = this.Context.Parameters[“InstallPath”];
installPath += @”\bin;”;
currentPath = currentPath.Replace(installPath, “”);
// 環境変数を削除する
System.Environment.SetEnvironmentVariable(“path”, currentPath, System.EnvironmentVariableTarget.User);
System.Environment.SetEnvironmentVariable(“raypath”, “”, System.EnvironmentVariableTarget.User);
}
カスタム動作の指定
上記のメソッドで処理を行うためにカスタム動作の指定を行います。
まずは「カスタム動作エディター」を表示します。メニューから[表示]-[エディター]-[カスタム動作]を選択。

「インストール」の項目で右ボタンをクリックして「カスタム動作の追加」を選択。

配布先に追加した、「CustomActionのプライマリ出力」を選択する。

そして、CustomActionDataの設定を追加します。これ、上記のソースコードにインストーラーから値を引き渡すための設定です。
追加したカスタム動作のCustomActionDataプロパティに以下の値を設定します。
/InstallPath=”[WindowsVolume][ProductName]”
実際に追加している画面を見た方がわかりやすいですね。

同じように「アンインストール」の項目もプライマリ出力を選択しておきます。
ビルドして実行
後はビルドして、インストールとアンインストールが動けばOK。
インストールしてみると、無事に環境変数が設定されいる。

そしてアンインストールを実行。これも無事に環境変数が元通りになっている。

ようやく、ここまでできた。しかし、やり方あってんのかな?動いているけど?
でも、もう一息。
ピンバック: Installer Project(Setup Project)を利用したインストーラ作成方法 | 備忘録的ななにか