Skip to content

FFI support for packed structs #38158

Closed
Closed
@ds84182

Description

@ds84182

Amazingly, it turns out that some Win32 structs are packed. (In my case, OPENFILENAMEW). I don't know how many Win32 structs are packed, but the lack of struct packing makes it difficult to interface with Win32.

Example:

class OpenFileNameW extends Struct<OpenFileNameW> {
  @Int32()
  int lStructSize; // DWORD         lStructSize;

  @IntPtr()
  int hwndOwner; // HWND          hwndOwner;

  @IntPtr()
  int hInstance; // HINSTANCE     hInstance;

  Pointer<Uint16 /* CWSTR */ > lpstrFilter; // LPCWSTR       lpstrFilter;

  Pointer<Uint16 /* WSTR */ > lpstrCustomFilter; // LPWSTR        lpstrCustomFilter;

  @Int32()
  int nMaxCustFilter; // DWORD         nMaxCustFilter;

  @Int32()
  int nFilterIndex; // DWORD         nFilterIndex;

  Pointer<Uint16 /* WSTR */ > lpstrFile; // LPWSTR        lpstrFile;

  @Int32()
  int nMaxFile; // DWORD         nMaxFile;

  Pointer<Uint16 /* WSTR */ > lpstrFileTitle; // LPWSTR        lpstrFileTitle;

  @Int32()
  int nMaxFileTitle; // DWORD         nMaxFileTitle;

  Pointer<Uint16 /* CWSTR */ > lpstrInitialDir; // LPCWSTR       lpstrInitialDir;

  Pointer<Uint16 /* CWSTR */ > lpstrTitle; // LPCWSTR       lpstrTitle;

  @Int32()
  int Flags; // DWORD         Flags;

  @Int16()
  int nFileOffset; // WORD          nFileOffset;

  @Int16()
  int nFileExtension; // WORD          nFileExtension;

  Pointer<Uint16 /* CWSTR */ > lpstrDefExt; // LPCWSTR       lpstrDefExt;

  @IntPtr()
  int lCustData; // LPARAM        lCustData;

  Pointer<Void /* OFNHOOKPROC */ > lpfnHook; // LPOFNHOOKPROC lpfnHook;

  Pointer<Uint16 /* CWSTR */ > lpTemplateName; // LPCWSTR       lpTemplateName;

  Pointer<Void /* EditMenu */ > lpEditInfo; // LPEDITMENU    lpEditInfo;

  Pointer<CString> lpstrPrompt; // LPCSTR        lpstrPrompt;

  Pointer<Void> pvReserved; // void          *pvReserved;

  @Int32()
  int dwReserved; // DWORD         dwReserved;

  @Int32()
  int FlagsEx; // DWORD         FlagsEx;
}

sizeOf returns 168, but in Godbolt with the actual comdlg headers, the result is 152. https://godbolt.org/z/6Tyt3h

Metadata

Metadata

Assignees

Labels

area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffi

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions