Skip to content

Instantly share code, notes, and snippets.

@Zensey
Created July 1, 2024 11:17
Show Gist options
  • Save Zensey/76b326d157c6ccca40e30385aeda3dc5 to your computer and use it in GitHub Desktop.
Save Zensey/76b326d157c6ccca40e30385aeda3dc5 to your computer and use it in GitHub Desktop.
import 'package:ffi/src/utf8.dart';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';
import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
Future<bool> startElevatedProcess({required String executable, required String args, bool window = false}) async {
var shellInput = calloc<SHELLEXECUTEINFO>();
shellInput.ref.lpFile = executable.toNativeUtf16();
shellInput.ref.lpParameters = args.toNativeUtf16();
shellInput.ref.nShow = window ? SW_SHOWNORMAL : SW_HIDE;
shellInput.ref.fMask = ES_AWAYMODE_REQUIRED;
shellInput.ref.lpVerb = "runas".toNativeUtf16();
shellInput.ref.cbSize = sizeOf<SHELLEXECUTEINFO>();
var shellResult = ShellExecuteEx(shellInput);
return shellResult == 1;
}
void main() async {
await startElevatedProcess(executable:"ping", args:"8.8.8.8", window:true);
print("Hello, World!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment