microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2.06k stars 87 forks source link

Add support for WDK consumption #895

Closed AArnott closed 1 year ago

AArnott commented 1 year ago

https://www.nuget.org/packages/Microsoft.Windows.WDK.Win32Metadata/

elachlan commented 1 year ago

Has the WDK been implemented in CsWin32?

AArnott commented 1 year ago

We haven't tried it yet. The architecture allows for the WDK metadata nuget package to be consumed by CsWin32 and emitted as C#, but we have never tested it. That's what this issue is about.

mikebattista commented 1 year ago

FYI in the latest update I removed NtCreateFile from the SDK metadata and moved it to the WDK metadata per https://github.com/microsoft/wdkmetadata/issues/15.

You'll need to support the WDK metadata to support this and other Nt- APIs, or you may want to add a temporary manual definition of NtCreateFile in the projection in the meantime. The WDK definition is below if you need it.


[DllImport("ntdll.dll", ExactSpelling = true, PreserveSig = false)]
public unsafe static extern NTSTATUS NtCreateFile([Out] HANDLE* FileHandle, [In] FILE_ACCESS_RIGHTS DesiredAccess, [In] OBJECT_ATTRIBUTES* ObjectAttributes, [Out] IO_STATUS_BLOCK* IoStatusBlock, [Optional][In] long* AllocationSize, [In] FILE_FLAGS_AND_ATTRIBUTES FileAttributes, [In] FILE_SHARE_MODE ShareAccess, [In] NTCREATEFILE_CREATE_DISPOSITION CreateDisposition, [In] NTCREATEFILE_CREATE_OPTIONS CreateOptions, [Optional][In][MemorySize(BytesParamIndex = 10)] void* EaBuffer, [In] uint EaLength);

[Flags]
public enum NTCREATEFILE_CREATE_OPTIONS : uint
{
    FILE_DIRECTORY_FILE = 1u,
    FILE_NON_DIRECTORY_FILE = 0x40u,
    FILE_WRITE_THROUGH = 2u,
    FILE_SEQUENTIAL_ONLY = 4u,
    FILE_RANDOM_ACCESS = 0x800u,
    FILE_NO_INTERMEDIATE_BUFFERING = 8u,
    FILE_SYNCHRONOUS_IO_ALERT = 0x10u,
    FILE_SYNCHRONOUS_IO_NONALERT = 0x20u,
    FILE_CREATE_TREE_CONNECTION = 0x80u,
    FILE_NO_EA_KNOWLEDGE = 0x200u,
    FILE_OPEN_REPARSE_POINT = 0x200000u,
    FILE_DELETE_ON_CLOSE = 0x1000u,
    FILE_OPEN_BY_FILE_ID = 0x2000u,
    FILE_OPEN_FOR_BACKUP_INTENT = 0x4000u,
    FILE_RESERVE_OPFILTER = 0x100000u,
    FILE_OPEN_REQUIRING_OPLOCK = 0x10000u,
    FILE_COMPLETE_IF_OPLOCKED = 0x100u,
    FILE_OPEN_FOR_FREE_SPACE_QUERY = 0x800000u,
    FILE_CONTAINS_EXTENDED_CREATE_INFORMATION = 0x10000000u,
    FILE_NO_COMPRESSION = 0x8000u,
    FILE_DISALLOW_EXCLUSIVE = 0x20000u,
    FILE_SESSION_AWARE = 0x40000u,
    FILE_OPEN_NO_RECALL = 0x400000u
}

public enum NTCREATEFILE_CREATE_DISPOSITION : uint
{
    FILE_SUPERSEDE = 0u,
    FILE_CREATE = 2u,
    FILE_OPEN = 1u,
    FILE_OPEN_IF = 3u,
    FILE_OVERWRITE = 4u,
    FILE_OVERWRITE_IF = 5u
}
AArnott commented 1 year ago

I'm working on this, but completion will depend on https://github.com/microsoft/wdkmetadata/issues/41.