Rev 81 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 81 | Rev 82 | ||
---|---|---|---|
Line 3... | Line 3... | ||
3 | (* *) |
3 | (* *) |
4 | (* Copyright (c) 2004 Shorter Path Software *) |
4 | (* Copyright (c) 2004 Shorter Path Software *) |
5 | (* http://www.shorterpath.com *) |
5 | (* http://www.shorterpath.com *) |
6 | (******************************************************************************) |
6 | (******************************************************************************) |
7 | 7 | ||
- | 8 | (*** Modified and extended by ViaThinkSoft ***) |
|
8 | 9 | ||
9 | { |
10 | { |
10 | SID is a data structure of variable length that identifies user, group, |
11 | SID is a data structure of variable length that identifies user, group, |
11 | and computer accounts. |
12 | and computer accounts. |
12 | Every account on a network is issued a unique SID when the account is first created. |
13 | Every account on a network is issued a unique SID when the account is first created. |
Line 21... | Line 22... | ||
21 | 22 | ||
22 | uses |
23 | uses |
23 | Windows, SysUtils; |
24 | Windows, SysUtils; |
24 | 25 | ||
25 | function GetCurrentUserSid: string; |
26 | function GetCurrentUserSid: string; |
- | 27 | function GetComputerSID: string; |
|
26 | 28 | ||
27 | implementation |
29 | implementation |
28 | 30 | ||
29 | const |
31 | const |
30 | HEAP_ZERO_MEMORY = $00000008; |
32 | HEAP_ZERO_MEMORY = $00000008; |
Line 153... | Line 155... | ||
153 | Result := szSid; |
155 | Result := szSid; |
154 | CloseHandle(hAccessToken); |
156 | CloseHandle(hAccessToken); |
155 | end; |
157 | end; |
156 | end; |
158 | end; |
157 | 159 | ||
- | 160 | // --- Section added by ViaThinkSoft --- |
|
- | 161 | ||
- | 162 | function SIDToString(ASID: PSID): string; |
|
- | 163 | ||
- | 164 | function _FallBack: string; |
|
- | 165 | var |
|
- | 166 | StringSid : PChar; |
|
- | 167 | len: DWORD; |
|
- | 168 | begin |
|
- | 169 | len := MAX_PATH; |
|
- | 170 | StringSid := AllocMem(MAX_PATH); |
|
- | 171 | ConvertSid(ASID, StringSid, len); |
|
- | 172 | Result := string(StringSid); |
|
- | 173 | FreeMem(StringSid); |
|
- | 174 | end; |
|
- | 175 | ||
- | 176 | type |
|
- | 177 | TFuncConvertSidToStringSid = function(Sid: PSID; out StringSid: PChar): BOOL; stdcall; |
|
- | 178 | var |
|
- | 179 | dllHandle: Cardinal; |
|
- | 180 | fConvertSidToStringSid: TFuncConvertSidToStringSid; |
|
- | 181 | StringSid : PChar; |
|
- | 182 | begin |
|
- | 183 | dllHandle := LoadLibrary(advapi32); |
|
- | 184 | if dllHandle = 0 then |
|
- | 185 | begin |
|
- | 186 | result := _FallBack; |
|
- | 187 | Exit; |
|
- | 188 | end; |
|
- | 189 | try |
|
- | 190 | @fConvertSidToStringSid := GetProcAddress(dllHandle, {$IFDEF UNICODE}'ConvertSidToStringSidW'{$ELSE}'ConvertSidToStringSidA'{$ENDIF}); |
|
- | 191 | if not Assigned(fConvertSidToStringSid) then |
|
- | 192 | begin |
|
- | 193 | result := _FallBack; |
|
- | 194 | Exit; |
|
- | 195 | end; |
|
- | 196 | ||
- | 197 | fConvertSidToStringSid(ASID, StringSid); |
|
- | 198 | Result := string(StringSid); |
|
- | 199 | LocalFree(HLocal(StringSid)); // added by ViaThinkSoft |
|
- | 200 | finally |
|
- | 201 | FreeLibrary(dllHandle); |
|
- | 202 | end; |
|
- | 203 | end; |
|
- | 204 | ||
- | 205 | function GetComputerName: string; |
|
- | 206 | // Source: http://www.delphi-treff.de/tipps-tricks/netzwerkinternet/netzwerkeigenschaften/computernamen-des-eigenen-rechners-ermitteln/ |
|
- | 207 | var |
|
- | 208 | Len: DWORD; |
|
- | 209 | begin |
|
- | 210 | Len := MAX_COMPUTERNAME_LENGTH+1; |
|
- | 211 | SetLength(Result,Len); |
|
- | 212 | if Windows.GetComputerName(PChar(Result), Len) then |
|
- | 213 | SetLength(Result,Len) |
|
- | 214 | else |
|
- | 215 | RaiseLastOSError; |
|
- | 216 | end; |
|
- | 217 | ||
- | 218 | function GetComputerSID: string; |
|
- | 219 | // Source: http://stackoverflow.com/a/7643383 |
|
- | 220 | var |
|
- | 221 | Sid: PSID; |
|
- | 222 | cbSid: DWORD; |
|
- | 223 | cbReferencedDomainName : DWORD; |
|
- | 224 | ReferencedDomainName: string; |
|
- | 225 | peUse: SID_NAME_USE; |
|
- | 226 | Success: BOOL; |
|
- | 227 | lpSystemName : string; |
|
- | 228 | lpAccountName: string; |
|
- | 229 | begin |
|
- | 230 | result := ''; |
|
- | 231 | Sid:=nil; |
|
- | 232 | try |
|
- | 233 | lpSystemName:=''; |
|
- | 234 | lpAccountName:=GetComputerName; |
|
- | 235 | ||
- | 236 | cbSid := 0; |
|
- | 237 | cbReferencedDomainName := 0; |
|
- | 238 | // First call to LookupAccountName to get the buffer sizes. |
|
- | 239 | Success := LookupAccountName(PChar(lpSystemName), PChar(lpAccountName), nil, cbSid, nil, cbReferencedDomainName, peUse); |
|
- | 240 | if (not Success) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then |
|
- | 241 | begin |
|
- | 242 | SetLength(ReferencedDomainName, cbReferencedDomainName); |
|
- | 243 | Sid := AllocMem(cbSid); |
|
- | 244 | // Second call to LookupAccountName to get the SID. |
|
- | 245 | Success := LookupAccountName(PChar(lpSystemName), PChar(lpAccountName), Sid, cbSid, PChar(ReferencedDomainName), cbReferencedDomainName, peUse); |
|
- | 246 | if not Success then |
|
- | 247 | begin |
|
- | 248 | FreeMem(Sid); |
|
- | 249 | Sid := nil; |
|
- | 250 | RaiseLastOSError; |
|
- | 251 | end |
|
- | 252 | else |
|
- | 253 | Result := SIDToString(Sid); |
|
- | 254 | end |
|
- | 255 | else |
|
- | 256 | RaiseLastOSError; |
|
- | 257 | finally |
|
- | 258 | if Assigned(Sid) then |
|
- | 259 | FreeMem(Sid); |
|
- | 260 | end; |
|
- | 261 | end; |
|
- | 262 | ||
158 | end. |
263 | end. |