@@ -2,42 +2,57 @@ import 'dart:ffi';
22import 'dart:io' ;
33
44import 'package:ffi/ffi.dart' as ffi;
5- import 'package:superuser_interfaces/superuser_interfaces.dart'
6- show SuperuserInterface;
5+ import 'package:superuser_interfaces/superuser_interfaces.dart' ;
76
87import 'superuser_plugin_windows_bindings_generated.dart' ;
98
109const String _libName = 'superuser_plugin_windows' ;
1110
1211/// Construct [SuperuserInterface] based on Windows API.
13- final class WindowsSuperuser implements SuperuserInterface {
14- static WindowsSuperuser ? _instance;
12+ final class WindowsSuperuser extends SuperuserPlatform {
13+ WindowsSuperuser ()
14+ : super (() {
15+ if (Platform .isWindows) {
16+ return DynamicLibrary .open ('$_libName .dll' );
17+ }
1518
16- late final SuperuserPluginWindowsBindings _bindings;
19+ throw UnsupportedError (
20+ 'Unknown platform: ${Platform .operatingSystem }' );
21+ });
1722
18- WindowsSuperuser ._() {
19- _bindings = SuperuserPluginWindowsBindings (() {
20- if (Platform .isWindows) {
21- return DynamicLibrary .open ('$_libName .dll' );
22- }
23+ @override
24+ bool get isActivated => onGettingProperties (
25+ (lib) => SuperuserPluginWindowsBindings (lib).is_elevated ());
26+ @override
27+ bool get isSuperuser => onGettingProperties (
28+ (lib) => SuperuserPluginWindowsBindings (lib).is_admin_user ());
2329
24- throw UnsupportedError ('Unknown platform: ${Platform .operatingSystem }' );
25- }());
26- }
30+ @override
31+ String get whoAmI => onGettingProperties ((lib) {
32+ final SuperuserPluginWindowsBindings binding =
33+ SuperuserPluginWindowsBindings (lib);
2734
28- factory WindowsSuperuser () {
29- _instance ?? = WindowsSuperuser ._();
35+ Pointer <Pointer <Char >> resultPtr = ffi.calloc <Pointer <Char >>();
3036
31- return _instance ! ;
32- }
37+ try {
38+ int errResult = binding. get_current_username (resultPtr);
3339
34- @override
35- bool get isActivated => _bindings.is_elevated ();
40+ if (errResult != 0 ) {
41+ throw SuperuserProcessError (
42+ errResult, "Unable to extract current username." );
43+ }
3644
37- @override
38- bool get isSuperuser => _bindings.is_admin_user ();
45+ Pointer <Char > result = resultPtr.value;
3946
40- @override
41- String get whoAmI =>
42- _bindings.get_current_username ().cast< ffi.Utf8 > ().toDartString ();
47+ try {
48+ Pointer <ffi.Utf8 > ctx = result.cast< ffi.Utf8 > ();
49+
50+ return ctx.toDartString ();
51+ } finally {
52+ binding.flush_string (result);
53+ }
54+ } finally {
55+ ffi.calloc.free (resultPtr);
56+ }
57+ });
4358}
0 commit comments