Dart:ffi - 'must be a subtype' for 'lookupFunction' issue for native code in dart

81 views Asked by At

The type 'Uint32 Function(IntPtr, Pointer<Uint32>)' must be a subtype of 'int Function(IntPtr, Pointer<Uint32>)' for 'lookupFunction'.

class Psapi {
  static final ffi.DynamicLibrary psapi = ffi.DynamicLibrary.open('psapi.dll');

  static int GetWindowThreadProcessId(
      ffi.IntPtr hwnd, ffi.Pointer<ffi.Uint32> lpdwProcessId) {
    return psapi.lookupFunction<ffi.Uint32 Function(ffi.IntPtr hwnd, ffi.Pointer<ffi.Uint32> lpdwProcessId),
        int Function(ffi.IntPtr hwnd, ffi.Pointer<ffi.Uint32> lpdwProcessId)>('GetWindowThreadProcessId')(
      hwnd,
      lpdwProcessId,
    );
  }
}

In this code int Function(ffi.IntPtr hwnd, ffi.Pointer<ffi.Uint32> lpdwProcessId) this part shows error and says The type 'Uint32 Function(IntPtr, Pointer<Uint32>)' must be a subtype of 'int Function(IntPtr, Pointer<Uint32>)' for 'lookupFunction' Can someone explain where it gone wrong and what's the solution. Thanks in advance.

1

There are 1 answers

0
Sharif Ahamed On BEST ANSWER

The lookupFunction's second type argument represent the Dart function signature for the corresponding Win32 function. IntPtr's Dart signature would be int. But implementing the code directly can cause error.