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

CreateRemoteThread(Ex) is not really usable #2014

Open
lowleveldesign opened this issue Nov 4, 2024 · 3 comments
Open

CreateRemoteThread(Ex) is not really usable #2014

lowleveldesign opened this issue Nov 4, 2024 · 3 comments
Assignees

Comments

@lowleveldesign
Copy link

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
Copy link
Collaborator

Are you using CsWin32? @AArnott

@lowleveldesign
Copy link
Author

Yes, I use it through cswin32.

@riverar
Copy link
Collaborator

riverar commented Nov 7, 2024

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)?

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

No branches or pull requests

4 participants