home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2005 November / PCWELT_11_2005.ISO / pcwsoft / Commandbar-Source.z.exe / CommandBar / CreateProcessHook.cs < prev    next >
Encoding:
Text File  |  2002-06-10  |  2.0 KB  |  72 lines

  1. // Pavel Zolnikov[http://www.codeproject.com/script/profile/whos_who.asp?id=35980], 2002
  2.  
  3. using System;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using ZCommon;
  7.  
  8. namespace CommandBar
  9. {
  10.     /// <summary>
  11.     /// Hooks into CreateProcessW API that is called from exporer (through shell32.dll).
  12.     /// </summary>
  13.     /// <remarks>
  14.     /// Makes sure that no CREATE_NEW_CONSOLE flag set so all console output of apps started 
  15.     /// by explorer goes into the Command Prompt Explorer Bar.
  16.     /// </remarks>
  17.     class CreateProcessHook : ApiInterceptor 
  18.     {
  19.         public CreateProcessHook()
  20.             : base("shell32.dll","kernel32.dll","CreateProcessW",new CreateProcessFn(MyCreateProcess)) {}
  21.         
  22.         static bool MyCreateProcess(
  23.             IntPtr    applicationName,   
  24.             IntPtr    commandLine,       
  25.             IntPtr    processAttributes, 
  26.             IntPtr    threadAttributes,  
  27.             Int32    inheritHandles,       
  28.             Int32    creationFlags,      
  29.             IntPtr    environment,       
  30.             IntPtr    currentDirectory,  
  31.             IntPtr    startupInfo,       
  32.             IntPtr    processInformation )
  33.         {
  34.             Win32.STARTUPINFO si = (Win32.STARTUPINFO)Marshal.PtrToStructure(startupInfo,typeof(Win32.STARTUPINFO));
  35.  
  36.             CommandBarObj bar = CommandBarObj.GetBarFromThreadId(Win32.GetCurrentThreadId());
  37.             if( bar != null && bar.Visible )
  38.             {
  39.                 creationFlags &= ~Win32.CREATE_NEW_CONSOLE;
  40.                 si.dwFlags &= ~0x400;//don't know what this flag is but its absence is necessary
  41.             }
  42.  
  43.             bool b = Win32.CreateProcessW(
  44.                 applicationName,   
  45.                 commandLine,       
  46.                 processAttributes, 
  47.                 threadAttributes,  
  48.                 inheritHandles,     
  49.                 creationFlags,     
  50.                 environment,       
  51.                 currentDirectory,  
  52.                 si,
  53.                 processInformation );
  54.  
  55.             return b;
  56.         }
  57.  
  58.         delegate bool CreateProcessFn(
  59.             IntPtr applicationName,   
  60.             IntPtr commandLine,       
  61.             IntPtr processAttributes, 
  62.             IntPtr threadAttributes,  
  63.             Int32  inheritHandles,       
  64.             Int32  creationFlags,      
  65.             IntPtr environment,       
  66.             IntPtr currentDirectory,  
  67.             IntPtr startupInfo,       
  68.             IntPtr processInformation 
  69.             );
  70.     }
  71. }
  72.