60 lines
1.4 KiB
Dart
60 lines
1.4 KiB
Dart
import 'dart:ffi';
|
|
import 'dart:io';
|
|
import 'package:ffi/ffi.dart';
|
|
|
|
class Windows {
|
|
static Windows? _instance;
|
|
late DynamicLibrary _shell32;
|
|
|
|
Windows._internal() {
|
|
_shell32 = DynamicLibrary.open('shell32.dll');
|
|
}
|
|
|
|
factory Windows() {
|
|
_instance ??= Windows._internal();
|
|
return _instance!;
|
|
}
|
|
|
|
bool runas(String command, String arguments) {
|
|
final commandPtr = command.toNativeUtf16();
|
|
final argumentsPtr = arguments.toNativeUtf16();
|
|
final operationPtr = 'runas'.toNativeUtf16();
|
|
|
|
final shellExecute = _shell32.lookupFunction<
|
|
Int32 Function(
|
|
Pointer<Utf16> hwnd,
|
|
Pointer<Utf16> lpOperation,
|
|
Pointer<Utf16> lpFile,
|
|
Pointer<Utf16> lpParameters,
|
|
Pointer<Utf16> lpDirectory,
|
|
Int32 nShowCmd),
|
|
int Function(
|
|
Pointer<Utf16> hwnd,
|
|
Pointer<Utf16> lpOperation,
|
|
Pointer<Utf16> lpFile,
|
|
Pointer<Utf16> lpParameters,
|
|
Pointer<Utf16> lpDirectory,
|
|
int nShowCmd)>('ShellExecuteW');
|
|
|
|
final result = shellExecute(
|
|
nullptr,
|
|
operationPtr,
|
|
commandPtr,
|
|
argumentsPtr,
|
|
nullptr,
|
|
1,
|
|
);
|
|
|
|
calloc.free(commandPtr);
|
|
calloc.free(argumentsPtr);
|
|
calloc.free(operationPtr);
|
|
|
|
if (result <= 32) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
final windows = Platform.isWindows ? Windows() : null;
|