views:

184

answers:

3

Is there a way in C# to start a process with a certain stack size?

A: 

In this thread they show a PoC that you can use Editbin.exe to modify the default stack size of the thread that enters main: http://bytes.com/topic/c-sharp/answers/229335-stack-size

and if you spawn new threads, there's an API for it: http://msdn.microsoft.com/en-us/library/ms149581.aspx

Henrik
-1: he asked about a process, and please don't post answers that are nothing but links.
John Saunders
A: 

You cannot start a process with a particular stack size because processes don't have stacks. Threads have stacks.

I don't believe there's a way to start a process and to tell it that it should start its threads with a particular stack size.

John Saunders
If you read my links you'll find that you are wrong.
Henrik
No, I'm not. He said he wanted to start a process with a particular stack size. He did not say he wanted to modify a binary so that it always starts with a particular stack size. It's possible that's what he _wants_, but it's not what he asked for.
John Saunders
Nitpicking and a very unhelpful attitude. If you modify the binary which translates to a process when run, so that its stacksize always is bigger, then the process' first threads' stack size, i.e. the process' stack size will be bigger, which is what he asked.
Henrik
This assumes that the goal is to always run that binary with the larger stack size.
John Saunders
Yes. :) Let's see what the OP says then.
Henrik
@Downvoter: have some guts and say what you mean. "-1" doesn't inform anyone of what the problem is.
John Saunders
+1  A: 

A down-voting fest, I hesitate to post. But Henrik is right, the very first thread is started by Windows when it launches the EXE. It does a whole bunch of work, loading the CLR is one of its duties. And it runs the Main() method in your program.

The .NET framework gives very few options to configure that thread. Only the [MTAThread] and [STAThread] attributes on the Main() method makes a difference, they affect how the CLR calls the CoInitializeEx() API function.

The stack size of the thread is in fact configurable. It is one of the fields in the PE32 file format, the format used in Windows for executable images. Usually the C# or VB.NET compiler is responsible for generating that file, neither have an option to set the initial thread stack size. Bit of an oversight. They use defaults, one megabyte for a 32-bit EXE, four megabytes for a 64-bit EXE (Platform Target = x64).

Changing that value is possible, you can run the Editbin.exe utility to modify the EXE file, use the /STACK command line option. You'll want to do this in a post-build step. Beware that this is incompatible with strong names or signing with a certificate since it modifies the EXE file.

It isn't a real problem btw, if you need a thread with a lot of stack space then you create one yourself in the Main() method.

Hans Passant