From fa3f592335063dbbcbcb797cd5dfa87638e862be Mon Sep 17 00:00:00 2001 From: Martok Date: Sun, 16 Apr 2017 23:51:10 +0200 Subject: [PATCH] uwinImports: some more imports --- uwinImports.pas | 79 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/uwinImports.pas b/uwinImports.pas index 1051ca8..d689d70 100644 --- a/uwinImports.pas +++ b/uwinImports.pas @@ -16,7 +16,15 @@ uses const shell32 = 'shell32.dll'; + shlwapi = 'shlwapi.dll'; +{%REGION System } +const + SECURITY_NT_AUTHORITY: SID_IDENTIFIER_AUTHORITY = (Value: (0, 0, 0, 0, 0, 5)); + +function CheckTokenMembership(TokenHandle: HANDLE; SidToCheck: PSID; var IsMember: BOOL): BOOL; stdcall; external advapi32; + +{%ENDREGION} {%REGION Filesystem } @@ -75,7 +83,6 @@ const {%ENDREGION} - {%REGION ShellAPI } {$if FPC_FULLVERSION < 030101} // only needed in versions where 0029036 is not fixed (before r32452) @@ -87,6 +94,29 @@ function SHOpenFolderAndSelectItems(pidlFolder:LPCITEMIDLIST;cidl:UINT; apidl: // FIXME: temporary until 0028760 is fixed in FPC function SHBindToParent2(pidl:LPCITEMIDLIST; constref riid:TREFIID; var ppv:Pointer; var ppidlLast:LPCITEMIDLIST):HRESULT;StdCall;external External_library name 'SHBindToParent'; +type + PHICON = ^HICON; + +function ExtractIconEx(lpszFile: LPCSTR; nIconIndex: Integer; phIconLarge, phIconSmall: PHICON; nIcons: UINT):UINT; external shell32 name 'ExtractIconExA'; +function ExtractIconExW(lpszFile: LPCWSTR; nIconIndex: Integer; phIconLarge, phIconSmall: PHICON; nIcons: UINT):UINT; external shell32 name 'ExtractIconExW'; + +function PathUnExpandEnvStrings(lpSrc:LPCSTR; lpDst:LPSTR; nSize:DWORD):BOOL; external shlwapi name 'PathUnExpandEnvStringsA'; + +{%ENDREGION} + +{%REGION Helper - System } +(* +Routine Description: This routine returns TRUE if the caller's +process is a member of the Administrators local group. Caller is NOT +expected to be impersonating anyone and is expected to be able to +open its own process and process token. +Arguments: None. +Return Value: + TRUE - Caller has Administrators local group. + FALSE - Caller does not have Administrators local group. -- +*) +function IsUserAdmin: BOOL; +function IsWow64: Boolean; {%ENDREGION} {%REGION Helper - Filesystem } @@ -100,7 +130,7 @@ function GetFileCompressedSize(const aFileName: string; out FSize: QWord): Boole function GetFileIndex(const aFilename: string; out FIndex: QWORD): boolean; {%ENDREGION} -{%REGION Helpr - ShellAPI } +{%REGION Helper - ShellAPI } function GetConfigLocation(const aPortable, aRoaming: boolean; aProgramSubdir: String): string; function OpenFolderAndSelectFile(const FileName: string): boolean; @@ -111,6 +141,51 @@ implementation uses windirs; +function IsWow64: Boolean; +type + TIsWow64Process = function( // Type of IsWow64Process API fn + Handle: Windows.THandle; var Res: Windows.BOOL + ): Windows.BOOL; stdcall; +var + IsWow64Result: Windows.BOOL; // Result from IsWow64Process + IsWow64Process: TIsWow64Process; // IsWow64Process fn reference +begin + IsWow64Result:= false; + // Try to load required function from kernel32 + IsWow64Process := TIsWow64Process(Windows.GetProcAddress( + Windows.GetModuleHandle(kernel32), 'IsWow64Process' + )); + if Assigned(IsWow64Process) then + begin + // Function is implemented: call it + if not IsWow64Process( + Windows.GetCurrentProcess, IsWow64Result + ) then + raise SysUtils.Exception.Create('IsWow64: bad process handle'); + // Return result of function + Result := IsWow64Result; + end + else + // Function not implemented: can't be running on Wow64 + Result := False; +end; + +function IsUserAdmin: BOOL; +var + NTAuthority: SID_IDENTIFIER_AUTHORITY; + AdministratorsGroup: PSID; +begin + Result:= false; + NTAuthority:= SECURITY_NT_AUTHORITY; + AdministratorsGroup:= nil; + Result:= AllocateAndInitializeSid(@NTAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0,0,0,0,0,0, AdministratorsGroup); + if Result then begin + if not CheckTokenMembership(0, AdministratorsGroup, Result) then + Result:= false; + FreeSid(AdministratorsGroup); + end; +end; + function GetVolumePathName(const aFileName: string): string; begin SetLength(Result, MAX_PATH + 2);