0

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?

3
  • What have you done thus far? Commented Apr 9, 2013 at 19:00
  • I know how to use the function in C++, but I want to learn how to use them in C# also, I need a place where I can see The HANDLE structure so I can write the HANDKE structure in C#, I hope you understand what I mean. Commented Apr 9, 2013 at 19:03
  • Other people have answered, so I'm just going to slip this in here [msdn.microsoft.com/en-us/library/…. Though my question is why are you going for this and not trying for C# multithreading? Commented Apr 9, 2013 at 19:21

3 Answers 3

5

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! I'm new to Api functions. This is the best answer to my question
FWIW, you need to do more than just map the types. If that's all there was to p/invoke, this would not exist: stackoverflow.com/questions/tagged/pinvoke
@DavidHeffernan - There's more to P/Invoke, yes. But the OP specifically asked about re-implementing the data types. In that case, finding the appropriate .NET type to use is appropriate.
Well, right off the bat, your link (which is certainly useful) does not have PSECURITY_ATTRIBUTES, PTHREAD_START_ROUTINE, PDWORD, STARTUPINFO or PROCESS_INFORMATION. These are two of the more complex Win32 APIs to use and just knowing that DWORD maps to uint and HANDLE to UIntPtr is really just scratching at the surface. My comments here are aimed at @juice in any case. You certainly answered the question that was asked.
3

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.

3 Comments

Kernel32 version of CreateProcess
Thanks for the answer, that is really interesting!
@CodesInChaos Thanks, I was sloppy, I fixed the link
1

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

1 Comment

Thank you, though the above answer was much more usefull to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.