Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Trying to access pointer value from the Struct closes the application #96

Closed
krll-kov opened this issue Apr 18, 2021 · 7 comments
Closed

Comments

@krll-kov
Copy link

I have a struct

class MODULEINFO extends Struct {
  external Pointer lpBaseOfDll;
  @Uint32()
  external int sizeOfImage;
  external Pointer entryPoint;
}

And a function

static getModuleInformation(int hProcess, int hModule) {
    final _getModuleInformation = _psapi.lookupFunction<
        Int32 Function(IntPtr hProcess, IntPtr hModule, Pointer<MODULEINFO> lpmodinfo, Int32 cb),
        int Function(int hProcess, int hModule, Pointer<MODULEINFO> lpmodinfo, int cb)>('GetModuleInformation');

    final mi = calloc<MODULEINFO>();
    final int dwSize = sizeOf<MODULEINFO>();

    int f = _getModuleInformation(hProcess, hModule, mi, dwSize);
    print(f); // Function succeeded with result 1

    print(mi.ref.lpBaseOfDll.cast<Int32>().elementAt(0).value); // Application just closes 
  }

When i try to print a value from the struct - application crashes without any errors. Am i doing something wrong or it's a bug?

@dcharkes
Copy link
Contributor

Hi!

What does print(mi.ref.lpBaseOfDll) print? If that is an address that is not allocated or it is 0x0 (nullptr) and you're trying to access that, the application crashes with a segmentation fault because you're trying to access memory that your application has not allocated.

@krll-kov
Copy link
Author

krll-kov commented Apr 19, 2021

Hi!

What does print(mi.ref.lpBaseOfDll) print? If that is an address that is not allocated or it is 0x0 (nullptr) and you're trying to access that, the application crashes with a segmentation fault because you're trying to access memory that your application has not allocated.

flutter: Pointer NativeType>: address=0x35410000

@mraleph
Copy link
Contributor

mraleph commented Apr 20, 2021

Is hProcess pointing to your current process or to some other process?

@krll-kov
Copy link
Author

Is hProcess pointing to your current process or to some other process?

To some other process

@mraleph
Copy link
Contributor

mraleph commented Apr 20, 2021

Then you need to use ReadProcessMemory. You can't directly dereference the pointer because it originates from virtual address space of an another process.

@mraleph mraleph closed this as completed Apr 20, 2021
@krll-kov
Copy link
Author

Then you need to use ReadProcessMemory. You can't directly dereference the pointer because it originates from virtual address space of an another process.

I have an issue with RPM - it does not work for some reason: halildurmus/win32#215. And i cannot find out the problem because GetLastError does not retrieve latest winapi error

@dcharkes
Copy link
Contributor

@kirill-21 we're tracking that in dart-lang/sdk#38832.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants