luca.p.alexandru
luca.p.alexandru

Reputation: 1750

Using Windows API functions in C#

So, I want to use the CreateProcess API function in C#. I know that I have to import the kernel32.dll file and overwrite the function header and mark it as extern. I also know that I have to implement the structures that the function uses. The problem that I have is the following: Where can I see the exact structure of the structure I need to implement?

[DllImport("Kernel32.dll")]
public static extern HANDLE CreateThread(PSECURITY_ATTRIBUTES psa,
                                             DWORD cbStack,
                                             PTHREAD_START_ROUTINE pfnStartAddr,
                                             PVOID pvParam,
                                             DWORD fwdCreate,
                                             PDWORD pdwThreadID);

I need to implemenent the following structures in order for this to work: HANDLE, DWORD... and the rest how do I do this?

Upvotes: 0

Views: 4309

Answers (3)

Justin Niessner
Justin Niessner

Reputation: 245479

You don't need to re-implement those types. You just need to translate them to their appropriate .NET equivalent:

Platform Invoke Data Types

You may also be interested in knowing that .NET has its own way of creating both Threads and Processes:

Process Class (System.Diagnostics)

Thread Class (System.Threading)

Upvotes: 5

JainSaw
JainSaw

Reputation: 11

I've found the MSDN topic Windows Data Types to be helpful in identifying the various type aliases used in windows DLLs.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613451

You can use pinvoke.net as a source of Win32 API translations. For example, here are the two you are interested in:

Do be warned that the translations there are of variable quality. For example, there are two versions of CreateThread at the link above. Only one of them is accurate. It's obvious which one it is!

More generally you should have a good read of the MSDN p/invoke tutorial, and Marshaling Data with Platform Invoke.

Upvotes: 3

Related Questions