アプリケーションのインスタンスハンドルを取得する

注意:
この文書は以前「.NETでいきまっしょい!」で公開していたものですが、公開以降メンテナンスされていません。 今や古い情報となった内容が記載されている場合があるのでご注意ください。

 現在実行しているアセンブリに含まれるモジュールからhInstanceを取得する。 モジュールのhInstanceを取得するにはMarshal.GetHINSTANCE()メソッドを使用する。
VB.NET
001
002
003
004
005
006
007
008
009
010
011
012
013
Public Shared Sub Main()

    Dim hInstance As IntPtr

    hInstance = GetHInstance()

End Sub

Public Shared Function GetHInstance() As IntPtr

    Return Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly().GetModules()(0))

End Function
C#
001
002
003
004
005
006
007
008
009
010
011
public static void Main()
{
    IntPtr hInstance;

    hInstance = GetHInstance();
}

public static IntPtr GetHInstance()
{
    return Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0] );
}