ドメイン名の取得          <TOP>


ドメイン名を取得します。Windows9xは対象外、WindowsNT以降

LookupAccountName システム名とアカウントを入力として受け取る

 

WindowsXP(Valuestar)での取得例(左:ユーザ名を入れない場合)    中:関係のない名前を入れた場合    右:当機のユーザ名を入れた場合

  

Windows2000(Hitachi)での例

  

 

'================================================================
'= ドメイン名の取得
'= WindowsNT/2000以降
'=    (LookupAccountName.bas)
'================================================================
#include "Windows.bi"

' システム名とアカウントを入力として受け取る。そのアカウントのセキュリティ識別子(SID)と、アカウントが見つかったドメインの名前を取得
Declare Function Api_LookupAccountName& Lib "advapi32" Alias "LookupAccountNameA" (ByVal lpSystemName$, ByVal lpAccountName$, Sid As Byte, cbSid&, ByVal DomainName$, cbDomainName&, peUse&)

Var Shared Edit1 As Object
Var Shared Text(4) As Object
Var Shared Button1 As Object

Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14
For i = 0 To 4
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1)))
    Text(i).SetFontSize 14
Next
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Function ValidateUser(sAccountName As String, sDomainName As String, sSystemName As String) As Integer
Function ValidateUser(sAccountName As String, sDomainName As String, sSystemName As String) As Integer
    Var success As Long
    Var cbSid As Long
    Var cbDomainName As Long
    Var peUse As Long

    sDomainName = Chr$(0)
    cbDomainName = 0
   
    If Len(sSystemName) = 0 Then
        sSystemName = Chr$(0)
    End If
   
    success = Api_LookupAccountName(sSystemName, sAccountName, 0, cbSid, sDomainName, cbDomainName, peUse)
    If cbSid = 0 Then Exit Function

    Var bSID(cbSid - 1) As Byte

    If (success = 0) And (cbSid > 0) Then
        sDomainName = Space$(cbDomainName)
        success = Api_LookupAccountName(sSystemName, sAccountName, bSID(0), cbSid, sDomainName, cbDomainName, peUse)
        If success > 0 Then
            If cbDomainName > 0 Then
                sDomainName = Left$(sDomainName, cbDomainName)
            End If
        End If
    End If
      
    ValidateUser = success
End Function

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var sAccount As String
    Var sSystem As String
    Var sDomain As String
    Var sValid As String
    Var vu As Integer

    sAccount = Edit1.GetWindowText
     
    sSystem = ""
    sDomain = ""

    vu = ValidateUser(sAccount, sDomain, sSystem)
    Select Case vu
        Case 1
            sValid = "ユーザ確認済"
        Case 0
            sValid = "ユーザ未確認"
    End Select

    Text(3).SetWindowText sValid
    Text(4).SetWindowText sDomain
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End