I want to get a WindowId from a HWND from F# and .NET so I can call other WinRT APIs which want a WindowID. I think this is done in WIL:
inline winrt::Windows::UI::WindowId GetWindowIdFromWindow(HWND hwnd)
{
ABI::Windows::UI::WindowId abiWindowId;
winrt::check_hresult(::GetWindowIdFromWindow(hwnd, &abiWindowId));
return winrt::Windows::UI::WindowId{ abiWindowId.Value };
}
An equivalent function, Win32Interop.GetWindowIdFromWindow(IntPtr), is available in Windows App SDK however I can't use the SDK from an F# project because the SDK is bundled with WinUI which does not work with F#.
The solution I'd like is being able to call some external function from F# which exchanges my HWND for a WindowId, but I don't know what I'd need to import or call. Possible workarounds are:
It's not obvious why this WindowId concept exists, beyond the fact it's documented for top-level windows only where HWND are for all windows. Maybe for a (future?) cross platform target?
Anyway, currently the WindowId's Value is equal to the HWND's value, so it's pretty easy to compute one from another.
Another way is to use an undocumented function from AppSDK's Microsoft.Internal.FrameworkUdk.dll
file. You can declare this in C# (don't know in F#):
[DllImport("Microsoft.Internal.FrameworkUdk.dll")]
private static extern int Windowing_GetWindowIdFromWindow(
IntPtr hwnd,
out WindowId windowId);
or to avoid linking to WindowId structure:
private static extern int Windowing_GetWindowIdFromWindow(
IntPtr hwnd,
out ulong windowId); // windowId = hwnd...
This dll is part of the AppSDK Runtime and always loaded in an WinU3I app. Maybe overkill...