Is Computer Joined to a Domain - Programmatically Check using Delphi
If a computer is a part of a network it can either be part of a workgroup or a domain.
If you need to programmatically check if a machine running your application is a part of a domain you can exploit functions found in the netapi32.dll. The trick is in using the NetRenameMachineInDomain function which can be used to change the name of a computer in a domain.
If the function fails with the return value of "NERR_SetupNotJoined" when supplying it with "nil" values - a machine is NOT a part of a domain.
// returns true if the machine running this code is in a domain
function IsInDomain: boolean;
type
TNetRenameMachineInDomain = function(lpServer, MachineName, lpAccount, Password: PWideChar; Options: Longint): LongInt stdcall;
var
ResultCode: Integer;
NetRenameMachineInDomain: TNetRenameMachineInDomain;
NetAPIHandle: THandle;
const //ref : lmerr.h
NERR_BASE = 2100;
// This machine is already joined to a domain.
NERR_SetupAlreadyJoined = (NERR_BASE + 591) ;
// This machine is not currently joined to a domain.
NERR_SetupNotJoined = (NERR_BASE + 592) ;
// This machine is a domain controller and cannot be unjoined from a domain.
NERR_SetupDomainController = (NERR_BASE + 593) ;
// The destination domain controller does not support
// creating machine accounts in OUs.
NERR_DefaultJoinRequired = (NERR_BASE + 594) ;
// The specified workgroup name is invalid.
NERR_InvalidWorkgroupName = (NERR_BASE + 595) ;
// The specified computer name is incompatible with the
// default language used on the domain controller.
NERR_NameUsesIncompatibleCodePage = (NERR_BASE + 596) ;
// The specified computer account could not be found.
// Contact an administrator to verify the account is in the domain.
// If the account has been deleted unjoin, reboot, and rejoin the domain.
NERR_ComputerAccountNotFound = (NERR_BASE + 597) ;
// This version of Windows cannot be joined to a domain.
NERR_PersonalSku = (NERR_BASE + 598) ;
// An attempt to resolve the DNS name of a DC in the domain being joined has failed.
// Please verify this client is configured to reach a DNS server that can
// resolve DNS names in the target domain.
NERR_SetupCheckDNSConfig = (NERR_BASE + 599) ;
begin
try
NetAPIHandle := LoadLibrary(PChar('netapi32.dll')) ;
@NetRenameMachineInDomain := GetProcAddress(NetAPIHandle, PChar('NetRenameMachineInDomain')) ;
ResultCode := NetRenameMachineInDomain(nil, nil, nil, nil, 0) ;
FreeLibrary(NetAPIHandle) ;
finally
end;
Result := ResultCode <> NERR_SetupNotJoined;
end; (*IsInDomain*)
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment