microsoft / win32metadata

Tooling to generate metadata for Win32 APIs in the Windows SDK.
Other
1.34k stars 118 forks source link

CreateRemoteThread(Ex) is not really usable #2014

Open lowleveldesign opened 2 weeks ago

lowleveldesign commented 2 weeks ago

When trying to use the CreateRemoteThread(Ex) function, I found that the generated signatures are not really usable as the lpStartAddress is of a delegate type:

internal static extern unsafe winmdroot.Foundation.HANDLE CreateRemoteThread(winmdroot.Foundation.HANDLE hProcess, [Optional] winmdroot.Security.SECURITY_ATTRIBUTES* lpThreadAttributes, nuint dwStackSize, winmdroot.System.Threading.LPTHREAD_START_ROUTINE lpStartAddress, [Optional] void* lpParameter, uint dwCreationFlags, [Optional] uint* lpThreadId);

The problem is that the remote function address is almost certainly invalid in the context of the calling process and creating a delegate to such an address (by using, for example, Marshal.GetDelegateForFunctionPointer) will fail. If there is a way to create this delegate, please let me know. Otherwise, could you please change the lpStartAddress type to either void * or nint?

mikebattista commented 2 weeks ago

Are you using CsWin32? @AArnott

lowleveldesign commented 2 weeks ago

Yes, I use it through cswin32.

riverar commented 2 weeks ago

While metadata uses delegates to describe unmanaged function pointers, they are not intended to represent actual C# delegates. When these types are encountered, CsWin32 could potentially decompose them into IntPtr. (However, I recognize that this approach would result in a loss of type safety and other benefits.)

@AArnott Is there a way to instruct CsWin32 to emit raw bindings for APIs like this on an individual basis (e.g., via NativeMethods.json)?