Advertisement
digemall

Launch a process by setting the curr dir

Mar 12th, 2011
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. class Program
  2. {
  3.     static void Main()
  4.     {
  5.         const uint NORMAL_PRIORITY_CLASS = 0x0020;
  6.  
  7.         bool retValue;
  8.         string Application = Environment.GetEnvironmentVariable("windir") + @"\Notepad.exe";
  9.         string CommandLine = "";
  10.         PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
  11.         STARTUPINFO sInfo = new STARTUPINFO();
  12.         SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
  13.         SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
  14.         pSec.nLength = Marshal.SizeOf(pSec);
  15.         tSec.nLength = Marshal.SizeOf(tSec);
  16.  
  17.         //Open Notepad with C:\ as current directory
  18.  
  19.         string currentDirectory = @"C:\";
  20.  
  21.         retValue = CreateProcess(Application, CommandLine,
  22.         ref pSec, ref tSec, false, NORMAL_PRIORITY_CLASS,
  23.         IntPtr.Zero, currentDirectory, ref sInfo, out pInfo);
  24.  
  25.         Console.WriteLine("Process ID (PID): " + pInfo.dwProcessId);
  26.         Console.WriteLine("Process Handle : " + pInfo.hProcess);
  27.  
  28.         Process p = new Process();
  29.         p.StartInfo = new ProcessStartInfo();
  30.  
  31.     }
  32.  
  33.  
  34.     [DllImport("kernel32.dll")]
  35.     public static extern bool CreateProcess(string lpApplicationName,
  36.         string lpCommandLine,
  37.         ref SECURITY_ATTRIBUTES lpProcessAttributes,
  38.         ref SECURITY_ATTRIBUTES lpThreadAttributes,
  39.         bool bInheritHandles,
  40.         uint dwCreationFlags,
  41.         IntPtr lpEnvironment,
  42.         string lpCurrentDirectory,
  43.         [In] ref STARTUPINFO
  44.         lpStartupInfo,
  45.         out PROCESS_INFORMATION lpProcessInformation);
  46.  
  47.  
  48.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  49.     public struct STARTUPINFO
  50.     {
  51.         public Int32 cb;
  52.         public string lpReserved;
  53.         public string lpDesktop;
  54.         public string lpTitle;
  55.         public Int32 dwX;
  56.         public Int32 dwY;
  57.         public Int32 dwXSize;
  58.         public Int32 dwYSize;
  59.         public Int32 dwXCountChars;
  60.         public Int32 dwYCountChars;
  61.         public Int32 dwFillAttribute;
  62.         public Int32 dwFlags;
  63.         public Int16 wShowWindow;
  64.         public Int16 cbReserved2;
  65.         public IntPtr lpReserved2;
  66.         public IntPtr hStdInput;
  67.         public IntPtr hStdOutput;
  68.         public IntPtr hStdError;
  69.     }
  70.  
  71.     [StructLayout(LayoutKind.Sequential)]
  72.     public struct PROCESS_INFORMATION
  73.     {
  74.         public IntPtr hProcess;
  75.         public IntPtr hThread;
  76.         public int dwProcessId;
  77.         public int dwThreadId;
  78.     }
  79.  
  80.     [StructLayout(LayoutKind.Sequential)]
  81.     public struct SECURITY_ATTRIBUTES
  82.     {
  83.         public int nLength;
  84.         public unsafe byte* lpSecurityDescriptor;
  85.         public int bInheritHandle;
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement