如何从NT成员服务器获得NT域名服务器的计算机名??(100分)

  • 主题发起人 主题发起人 ybangl
  • 开始时间 开始时间
Y

ybangl

Unregistered / Unconfirmed
GUEST, unregistred user!
如何从NT成员服务器获得NT域名服务器的计算机名??
 
You should use API function<br><br>NET_API_STATUS NetServerEnum(<br>&nbsp; LPWSTR servername, &nbsp; &nbsp; <br>&nbsp; DWORD level, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; LPBYTE *bufptr, &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; DWORD prefmaxlen, &nbsp; &nbsp; &nbsp;<br>&nbsp; LPDWORD entriesread, &nbsp; <br>&nbsp; LPDWORD totalentries, &nbsp;<br>&nbsp; DWORD servertype, &nbsp; &nbsp; &nbsp;<br>&nbsp; LPWSTR domain, &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; LPDWORD resume_handle &nbsp;<br>);<br>
 
//此过程得到局域网中所有的域<br>procedure GetServerList(List:TStrings);<br>Type<br>&nbsp; {$H+}<br>&nbsp; PMyRec = ^MyRec;<br>&nbsp; MyRec = Record<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwScope &nbsp; &nbsp; &nbsp;: Integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwType &nbsp; &nbsp; &nbsp; &nbsp;: Integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwDisplayType : Integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwUsage &nbsp; &nbsp; &nbsp;: Integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalName &nbsp; &nbsp;: String;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RemoteName &nbsp; &nbsp;: String;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Comment &nbsp; &nbsp; &nbsp;: String;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Provider &nbsp; &nbsp; &nbsp;: String;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End;<br>&nbsp; {H-}<br>Var<br>&nbsp; NetResource : TNetResource;<br>&nbsp; TempRec &nbsp; &nbsp;: PMyRec;<br>&nbsp; Buf &nbsp; &nbsp; &nbsp; &nbsp;: Pointer;<br>&nbsp; Count,<br>&nbsp; BufSize,<br>&nbsp; Res &nbsp; &nbsp; &nbsp; &nbsp;: DWORD;<br>&nbsp; lphEnum &nbsp; &nbsp;: THandle;<br>&nbsp; p &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: PNetResourceArr;<br>&nbsp; i,<br>&nbsp; j &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: SmallInt;<br>&nbsp; NetworkTypeList : TList;<br>begin<br>// Result := False;<br><br>&nbsp; NetworkTypeList := TList.Create;<br>&nbsp; List.BeginUpdate;<br>&nbsp; List.Clear;<br>&nbsp; GetMem(Buf, 8192);<br>&nbsp; Try<br>&nbsp; &nbsp; Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, Nil,lphEnum);<br>&nbsp; &nbsp; If Res &lt;&gt; 0 Then Raise Exception(Res);<br>&nbsp; &nbsp; Count := $FFFFFFFF;<br>&nbsp; &nbsp; BufSize := 8192;<br>&nbsp; &nbsp; Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);<br>&nbsp; &nbsp; If Res = ERROR_NO_MORE_ITEMS Then Exit;<br>&nbsp; &nbsp; If (Res &lt;&gt; 0) Then Raise Exception(Res);<br>&nbsp; &nbsp; P := PNetResourceArr(Buf);<br>&nbsp; &nbsp; For I := 0 To Count - 1 Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; New(TempRec);<br>&nbsp; &nbsp; &nbsp; TempRec^.dwScope := P^.dwScope;<br>&nbsp; &nbsp; &nbsp; TempRec^.dwType := P^.dwType ;<br>&nbsp; &nbsp; &nbsp; TempRec^.dwDisplayType := P^.dwDisplayType ;<br>&nbsp; &nbsp; &nbsp; TempRec^.dwUsage := P^.dwUsage ;<br>&nbsp; &nbsp; &nbsp; TempRec^.LocalName := StrPas(P^.lpLocalName);<br>&nbsp; &nbsp; &nbsp; TempRec^.RemoteName := StrPas(P^.lpRemoteName);<br>&nbsp; &nbsp; &nbsp; TempRec^.Comment := StrPas(P^.lpComment);<br>&nbsp; &nbsp; &nbsp; TempRec^.Provider := StrPas(P^.lpProvider);<br>&nbsp; &nbsp; &nbsp; NetworkTypeList.Add(TempRec);<br>&nbsp; &nbsp; &nbsp; Inc(P);<br>&nbsp; &nbsp; End;<br>&nbsp; &nbsp; Res := WNetCloseEnum(lphEnum);<br>&nbsp; &nbsp; If Res &lt;&gt; 0 Then Raise Exception(Res);<br>&nbsp; &nbsp; For J := 0 To NetworkTypeList.Count-1 Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; TempRec := NetworkTypeList.Items[J];<br>&nbsp; &nbsp; &nbsp; NetResource := TNetResource(TempRec^);<br>&nbsp; &nbsp; &nbsp; Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);<br>&nbsp; &nbsp; &nbsp; If Res &lt;&gt; 0 Then Raise Exception(Res);<br>&nbsp; &nbsp; &nbsp; While true Do<br>&nbsp; &nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Count := $FFFFFFFF;<br>&nbsp; &nbsp; &nbsp; &nbsp; BufSize := 8192;<br>&nbsp; &nbsp; &nbsp; &nbsp; Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);<br>&nbsp; &nbsp; &nbsp; &nbsp; If Res = ERROR_NO_MORE_ITEMS Then Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; If (Res &lt;&gt; 0) Then Raise Exception(Res);<br>&nbsp; &nbsp; &nbsp; &nbsp; P := PNetResourceArr(Buf);<br>&nbsp; &nbsp; &nbsp; &nbsp; For I := 0 To Count - 1 Do<br>&nbsp; &nbsp; &nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List.Add(P^.lpRemoteName);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(P);<br>&nbsp; &nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; End;<br>&nbsp; &nbsp; Res := WNetCloseEnum(lphEnum);<br>&nbsp; &nbsp; If Res &lt;&gt; 0 Then Raise Exception(Res);<br>&nbsp; &nbsp; //Result := True;<br>&nbsp; &nbsp; Finally<br>&nbsp; &nbsp; &nbsp; FreeMem(Buf);<br>&nbsp; &nbsp; &nbsp; NetworkTypeList.Destroy;<br>&nbsp; End;<br>&nbsp; List.EndUpdate;<br>end;<br><br>//此过程得到某个域中所有的计算机名<br>procedure GetUserList(fServer:string;List:TStrings);<br>Var<br>&nbsp; NetResource : TNetResource;<br>&nbsp; Buf &nbsp; &nbsp; &nbsp; &nbsp;: Pointer;<br>&nbsp; Count,<br>&nbsp; BufSize,<br>&nbsp; Res &nbsp; &nbsp; &nbsp; &nbsp;: DWord;<br>&nbsp; Ind &nbsp; &nbsp; &nbsp; &nbsp;: Integer;<br>&nbsp; lphEnum &nbsp; &nbsp;: THandle;<br>&nbsp; Temp &nbsp; &nbsp; &nbsp; &nbsp;: PNetResourceArr;<br>Begin<br><br><br>&nbsp; List.Clear;<br>&nbsp; GetMem(Buf, 8192);<br>&nbsp; Try<br>&nbsp; &nbsp; FillChar(NetResource, SizeOf(NetResource), 0);<br>&nbsp; &nbsp; NetResource.lpRemoteName := @fServer[1];<br>&nbsp; &nbsp; NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;<br>&nbsp; &nbsp; NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;<br>&nbsp; &nbsp; NetResource.dwScope := RESOURCETYPE_DISK;<br>&nbsp; &nbsp; Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);<br>&nbsp; &nbsp; If Res &lt;&gt; 0 Then Exit;<br>&nbsp; &nbsp; While True Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; Count := $FFFFFFFF;<br>&nbsp; &nbsp; &nbsp; BufSize := 8192;<br>&nbsp; &nbsp; &nbsp; Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);<br>&nbsp; &nbsp; &nbsp; If Res = ERROR_NO_MORE_ITEMS Then Exit;<br>&nbsp; &nbsp; &nbsp; If (Res &lt;&gt; 0) then Exit;<br>&nbsp; &nbsp; &nbsp; Temp := PNetResourceArr(Buf);<br>&nbsp; &nbsp; &nbsp; For Ind := 0 to Count - 1 do<br>&nbsp; &nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; List.Add(Temp^.lpRemoteName + 2); { Add all the network usernames to List StringList }<br>&nbsp; &nbsp; &nbsp; &nbsp; Inc(Temp);<br>&nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; End;<br>&nbsp; &nbsp; Res := WNetCloseEnum(lphEnum);<br>&nbsp; &nbsp; If Res &lt;&gt; 0 Then Raise Exception(Res);<br>// &nbsp; &nbsp;Result := True;<br>&nbsp; Finally<br>&nbsp; &nbsp; FreeMem(Buf);<br>&nbsp; End;<br>End;<br><br>在NT4、D5下测试通过。<br>
 
怎样判断谁是主域控制器
 
我用API函数NetServerEnum,如果只给域名则返回错误6118,如果服务器域名都给则返回错误2351<br>我的代码如下:<br>//获得指定域中可见的指定类型的所有服务器<br>Function NetServerEnum(ServerName:LPTSTR;Level:DWORD;BufPtr:SERVER_INFO_101_Array;PrefMaxLen:DWORD;EntriesRead:LPDWORD;Totalentries:LPDWORD;ServerType:DWORD;Domain:LPTSTR;Resume_handle:LPDWORD):integer; stdcall; external 'netapi32.dll';<br>//获得域控制器<br>Function GetDomain_Ctrl_ComputerName(Domain:string):string;<br>var<br>&nbsp; V_ServerName:LPTSTR;<br>&nbsp; V_Level:DWORD;<br>&nbsp; V_BufPtr:SERVER_INFO_101_Array;<br>&nbsp; V_PrefMaxLen:DWORD;<br>&nbsp; V_EntriesRead:LPDWORD;<br>&nbsp; V_Totalentries:LPDWORD;<br>&nbsp; V_ServerType:DWORD;<br>&nbsp; V_Domain:LPTSTR;<br>&nbsp; V_Resume_handle:LPDWORD;<br>&nbsp; TempResult:DWORD;<br>begin<br>&nbsp; V_ServerName:='ybl';<br>&nbsp; V_Level:=101;<br>&nbsp; SetLength(V_BufPtr,20);<br>&nbsp; V_PrefMaxLen :=1024;<br>&nbsp; new(V_EntriesRead);<br>&nbsp; V_EntriesRead^:=1;<br>&nbsp; new(V_Totalentries);<br>&nbsp; V_Totalentries^:=1;<br>&nbsp; V_ServerType :=$00000008;<br>&nbsp; V_Domain :=Pchar(Domain);<br>&nbsp; new(V_Resume_handle);<br>&nbsp; V_Resume_handle^ :=0;<br>&nbsp; TempResult:=NetServerEnum(V_ServerName,V_Level,V_BufPtr,V_PrefMaxLen,V_EntriesRead,V_Totalentries,V_ServerType,V_Domain,V_Resume_handle);<br>&nbsp; if TempResult=NO_ERROR then<br>&nbsp; begin<br>&nbsp; &nbsp; TempResult:=NetServerEnum(V_ServerName,V_Level,V_BufPtr,V_PrefMaxLen,V_EntriesRead,V_Totalentries,V_ServerType,V_Domain,V_Resume_handle);<br>&nbsp; &nbsp; GetDomain_Ctrl_ComputerName :=V_BufPtr[0].sv101_name;<br>&nbsp; &nbsp; //Application.MessageBox(pchar(inttostr(TempResult)),'dsa',mb_ok);<br>&nbsp; end<br>end;<br><br>
 
接受答案了.
 
后退
顶部