Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer Pointer than int/IntPtr #363

Closed
Sunbreak opened this issue Feb 9, 2022 · 6 comments
Closed

Prefer Pointer than int/IntPtr #363

Sunbreak opened this issue Feb 9, 2022 · 6 comments
Labels
question Further information is requested

Comments

@Sunbreak
Copy link
Contributor

Sunbreak commented Feb 9, 2022

Problem

After applying type-map with package:win32, HANDLE and HWND changed from Pointer to IntPtr

  • Before
  int HidD_GetAttributes(
    HANDLE HidDeviceObject,
    PHIDD_ATTRIBUTES Attributes,
  )

typedef HANDLE = ffi.Pointer<ffi.Void>;
  HDEVINFO SetupDiGetClassDevsW(
    ffi.Pointer<GUID> ClassGuid,
    PCWSTR Enumerator,
    HWND hwndParent,
    int Flags,
  )

typedef HWND = ffi.Pointer<HWND__>;

class HWND__ extends ffi.Struct {
  @pkg_ffi.Int()
  external int unused;
}
  • After
  int HidD_GetAttributes(
    int HidDeviceObject,
    PHIDD_ATTRIBUTES Attributes,
  )
  HDEVINFO SetupDiGetClassDevsW(
    ffi.Pointer<GUID> ClassGuid,
    PCWSTR Enumerator,
    int hwndParent,
    int Flags,
  )

Both before and after work. But I prefer type-safe Pointer than Int/IntPtr

Request

Could we change HANDLE and HWND to Pointer?

https://github.com/timsneath/win32/blob/4d0e4dec9bafcddef331aa96244b9d59836ce87c/lib/src/types.dart#L30

https://github.com/timsneath/win32/blob/4d0e4dec9bafcddef331aa96244b9d59836ce87c/lib/src/types.dart#L41

@timsneath
Copy link
Contributor

These should be integer values though, no? They are sized to the same width as a Pointer (i.e. 64-bit on modern Windows systems, 32-bit or even 16-bit on older systems), but they are not pointers in that they don't reference a specific address in memory. You can think of them as a primary key. IntPtr seems like the right type for that, because it represents an integer which has the same size as a pointer.

(Arguably, UintPtr might be an even better fit, but we'd have to wait for Dart 2.17 for that, and in practice values are 32-bit for compatibility purposes.)

@timsneath timsneath added the question Further information is requested label Feb 14, 2022
@Sunbreak
Copy link
Contributor Author

Then why ffigen generate HANDLE and HWND as Pointer to a struct?

@timsneath
Copy link
Contributor

Ah, that's a great question! The answer is that HANDLE is defined in the Windows SDK as a void *.

Here's the definition in winnt.h:

typedef void *HANDLE;
#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name

If Windows was being written today, or with a different language in mind than C, they might have done things differently. But C doesn't know whether a Pointer is being used as a reference to a memory address or just as an opaque index. You can see that there's an attempt with the DECLARE_HANDLE macro to provide some primitive typing (so for example, you can't cast a HINSTANCE to an HRGN). But fundamentally, all handles are really just pointers under the hood.

Of course, ffigen knows nothing about these semantics or higher level Windows programming abstractions like COM. That's why I haven't adopted it for win32, even though it's a great tool.

You and I are not the only ones who grapple with this - see here: microsoft/win32metadata#21

@Sunbreak
Copy link
Contributor Author

There is a similiar question on Pointer<io_service_t> at dart-lang/native#509

Then should we use Int or IntPtr on Darwin platform?

@timsneath
Copy link
Contributor

I'm sorry -- I wish I could help you with that question, but I don't know much about macOS development. Although I switch around devices a fair amount, when it comes to development, Windows is my niche!

@Sunbreak
Copy link
Contributor Author

Thanks very much for your answer on Windows. Closing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants