admin 1 year ago
parent
commit
4ae53da970

+ 34 - 0
example/lib/main.dart

@@ -21,6 +21,7 @@ class _MyAppState extends State<MyApp> {
   String _platformVersion = 'Unknown';
   String _isRunAdminEerr = 'Unknown';
   String _isAdmin = 'no';
+  String _isRunPassName = 'no';
   final _wlBaseHelpPlugin = WlBaseHelp();
 
   @override
@@ -76,6 +77,30 @@ class _MyAppState extends State<MyApp> {
       _isAdmin = isAdmin ? 'yes' : 'no';
     });
   }
+
+  Future<void> onisProcessRunning(String passName) async {
+    bool isRun;
+    try {
+      isRun = await _wlBaseHelpPlugin.isProcessRunning(passName) ?? false;
+    } on PlatformException {
+      isRun = false;
+      _isRunPassName = 'error';
+      setState(() {} );
+    }
+    setState(() {
+      _isRunPassName = isRun ? 'yes' : 'no';
+    });
+  }
+
+  Future<void> onkillProcess(String passName) async {
+
+    try {
+      await _wlBaseHelpPlugin.killProcess(passName);
+    } on PlatformException {
+      setState(() {} );
+    }
+
+  }
   @override
   Widget build(BuildContext context) {
     return MaterialApp(
@@ -105,6 +130,15 @@ class _MyAppState extends State<MyApp> {
                   onPressed: () async{
                     await _wlBaseHelpPlugin.hideConsole();
                   }, child: Text('隐藏控制台')),
+
+              ElevatedButton(
+                  onPressed: () async{
+                    await onisProcessRunning('dart.exe');
+                  }, child: Text(_isRunPassName)),
+              ElevatedButton(
+                  onPressed: () async{
+                    await onkillProcess('IDMan.exe');
+                  }, child: Text('隐藏控制台')),
             ],
           ),
         ),

+ 7 - 0
lib/wl_base_help.dart

@@ -22,4 +22,11 @@ class WlBaseHelp {
     await  WlBaseHelpPlatform.instance.hideConsole();
   }
 
+  Future<bool?> isProcessRunning(String processName){
+    return WlBaseHelpPlatform.instance.isProcessRunning(processName);
+  }
+
+  Future<void> killProcess(String processName)  async {
+    await  WlBaseHelpPlatform.instance.killProcess(processName);
+  }
 }

+ 11 - 1
lib/wl_base_help_method_channel.dart

@@ -29,12 +29,22 @@ class MethodChannelWlBaseHelp extends WlBaseHelpPlatform {
     final isAdmin = await methodChannel.invokeMethod<bool>('isRunningAsAdmin');
     return isAdmin;
   }
+  @override
   Future<void> showConsole() async {
     await methodChannel.invokeMethod('showConsole');
   }
-
+  @override
   Future<void> hideConsole() async {
     await methodChannel.invokeMethod('hideConsole');
   }
+  @override
+  Future<bool?> isProcessRunning(String processName) async {
+    final isRunning = await methodChannel.invokeMethod('isProcessRunning', {'processName': processName});
+    return isRunning;
+  }
 
+  @override
+  Future<void> killProcess(String processName) async {
+    await methodChannel.invokeMethod('killProcess', {'processName': processName});
+  }
 }

+ 8 - 0
lib/wl_base_help_platform_interface.dart

@@ -43,4 +43,12 @@ abstract class WlBaseHelpPlatform extends PlatformInterface {
    Future<void> hideConsole() async {
      throw UnimplementedError('hideConsole() has not been implemented.');
   }
+
+  Future<bool?> isProcessRunning(String processName)  async {
+    throw UnimplementedError('isProcessRunning() has not been implemented.');
+  }
+
+  Future<void> killProcess(String processName) async {
+    throw UnimplementedError('killProcess() has not been implemented.');
+  }
 }

+ 12 - 0
test/wl_base_help_test.dart

@@ -34,6 +34,18 @@ class MockWlBaseHelpPlatform
     // TODO: implement showConsole
     throw UnimplementedError();
   }
+
+  @override
+  Future<bool?> isProcessRunning(String processName) {
+    // TODO: implement isProcessRunning
+    throw UnimplementedError();
+  }
+
+  @override
+  Future<void> killProcess(String processName) {
+    // TODO: implement killProcess
+    throw UnimplementedError();
+  }
 }
 
 void main() {

+ 64 - 2
windows/wl_base_help_plugin.cpp

@@ -2,7 +2,8 @@
 
 // This must be included before many other Windows headers.
 #include <windows.h>
-
+#include <tlhelp32.h>
+#include <string>
 // For getPlatformVersion; remove unless needed for your plugin implementation.
 #include <VersionHelpers.h>
 
@@ -53,6 +54,54 @@ void HideConsole() {
         ShowWindow(consoleWindow, SW_HIDE);
     }
 }
+
+
+bool IsProcessRunning(const std::wstring& processName) {
+    bool isRunning = false;
+    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+
+    if (hSnapshot != INVALID_HANDLE_VALUE) {
+        PROCESSENTRY32 pe32;
+        pe32.dwSize = sizeof(PROCESSENTRY32);
+
+        if (Process32First(hSnapshot, &pe32)) {
+            do {
+                if (processName == pe32.szExeFile) {
+                    isRunning = true;
+                    break;
+                }
+            } while (Process32Next(hSnapshot, &pe32));
+        }
+
+        CloseHandle(hSnapshot);
+    }
+
+    return isRunning;
+}
+void KillProcess(const std::wstring& processName) {
+    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+
+    if (hSnapshot != INVALID_HANDLE_VALUE) {
+        PROCESSENTRY32 pe32;
+        pe32.dwSize = sizeof(PROCESSENTRY32);
+
+        if (Process32First(hSnapshot, &pe32)) {
+            do {
+                if (processName == pe32.szExeFile) {
+                    HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
+                    if (hProcess != NULL) {
+                        TerminateProcess(hProcess, 0);
+                        CloseHandle(hProcess);
+                        break;
+                    }
+                }
+            } while (Process32Next(hSnapshot, &pe32));
+        }
+
+        CloseHandle(hSnapshot);
+    }
+}
+
 namespace wl_base_help {
 
 // static
@@ -81,7 +130,20 @@ void WlBaseHelpPlugin::HandleMethodCall(
     const flutter::MethodCall<flutter::EncodableValue>& method_call,
     std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
     
-    if (method_call.method_name().compare("showConsole") == 0) {
+
+    if (method_call.method_name().compare("isProcessRunning") == 0) {
+        const auto* arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
+        auto processName = std::get<std::string>(arguments->find(flutter::EncodableValue("processName"))->second);
+        bool isRunning = IsProcessRunning(std::wstring(processName.begin(), processName.end()));
+        result->Success(flutter::EncodableValue(isRunning));
+    }
+    else if (method_call.method_name().compare("killProcess") == 0) {
+        const auto* arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
+        auto processName = std::get<std::string>(arguments->find(flutter::EncodableValue("processName"))->second);
+        KillProcess(std::wstring(processName.begin(), processName.end()));
+        result->Success();
+    }
+    else  if (method_call.method_name().compare("showConsole") == 0) {
         ShowConsole();
         result->Success();
     }