Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

ffi - Dart _psapi not found

I am trying to call WinAPI. I have copy-past code from 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);
 }

}

enter image description here

question from:https://stackoverflow.com/questions/65905604/dart-psapi-not-found

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you've got a reference to win32, you don't need to copy this code also -- you should already have the EnumProcesses API available to you.

Here's a simple example of how to use it:

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('
Process 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('${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]);
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...