function EnableProcessPrivilege(
const Privilege:
string; Enable: Boolean = True): Boolean;
var
Token: THandle;
ReturnLen: LongWord;
TokenPriv: TTokenPrivileges;
begin
Result := False;
if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, Token)
then
try
TokenPriv.PrivilegeCount := 1;
TokenPriv.Privileges[0].Attributes := IfThen(Enable, SE_PRIVILEGE_ENABLED, 0);
Result := LookupPrivilegeValue(
nil, PChar(Privilege), TokenPriv.Privileges[0].Luid)
and AdjustTokenPrivileges(Token, False, TokenPriv, SizeOf(TokenPriv),
nil, ReturnLen);
finally
CloseHandle(Token);
end;
end;
function EnableThreadPrivilege(
const Privilege:
string; Enable: Boolean = True): Boolean;
var
Token: THandle;
ReturnLen: LongWord;
TokenPriv: TTokenPrivileges;
begin
Result := False;
if OpenThreadToken(GetCurrentThread, TOKEN_ADJUST_PRIVILEGES, False, Token)
or ((GetLastError = ERROR_NO_TOKEN)
and OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, Token))
then
try
TokenPriv.PrivilegeCount := 1;
TokenPriv.Privileges[0].Attributes := IfThen(Enable, SE_PRIVILEGE_ENABLED, 0);
Result := LookupPrivilegeValue(
nil, PChar(Privilege), TokenPriv.Privileges[0].Luid)
and AdjustTokenPrivileges(Token, False, TokenPriv, SizeOf(TokenPriv),
nil, ReturnLen);
finally
CloseHandle(Token);
end;
end;
procedure TForm4.Button1Click(Sender: TObject);
var
Testfile:
String;
Handle, FindHandle: THandle;
FindData: TWIN32FindData;
SetFileShortName:
function(hFile: THandle; lpShortName: PChar): LongBool;
stdcall;
begin
SetFileShortName := GetProcAddress(GetModuleHandle('
Kernel32.dll'), '
SetFileShortNameW');
Testfile := ExtractFilePath(ParamStr(0)) + '
abgfdsarv'#12345'
.txt';
//if not EnableProcessPrivilege('SeDebugPrivilege') then
// RaiseLastOSError;
if not EnableProcessPrivilege('
SeRestorePrivilege')
then
RaiseLastOSError;
Handle := CreateFile(PChar(Testfile), GENERIC_ALL, FILE_SHARE_READ,
nil, OPEN_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, 0);
if Handle = INVALID_HANDLE_VALUE
then
RaiseLastOSError;
try
FindHandle := FindFirstFile(PChar(Testfile), FindData);
if FindHandle = INVALID_HANDLE_VALUE
then
RaiseLastOSError;
Winapi.Windows.FindClose(FindHandle);
OutputDebugString(PChar(
String(FindData.cAlternateFileName)));
if not SetFileShortName(
Handle, '
abgfd~13.txt')
then
RaiseLastOSError;
FindHandle := FindFirstFile(PChar(Testfile), FindData);
if FindHandle = INVALID_HANDLE_VALUE
then
RaiseLastOSError;
Winapi.Windows.FindClose(FindHandle);
OutputDebugString(PChar(
String(FindData.cAlternateFileName)));
if not SetFileShortName(
Handle, '
abgfds~1.txt')
then
RaiseLastOSError;
FindHandle := FindFirstFile(PChar(Testfile), FindData);
if FindHandle = INVALID_HANDLE_VALUE
then
RaiseLastOSError;
Winapi.Windows.FindClose(FindHandle);
OutputDebugString(PChar(
String(FindData.cAlternateFileName)));
if not SetFileShortName(
Handle,
nil)
then
RaiseLastOSError;
FindHandle := FindFirstFile(PChar(Testfile), FindData);
if FindHandle = INVALID_HANDLE_VALUE
then
RaiseLastOSError;
Winapi.Windows.FindClose(FindHandle);
OutputDebugString(PChar(
String(FindData.cAlternateFileName)));
finally
CloseHandle(
Handle);
if not DeleteFile(PChar(Testfile))
then
RaiseLastOSError;
end;