Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / July 2004

Tip: Looking for answers? Try searching our database.

Attach a .NET Stream Class to StdOut/StdErro in StartupInfo for CreateProcessWithLogonW???

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Shawn Anderson - 15 Jul 2004 17:37 GMT
Anyone know how to do this?  I have CreateProcessWithLogonW working in my C#
app, but I cannot seem to be able to attach a handle to the StdOutput in the
STARTUPINFO structure -- no matter what I do, I get a "Invalid Handle" error
(Win32 error 6)

Here is some code for anyone that is interested :)

  AdvApi32.StartupInfo si    = new AdvApi32.StartupInfo();
  AdvApi32.ProcessInfoEx pi    = new AdvApi32.ProcessInfoEx();

  IntPtr       hRead;
  IntPtr       hWrite;
  Kernel32.SECURITY_ATTRIBUTES sa = new
CriticalSites.Win32.Kernel32.SECURITY_ATTRIBUTES();

  sa.nLength    = Marshal.SizeOf(typeof(Kernel32.SECURITY_ATTRIBUTES));
  sa.bInheritHandle  = true;
  sa.lpSecurityDescriptor = IntPtr.Zero;

  si.cb   = Marshal.SizeOf(typeof(AdvApi32.StartupInfo));
  si.dwFlags  = 0;
  si.wShowWindow = User32.ShowWindowFlags.SW_HIDE;
  si.desktop  = "";
  si.lpReserved = String.Empty;
  si.cbReserved2 = 0;
  si.lpReserved2 = IntPtr.Zero;
  si.dwFlags  |= (int) AdvApi32.StartFFlags.STARTF_USESHOWWINDOW;
  si.hStdError = IntPtr.Zero;
  si.hStdInput = IntPtr.Zero;
  si.hStdOutput = IntPtr.Zero;

  if (psi.RedirectStandardError)
  {
   hRead = IntPtr.Zero;
   hWrite = IntPtr.Zero;

   if (!Kernel32.CreatePipe(ref hRead, ref hWrite, ref sa, 0))
   {
    throw new
System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
   }

  if (si.hStdError != IntPtr.Zero || si.hStdOutput != IntPtr.Zero ||
si.hStdOutput != IntPtr.Zero)
  {
   si.dwFlags |= (int) AdvApi32.StartFFlags.STARTF_USESTDHANDLES;
  }

   si.hStdError = hRead;
  }

  if (psi.RedirectStandardOutput)
  {
   hRead = IntPtr.Zero;
   hWrite = IntPtr.Zero;

   if (!Kernel32.CreatePipe(ref hRead, ref hWrite, ref sa, 0))
   {
    throw new
System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
   }

   si.hStdOutput = hRead;
  }

  if (!AdvApi32.CreateProcessWithLogonW(sUserName, sDomain, sPassword,
AdvApi32.LogonFlags.LOGON_WITH_PROFILE, psi.FileName, psi.Arguments,
iCreateFlags, IntPtr.Zero, psi.WorkingDirectory, ref si, out pi))
  {
   throw new
System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); <-- this
is always throw with Win32 Error 6
  }
Mattias Sj?gren - 15 Jul 2004 18:48 GMT
Shawn,

According to

http://msdn.microsoft.com/library/en-us/dllproc/base/creating_a_child_process_wi
th_redirected_input_and_output.asp


you have to use DuplicateHandle to get non-inheritable handles.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Shawn Anderson - 15 Jul 2004 22:43 GMT
Thanks for the thought :)  I gave it a try, and am still getting the exist
same error :-/

Shawn

> Shawn,
>
[quoted text clipped - 5 lines]
>
> Mattias
Mattias Sj?gren - 16 Jul 2004 22:54 GMT
Shawn,

>Thanks for the thought :)  I gave it a try, and am still getting the exist
>same error :-/

OK, after looking further at your code, there are some other things
that I find confusing:

>if (si.hStdError != IntPtr.Zero || si.hStdOutput != IntPtr.Zero ||
>si.hStdOutput != IntPtr.Zero)
>{
> si.dwFlags |= (int) AdvApi32.StartFFlags.STARTF_USESTDHANDLES;
>}

At this point, you haven't assigned anything to any of those handles
in the StartupInfo struct other than IntPtr.Zero (unless you left out
some code). Does that mean you never set the STARTF_USESTDHANDLES
flag?

>si.hStdOutput = hRead;

Here you're assinging the *read* handle to the output. Surely it's the
*write* handle you should pass to the new process, since it will write
to those pipes.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Shawn Anderson - 16 Jul 2004 23:54 GMT
No, it means that I cut and pasted the code in the wrong order.

but good catch...

Shawn

> Shawn,
>
[quoted text clipped - 22 lines]
>
> Mattias

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.