1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117:
| unit NetworkUser;
interface
uses Windows;
type ATStrings = array of string; function GetUser(const ServerName: string): string;
implementation
function GetUser(const ServerName: string): string; const MAX_NAME_STRING = 1024; var userName, domainName: array [0..MAX_NAME_STRING] of Char; subKeyName: array [0..MAX_PATH] of Char; NIL_HANDLE: Integer ABSOLUTE 0; ArryList: ATStrings; subKeyNameSize: DWORD; index: DWORD; userNameSize: DWORD; domainNameSize: DWORD; lastWriteTime: FILETIME; usersKey: HKEY; sid: PSID; sidType: SID_NAME_USE; authority: SID_IDENTIFIER_AUTHORITY; subAuthorityCount: BYTE; authorityVal: DWORD; revision: DWORD; subAuthorityVal: ARRAY[0..7] OF DWORD;
function getvals(s: string): integer; var i, j, k, l: integer; tmp: string; begin delete(s, 1, 2); j := pos('-', s); tmp := copy(s, 1, j - 1); val(tmp, revision, k); delete(s, 1, j); j := pos('-', s); tmp := copy(s, 1, j - 1); val('$' + tmp, authorityVal, k); delete(s, 1, j); i := 2; s := s + '-'; for l := 0 to 7 do begin j := pos('-', s); if j > 0 then begin tmp := copy(s, 1, j - 1); val(tmp, subAuthorityVal[l], k); delete(s, 1, j); inc(i); end else break; end; result := i; end;
begin setlength(ArryList, 0); revision := 0; authorityVal := 0; FillChar(subAuthorityVal, SizeOf(subAuthorityVal), #0); FillChar(userName, SizeOf(userName), #0); FillChar(domainName, SizeOf(domainName), #0); FillChar(subKeyName, SizeOf(subKeyName), #0); if ServerName <> '' then begin usersKey := 0; if (RegConnectRegistry(pchar(ServerName), HKEY_USERS, usersKey) <> 0) then Exit; end else begin if (RegOpenKey(HKEY_USERS, NIL, usersKey) <> ERROR_SUCCESS) then Exit; end; index := 0; subKeyNameSize := SizeOf(subKeyName); while (RegEnumKeyEx(usersKey, index, subKeyName, subKeyNameSize, NIL, NIL, NIL, @lastWriteTime) = ERROR_SUCCESS) do begin if (lstrcmpi(subKeyName, '.default') <> 0) and (Pos('Classes', STRING(subKeyName)) = 0) then begin subAuthorityCount := getvals(subKeyName); if (subAuthorityCount >= 3) then begin subAuthorityCount := subAuthorityCount - 2; if (subAuthorityCount < 2) then subAuthorityCount := 2; authority.Value[5] := PByte(@authorityVal)^; authority.Value[4] := PByte(DWORD(@authorityVal) + 1)^; authority.Value[3] := PByte(DWORD(@authorityVal) + 2)^; authority.Value[2] := PByte(DWORD(@authorityVal) + 3)^; authority.Value[1] := 0; authority.Value[0] := 0; sid := NIL; userNameSize := MAX_NAME_STRING; domainNameSize := MAX_NAME_STRING; if AllocateAndInitializeSid(authority, subAuthorityCount, subAuthorityVal[0], subAuthorityVal[1], subAuthorityVal[2], subAuthorityVal[3], subAuthorityVal[4], subAuthorityVal[5], subAuthorityVal[6], subAuthorityVal[7], sid) then begin if LookupAccountSid(Pchar(ServerName), sid, userName, userNameSize, domainName, domainNameSize, sidType) then begin setlength(ArryList, length(ArryList) + 1); ArryList[length(ArryList) - 1] := string(domainName) + '\' + string(userName); result := string(userName); end; end; if Assigned(sid) then FreeSid(sid); end; end; subKeyNameSize := SizeOf(subKeyName); Inc(index); end; RegCloseKey(usersKey); end;
end. |