|











|
 |
Domain名、Workgroup名の取得
|
開発環境: Visual Studio 2003
|
|
|
|
|
|
BBSのNo.248 でPCのドメイン名,もしくはワークグループ名の取得方法がありましたので、まとめておききます。

|
|
|
(1) MSDN
System.Environment クラス (2) MSDN
SystemInformation クラス (3) MSDN
NetWkstaGetInfo (4) アンマネージコードとの相互運用 (5) クラス、構造体、および共用体のマーシャリング
|
|
|
| 方法 |
| (1) | System.Environment.UserDomainName |
| (2) | System.Windows.Forms.SystemInformation.UserDomainName |
| 注意 |
| Workgroup で使用している場合のユーザドメイン名は コンピュータ名と同じになります。 |
|
|
|
BBSのNo.248 でWORKGROUP名の取得ができないということで、ちょっとしらべてみましたが、やはりプラットフォームSDKを呼び出さないとだめですね。
| 注意 |
NetWkstaGetInfo は、NT系のみサポートされています。95・98・Me系はサポートされていません。 ただし、ネットワーク経由で95・98・Me系を見れるかもしれません。 第1引数を null ではなくて、サーバ名を指定してみてね。 |
| 例によってエラーコードは省略しています。 |
| プラットフォームSDKのプロトタイプ宣言部分 |
[StructLayout(LayoutKind.Sequential, Pack=4)] struct WKSTA_INFO_100 { public int wki100_platform_id; public IntPtr wki100_computername; public IntPtr wki100_langroup; public int wki100_ver_major; public int wki100_ver_minor; }
[DllImport("Netapi32.dll")] // NET_API_STATUS NetWkstaGetInfo( // LPWSTR servername, // DWORD level, // LPBYTE* bufptr // ); static extern int NetWkstaGetInfo(string servername, int level, out
IntPtr bufptr);
// NET_API_STATUS NetApiBufferFree( // LPVOID Buffer // ); [DllImport("Netapi32.dll")] private static extern int NetApiBufferFree(IntPtr bufptr);
|
| 呼び出し例 |
IntPtr bufptr; NetWkstaGetInfo( null, 100, out bufptr); WKSTA_INFO_100 s = (WKSTA_INFO_100)Marshal.PtrToStructure(bufptr,
typeof(WKSTA_INFO_100)); string workgroup = Marshal.PtrToStringUni(s.wki100_langroup); string compname = Marshal.PtrToStringUni(s.wki100_computername); Console.WriteLine("Workgroup:" + workgroup); Console.WriteLine("Compname:" + compname); NetApiBufferFree(bufptr); |
|
|
|
| テスト環境 |
| 2003/12/7 | Ver.1.0 Windows XP Professional WORKGROUP
でローカルマシンに対して動作を確認しました。 それ以外は確認していません。 |
| Workgroup.cs |
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ConsoleApplication6
{
class Class1
{
[StructLayout(LayoutKind.Sequential, Pack=4)]
struct WKSTA_INFO_100
{
public int wki100_platform_id;
public IntPtr wki100_computername;
public IntPtr wki100_langroup;
public int wki100_ver_major;
public int wki100_ver_minor;
}
[DllImport("Netapi32.dll")]
// NET_API_STATUS NetWkstaGetInfo(
// LPWSTR servername,
// DWORD level,
// LPBYTE* bufptr
// );
static extern int NetWkstaGetInfo(string servername, int level, out IntPtr bufptr);
// NET_API_STATUS NetApiBufferFree(
// LPVOID Buffer
// );
[DllImport("Netapi32.dll")]
private static extern int NetApiBufferFree(IntPtr bufptr);
[STAThread]
static void Main(string[] args)
{
Test_SystemInformation();
Test_NetWkstaGetInfo();
Console.ReadLine();
}
static void Test_SystemInformation()
{
Console.WriteLine("Environment.UserDomainName:" + Environment.UserDomainName);
Console.WriteLine("SystemInformation.UserDomainName:" + SystemInformation.UserDomainName);
Console.WriteLine("SystemInformation.ComputerName:" + SystemInformation.ComputerName);
Console.WriteLine("----------------------");
}
static void Test_NetWkstaGetInfo()
{
IntPtr bufptr;
NetWkstaGetInfo( null, 100, out bufptr);
WKSTA_INFO_100 s = (WKSTA_INFO_100)Marshal.PtrToStructure(bufptr, typeof(WKSTA_INFO_100));
string workgroup = Marshal.PtrToStringUni(s.wki100_langroup);
string compname = Marshal.PtrToStringUni(s.wki100_computername);
Console.WriteLine("Workgroup:" + workgroup);
Console.WriteLine("Compname:" + compname);
NetApiBufferFree(bufptr);
}
}
}
|
|
|
|
2003/12/7 初版作成
|
|