Share via


ActiveDir のサンプル

このサンプルでは、CoInitialize メソッドを呼び出すアンマネージ メソッドにマネージ オブジェクトからデータを渡す場合に、そのマネージ オブジェクトの既定のアパートメント設定を調整する方法を示します。 CoInitialize メソッドは、シングルスレッド アパートメント (STA) の中で COM ライブラリを初期化します。 既定では、C# クライアントはマルチスレッド アパートメント (MTA) の中で初期化されます。 Visual Basic 2005 クライアントは STA オブジェクトとして初期化され、調整は不要です。

ActiveDir のサンプルで使用するアンマネージ メソッドとその関数宣言を次に示します。

  • Dsuiext.dll からエクスポートされる DsBrowseForContainer

    int DsBrowseForContainer(PDSBROWSEINFO pInfo);
    

C# の場合は、STAThreadAttribute 属性によって STA アパートメントが作成されます。 STA は Visual Basic 2005 クライアントに関する既定のアパートメント設定なので、属性は必要ありません。 Marshal.SizeOf メソッドは、アンマネージ構造体のサイズを動的に計算します。

プロトタイプの宣言

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Structure DsBrowseInfo
    Public Const MAX_PATH As Integer = 256

    Public Size As Integer
    Public OwnerHandle As IntPtr
    Public Caption As String
    Public Title As String
    Public Root As String
    Public Path As String
    Public PathSize As Integer
    Public Flags As Integer
    Public Callback As IntPtr
    Public Param As Integer
    Public ReturnFormat As Integer
    Public UserName As String
    Public Password As String
    Public ObjectClass As String
    Public ObjectClassSize As Integer
End Structure

Public Class LibWrap
    ' Declares a managed prototype for the unmanaged function.
    Declare Unicode Function DsBrowseForContainerW Lib "dsuiext.dll" ( _
        ByRef info As DSBrowseInfo ) As Integer

    Public Const DSBI_ENTIREDIRECTORY As Integer = &H90000
    Public Const ADS_FORMAT_WINDOWS As Integer = 1
    Public Enum BrowseStatus
        BrowseError = -1
        BrowseOk = 1
        BrowseCancel = 2
    End Enum
End Class
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct DsBrowseInfo
{
    public const int MAX_PATH = 256;

    public int       Size;
    public IntPtr    OwnerHandle;
    public string    Caption;
    public string    Title;
    public string    Root;
    public string    Path;
    public int       PathSize;
    public int       Flags;
    public IntPtr    Callback;
    public int       Param;
    public int       ReturnFormat;
    public string    UserName;
    public string    Password;
    public string    ObjectClass;
    public int       ObjectClassSize;
}

public class LibWrap
{
    // Declares a managed prototype for the unmanaged function.
    [DllImport("dsuiext.dll", CharSet=CharSet.Unicode)]
    public static extern int DsBrowseForContainerW(ref DsBrowseInfo info);

    public const int DSBI_ENTIREDIRECTORY = 0x00090000;
    public const int ADS_FORMAT_WINDOWS = 1;
    public enum BrowseStatus
    {
        BrowseError = -1,
        BrowseOk = 1,
        BrowseCancel = 2
    }
}
[StructLayout(LayoutKind::Sequential, CharSet=CharSet::Unicode)]
public value struct DsBrowseInfo
{
public:
    static int MAX_PATH = 256;

    int        Size;
    IntPtr     OwnerHandle;
    String^    Caption;
    String^    Title;
    String^    Root;
    String^    Path;
    int        PathSize;
    int        Flags;
    IntPtr     Callback;
    int        Param;
    int        ReturnFormat;
    String^    UserName;
    String^    Password;
    String^    ObjectClass;
    int        ObjectClassSize;
};

public ref class LibWrap
{
public:
    // Declares a managed prototype for the unmanaged function.
    [DllImport("dsuiext.dll", CharSet=CharSet::Unicode)]
    static int DsBrowseForContainerW(DsBrowseInfo^ info);

    static int DSBI_ENTIREDIRECTORY = 0x00090000;
    static int ADS_FORMAT_WINDOWS = 1;
    enum class BrowseStatus
    {
        BrowseError = -1,
        BrowseOk = 1,
        BrowseCancel = 2
    };
};

関数の呼び出し

Public Class App
    ' Must be marked as STA because the default is MTA.
    ' DsBrowseForContainerW calls CoInitialize, which initializes the
    ' COM library as STA.
    <STAThread> _
    Public Shared Sub Main()
        Dim dsbi As New DsBrowseInfo()

        dsbi.Size = Marshal.SizeOf(GetType(DsBrowseInfo))
        dsbi.PathSize = DsBrowseInfo.MAX_PATH
        dsbi.Caption = "Container Selection Example"
        dsbi.Title = "Select a container from the list."
        dsbi.ReturnFormat = LibWrap.ADS_FORMAT_WINDOWS
        dsbi.Flags = LibWrap.DSBI_ENTIREDIRECTORY
        dsbi.Root = "LDAP:"
        dsbi.Path = New String(New Char(DsBrowseInfo.MAX_PATH){})
        ' Initialize remaining members...

        Dim status As Integer = LibWrap.DsBrowseForContainerW(dsbi)
        if CType(status, LibWrap.BrowseStatus) = LibWrap.BrowseStatus.BrowseOk Then
            Console.WriteLine(dsbi.Path)
        Else
            Console.WriteLine("No path returned.")
        End If
    End Sub
End Class
class App
{
    // Must be marked as STA because the default is MTA.
    // DsBrowseForContainerW calls CoInitialize, which initializes the
    // COM library as STA.
    [STAThread]
    public static void Main()
    {
        DsBrowseInfo dsbi = new DsBrowseInfo();

        dsbi.Size = Marshal.SizeOf(typeof(DsBrowseInfo));
        dsbi.PathSize = DsBrowseInfo.MAX_PATH;
        dsbi.Caption = "Container Selection Example";
        dsbi.Title = "Select a container from the list.";
        dsbi.ReturnFormat = LibWrap.ADS_FORMAT_WINDOWS;
        dsbi.Flags = LibWrap.DSBI_ENTIREDIRECTORY;
        dsbi.Root = "LDAP:";
        dsbi.Path = new string(new char[DsBrowseInfo.MAX_PATH]);
        // Initialize remaining members...

        int status = LibWrap.DsBrowseForContainerW(ref dsbi);
        if ((LibWrap.BrowseStatus)status == LibWrap.BrowseStatus.BrowseOk)
        {
            Console.WriteLine(dsbi.Path);
        }
        else
        {
            Console.WriteLine("No path returned.");
        }
    }
}
public ref class App
{
public:
    // Must be marked as STA because the default is MTA.
    // DsBrowseForContainerW calls CoInitialize, which initializes the
    // COM library as STA.
    [STAThread]
    static void Main()
    {
        DsBrowseInfo dsbi;

        // Initialize the fields.
        dsbi.Size = Marshal::SizeOf(DsBrowseInfo::typeid);
        dsbi.PathSize = DsBrowseInfo::MAX_PATH;
        dsbi.Caption = "Container Selection Example";
        dsbi.Title = "Select a container from the list.";
        dsbi.ReturnFormat = LibWrap::ADS_FORMAT_WINDOWS;
        dsbi.Flags = LibWrap::DSBI_ENTIREDIRECTORY;
        dsbi.Root = "LDAP:";
        dsbi.Path = gcnew String(gcnew array<Char>(DsBrowseInfo::MAX_PATH));
        // Initialize remaining members...

        int status = LibWrap::DsBrowseForContainerW(dsbi);
        if ((LibWrap::BrowseStatus)status == LibWrap::BrowseStatus::BrowseOk)
        {
            Console::WriteLine(dsbi.Path);
        }
        else
        {
            Console::WriteLine("No path returned.");
        }
    }
};

参照

概念

各種のマーシャリングのサンプル

プラットフォーム呼び出しのデータ型

マネージ コードでのプロトタイプの作成