-
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'm not quite sure what you're trying to do... so forgive me if this isn't helpful. The challenge is that both of these are consts. You can work around this by implementing a custom allocator, of course: import 'dart:ffi';
import 'dart:typed_data';
import 'package:win32/win32.dart';
class Allocator2 implements Allocator {
Allocator2();
@override
Pointer<T> allocate<T extends NativeType>(int byteCount, {int? alignment}) {
Pointer<T> result;
result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, byteCount)
.cast();
if (result.address == 0) {
throw ArgumentError('Could not allocate $byteCount bytes.');
}
return result;
}
@override
void free(Pointer pointer) {
if (HeapFree(GetProcessHeap(), 0, pointer) == 0) {
throw ArgumentError('Could not free $pointer.');
}
}
}
final hProcess = GetCurrentProcess();
int sizeOf2<T>() {
if (T.runtimeType == Uint32) return sizeOf<Uint32>();
if (T.runtimeType == IntPtr) return sizeOf<IntPtr>();
if (T.runtimeType == Pointer) return sizeOf<Pointer>();
//...
return 0; // or throw an exception
}
Uint8List? readProcessMemory<T extends NativeType>(int lpBaseAddress,
[int? sizeInBytes]) {
final actualSize = sizeInBytes ?? sizeOf2<T>();
final alloc = Allocator2();
final Pointer lpBuffer = alloc.allocate<T>(actualSize);
try {
final result = ReadProcessMemory(hProcess,
Pointer.fromAddress(lpBaseAddress), lpBuffer, actualSize, nullptr);
if (result == 1) {
// Do what you want with the result
} else {
return null;
}
} finally {
alloc.free(lpBuffer);
}
} But I don't quite understand what you want to do with the resultant data. The returned |
Beta Was this translation helpful? Give feedback.
I'm not quite sure what you're trying to do... so forgive me if this isn't helpful.
The challenge is that both of these are consts. You can work around this by implementing a custom allocator, of course: