by Guest on 2023/01/12 02:25:09 PM
by Guest on 2023/01/12 03:15:33 PM
Here is the test, use Python.
import ctypes
import os
import stat
import time
is_admin = ctypes.windll.shell32.IsUserAnAdmin()
print(f'This is{"" if is_admin else " not"} a elevated process.')
NULL = None
GENERIC_READ = 0x80000000
GENERIC_WRITE = 0x40000000
FILE_SHARE_READ = 0x00000001
FILE_SHARE_WRITE = 0x00000002
OPEN_ALWAYS = 4
FILE_ATTRIBUTE_NORMAL = 0x80
FILE_BEGIN = 0
INVALID_HANDLE_VALUE = -1
filepath = 'testfile'
filepath = os.path.abspath(filepath)
filesize = 100 # MB
offset = (1 << 20) * filesize
startn = time.time()
hFile = ctypes.windll.Kernel32.CreateFileW(
filepath, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL)
assert hFile != INVALID_HANDLE_VALUE
assert ctypes.windll.Kernel32.SetFilePointerEx(hFile, offset, NULL, FILE_BEGIN)
assert ctypes.windll.Kernel32.SetEndOfFile(hFile)
assert ctypes.windll.Kernel32.CloseHandle(hFile)
costn = (time.time() - startn) * 1e3
is_sparse = stat.FILE_ATTRIBUTE_SPARSE_FILE & os.stat(filepath).st_file_attributes
input(f'''
File "{filepath}" has been created.
File Size: {filesize} MB
Is Sparse: {bool(is_sparse)}
Cost Time: {costn} ms
Press Enter to delete test file and exit...''')
os.remove(filepath)