<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx</link><description>I came across this issue recently. I was trying to access CreateProcess using DLLImport from managed code using C# and VB.Net. Here is my code from C#: using System; using System.Diagnostics; using System.Runtime.InteropServices; public struct PROCESS_INFORMATION</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Interesting Finds: August 10 part 2 and August 11, 2006</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#696191</link><pubDate>Sat, 12 Aug 2006 06:24:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:696191</guid><dc:creator>Jason Haley</dc:creator><description /></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#696571</link><pubDate>Sat, 12 Aug 2006 17:43:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:696571</guid><dc:creator>ShayEr</dc:creator><description>a. Any reason why the Process class is not enough?&lt;br&gt;b. are you aware of &lt;a rel="nofollow" target="_new" href="http://www.pinvoke.net/default.aspx/coredll/CreateProcess.html"&gt;http://www.pinvoke.net/default.aspx/coredll/CreateProcess.html&lt;/a&gt;&lt;br&gt;</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#699409</link><pubDate>Mon, 14 Aug 2006 14:59:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:699409</guid><dc:creator>Andrés</dc:creator><description>Could you explain a little bit more about why structs are compiled as classes and what are the implications of that? What about that attribute??&lt;br&gt;&lt;br&gt;Thanks&lt;br&gt;</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#699731</link><pubDate>Mon, 14 Aug 2006 19:31:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:699731</guid><dc:creator>thottams@microsoft.com</dc:creator><description>Using porocess is another alternative. I am aware of pinvoke.net. The sample for CreateProcess from there did not work for me.&lt;br&gt;&lt;br&gt;It was my mistake on the wording. I had authored the struct as classes in VB and hence the problem. I changed them to structs and it worked fine.</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#699911</link><pubDate>Mon, 14 Aug 2006 21:24:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:699911</guid><dc:creator>RonO</dc:creator><description>Did you update the sample (and anything else you found to be incorrect) for the pinvoke entry? &amp;nbsp;:)</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#700122</link><pubDate>Tue, 15 Aug 2006 00:11:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:700122</guid><dc:creator>Thottam Sriram</dc:creator><description>The sample represented PROCESS_INFORMATION and SECURITY_ATTRIBUTES and STARTUPINFO as classes in VB. This made the call to fail. I changed them to struct in VB and I could get notepad up and running from the VB code! The working sample is the code above.</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#702551</link><pubDate>Wed, 16 Aug 2006 17:19:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:702551</guid><dc:creator>Austin</dc:creator><description>I don't understand the desire to use P/Invoke for this. &amp;nbsp;The managed world already has a Process class for doing all these sorts of things. &amp;nbsp;It'll not only be more performant (by avoiding P/Invoke), but also be easier for future maintainers of any code to understand and modify, and less error-prone.&lt;br&gt;&lt;br&gt;Can you explain why you would ever want to use this over the normal Process class of the .NET Base Class Library?</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#703126</link><pubDate>Thu, 17 Aug 2006 02:02:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:703126</guid><dc:creator>Thottam Sriram</dc:creator><description>I totally agree with you and would think that to be a better decision. &lt;br&gt;&lt;br&gt;There were a few questions on the web relating to this not working and I just wanted to aaddress that. Regardig using this over existing Process class, I agree with you that it will be a better choice.&lt;br&gt;&lt;br&gt;Question to users:&lt;br&gt;If you are using it, can you explain the scenario that force you to using this?</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#712603</link><pubDate>Tue, 22 Aug 2006 16:24:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:712603</guid><dc:creator>Andy</dc:creator><description>One reason for wanting to do this is:&lt;br&gt;&lt;br&gt;Open a socket for listening in the parent process.&lt;br&gt;&lt;br&gt;Create a child process using Process class.&lt;br&gt;&lt;br&gt;Kill parent.&lt;br&gt;&lt;br&gt;Socket is still open for listening and doesn't close until the child process exits. I cannot see anyway of just using the Process class from stopping this from working. I suspect the call to CreateProcess has inherit handle set to true. Which seems like a bug to me.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#1237221</link><pubDate>Fri, 08 Dec 2006 10:12:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1237221</guid><dc:creator>Dewald Swaneoel</dc:creator><description>&lt;p&gt;Here's one reason why one would prefer to use CreateProcess this way as opposed to the managed code of System.Diagnostics.Process is that the Process class quite simply does not exist in the .NET Compact Framework 1.0&lt;/p&gt;
&lt;p&gt;So while Microst assumes that the minute hey shipped the newer version of the compact framework all older devices stopped existing, a few unlucy souls such as myself still have to develop for devices running the CF 1.0&lt;/p&gt;
&lt;p&gt;I am very grateful to have stmbled across this article for that very reason.&lt;/p&gt;
</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#1565017</link><pubDate>Wed, 31 Jan 2007 15:54:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1565017</guid><dc:creator>jon nolen</dc:creator><description>&lt;p&gt;Sorry to be a month late on this&amp;gt; &amp;nbsp;you also have to pInvoke to create a process if you are trying to run the process in a different user context (some WTS applications).&lt;/p&gt;
</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#1620817</link><pubDate>Wed, 07 Feb 2007 20:55:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1620817</guid><dc:creator>thottams@microsoft.com</dc:creator><description>&lt;p&gt;These are some good scenarios for this.&lt;/p&gt;
&lt;p&gt;Can't you use System.Diagnostics.Process to create a process after you impersonate an user? I have not tried it, but would be interesting to know. &lt;/p&gt;
</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#2461067</link><pubDate>Mon, 07 May 2007 13:29:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2461067</guid><dc:creator>Vladimir Jelovac</dc:creator><description>&lt;p&gt;Great article!!! Thanks for help :)&lt;/p&gt;
&lt;p&gt;Vlada, jelovac.vladimir@cont.co.yu&lt;/p&gt;
</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#3669878</link><pubDate>Tue, 03 Jul 2007 14:27:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3669878</guid><dc:creator>SuspendedUser</dc:creator><description>&lt;p&gt;Another reason would be that you cannot create a process in suspended mode with the .NET Process class.&lt;/p&gt;
</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#4040529</link><pubDate>Wed, 25 Jul 2007 12:14:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4040529</guid><dc:creator>Er. Kartikay Malhotra</dc:creator><description>&lt;p&gt;Beauty lies in utility &amp;amp; simplicity.&lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;
</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#8961980</link><pubDate>Tue, 23 Sep 2008 09:56:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8961980</guid><dc:creator>Reasonator</dc:creator><description>&lt;p&gt;One reason to use this instead of Process.Start is that Process.Start has a upper limit of maximum 2048 characters passed as parameter to the process.&lt;/p&gt;
&lt;p&gt;This method has a maximum of 32000-something&lt;/p&gt;
</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#9141172</link><pubDate>Tue, 25 Nov 2008 11:33:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9141172</guid><dc:creator>doboloh</dc:creator><description>&lt;p&gt;thanks very useful in creating a new Process group with C#,&lt;/p&gt;
&lt;p&gt;i think System.Diagnostics.Process lacks this&lt;/p&gt;
</description></item><item><title>Window's Handle</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#9196795</link><pubDate>Thu, 11 Dec 2008 16:54:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9196795</guid><dc:creator>Yaniv</dc:creator><description>&lt;p&gt;Thank you, that was very very useful.&lt;/p&gt;
&lt;p&gt;Tell me, how do you find the Window's handle ? (HWND) from createprocess ?&lt;/p&gt;
</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#9853604</link><pubDate>Thu, 30 Jul 2009 21:09:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9853604</guid><dc:creator>Troy Howard</dc:creator><description>&lt;p&gt;A little late on this very old blog post, but I recently used CreateProcess to enable a weird situation. &lt;/p&gt;
&lt;p&gt;Basically, I had an existing commandline application, which was originally developed for *nix... For it's output, it needed a seekable output file handle. The standard file handles presented to a process are all either read or write (ie, the file handles created from CreatePipe), so I wrote a simple wrapper application that setup a pipe for STDIN, but setup a seekable file handle for it's output via CreateFile. Without access to the STARTUPINFO struct, I could not have redirected the output in the correct way. The redirection provided by System.Diagnostics.Process only allow for TextReaders/TextWriters, and that's because they are operating against handles created via the standard CreatePipe call. &lt;/p&gt;
&lt;p&gt;So, in short, there was no way to do what I needed to do with the managed classes, so I had to re-create all that functionality with P/Invoke calls... &lt;/p&gt;
&lt;p&gt;Which reminds me... A commenter here posted that using P/Invoke is slower for this task,as compared to just the managed Process class. I invite him to disassemble the code using Reflector and realize that all that class does is wrap P/Invoke calls. In fact, it does a lot of extra work, than simply calling CreateProcess directly, and is likely slower due to the additional overhead. Though the difference in speed is probably so slight that it would be hard to measure. &lt;/p&gt;
&lt;p&gt;Regarding the last poster's question (Yaniv from Dec. 2008) &amp;nbsp;you can obtain the window handle(s) (there can be more than one per process) for a given process id by iterating across all windows (Call GetTopWindow, then GetNextWindow in a loop), then passing the window handle to GetWindowThreadProcessId to retreive it's processid, and check to see if that's the processid you're looking for... It's a little roundabout, but it does the job. You obtain the processid from PROCESS_INFORMATION.hProcess field after calling CreateProcess.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Troy&lt;/p&gt;
</description></item><item><title>re: Accessing CreateProcess from C# and VB.NET</title><link>http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx#9906042</link><pubDate>Mon, 12 Oct 2009 08:51:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9906042</guid><dc:creator>Venu</dc:creator><description>&lt;p&gt;I tried this code, it works on Win xp and does not on citrix.&lt;/p&gt;
&lt;p&gt;I use createprocess to call AcroRd32.exe with switches (args) to print.&lt;/p&gt;
&lt;p&gt;AcroRd32.exe /h /t &amp;quot;mypdf&amp;quot; &amp;quot;myprinter&amp;quot;&lt;/p&gt;
&lt;p&gt;Any ideas why this does not work on citrix?&lt;/p&gt;
&lt;p&gt;How can i debug this code.?&lt;/p&gt;
&lt;p&gt;Appreciate any input.&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;
</description></item></channel></rss>