【问题标题】:Dart _psapi not foundDart _psapi 未找到
【发布时间】:2021-04-30 11:43:08
【问题描述】:

我正在尝试调用 WinAPI。我从docs 复制过去的代码:


import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';

main() { 

 int EnumProcesses(
    Pointer<Uint32> lpidProcess, int cb, Pointer<Uint32> lpcbNeeded) {
  final _EnumProcesses = _psapi.lookupFunction<
      Int32 Function(
          Pointer<Uint32> lpidProcess, Uint32 cb, Pointer<Uint32> lpcbNeeded),
      int Function(Pointer<Uint32> lpidProcess, int cb,
          Pointer<Uint32> lpcbNeeded)>('EnumProcesses');
  return _EnumProcesses(lpidProcess, cb, lpcbNeeded);
 }

}

【问题讨论】:

    标签: dart ffi dart-ffi


    【解决方案1】:

    如果你有对 win32 的引用,你也不需要复制这段代码——你应该已经有 EnumProcesses API 可供你使用。

    下面是一个简单的使用示例:

    import 'dart:ffi';
    import 'dart:io';
    
    import 'package:ffi/ffi.dart';
    
    import 'package:win32/win32.dart';
    
    int printModules(int processID) {
      // Print the process identifier.
      print('\nProcess ID: $processID');
    
      // Get a handle to the process.
      final hProcess = OpenProcess(
          PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
    
      if (hProcess == 0) {
        return 1;
      }
    
      // Get a list of all the modules in this process.
      final hMods = allocate<IntPtr>(count: 1024);
      final cbNeeded = allocate<Uint32>();
    
      if (EnumProcessModules(
              hProcess, hMods, sizeOf<IntPtr>() * 1024, cbNeeded.cast()) ==
          1) {
        for (var i = 0; i < ((cbNeeded.value / sizeOf<IntPtr>()).floor()); i++) {
          final szModName = allocate<Uint16>(count: MAX_PATH).cast<Utf16>();
    
          // Get the full path to the module's file.
          final hModule = hMods.elementAt(i).value;
          final moduleValue =
              '0x${hModule.toRadixString(16).padLeft(sizeOf<IntPtr>(), '0').toUpperCase()}';
    
          if (GetModuleFileNameEx(hProcess, hModule, szModName, MAX_PATH) != 0) {
            // Print the module name and handle value.
            print('\t${szModName.unpackString(MAX_PATH)} ($moduleValue)');
          }
          free(szModName);
        }
      }
    
      free(hMods);
      free(cbNeeded);
    
      // Release the handle to the process.
      CloseHandle(hProcess);
    
      return 0;
    }
    
    void main() {
      final aProcesses = allocate<Uint32>(count: 1024);
      final cbNeeded = allocate<Uint32>();
    
      // Get the list of process identifiers.
      if (EnumProcesses(aProcesses, sizeOf<Uint32>() * 1024, cbNeeded.cast()) ==
          0) {
        print('EnumProcesses failed.');
        exit(1);
      }
    
      // Calculate how many process identifiers were returned.
      final cProcesses = (cbNeeded.value / sizeOf<Uint32>()).floor();
    
      // Print the names of the modules for each process.
      for (var i = 0; i < cProcesses; i++) {
        printModules(aProcesses[i]);
      }
    }
    

    【讨论】:

    • 非常感谢!!!你能解释一下如何从结果列表中获取 pid 作为 int 吗?为了能够在 dart 代码中使用它。
    • 在上例的最后一行,aProcesses[i] 是 pid。如果您对进程的详细信息不感兴趣而不是 pid,则不需要 printModules 函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2017-02-24
    • 2018-08-26
    • 2022-12-21
    相关资源
    最近更新 更多