Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to open notepad from windows service

How to open notepad from windows service

Scheduled Pinned Locked Moved C#
helptutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ravindradonkada
    wrote on last edited by
    #1

    Hi , This is ravindra,currently working on Windows Service. I implemented the following two methods to call notepad.exe from windows service,but its not working.So,please anybody help to complete my task. Method1: ======= STARTUPINFO si = new STARTUPINFO(); PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); CreateProcess("C:\\WINDOWS\\SYSTEM32\\notepad.exe", null, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi); EventLog.WriteEntry("After notepad"); } [DllImport("kernel32.dll")] static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); } } Method2: ======== EventLog.WriteEntry("In the notepad"); ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "notepad.exe"; psi.UseShellExecute = false; psi.Domain = "sys43."; psi.UserName = "Acer OEM User"; SecureString secure = new SecureString(); string password = "nopassword"; foreach (char c in password) { secure.AppendChar(c); } psi.Password = secure; Process process = new Process(); process.StartInfo = psi; process.Start();

    Mircea PuiuM J 3 Replies Last reply
    0
    • R ravindradonkada

      Hi , This is ravindra,currently working on Windows Service. I implemented the following two methods to call notepad.exe from windows service,but its not working.So,please anybody help to complete my task. Method1: ======= STARTUPINFO si = new STARTUPINFO(); PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); CreateProcess("C:\\WINDOWS\\SYSTEM32\\notepad.exe", null, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi); EventLog.WriteEntry("After notepad"); } [DllImport("kernel32.dll")] static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); } } Method2: ======== EventLog.WriteEntry("In the notepad"); ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "notepad.exe"; psi.UseShellExecute = false; psi.Domain = "sys43."; psi.UserName = "Acer OEM User"; SecureString secure = new SecureString(); string password = "nopassword"; foreach (char c in password) { secure.AppendChar(c); } psi.Password = secure; Process process = new Process(); process.StartInfo = psi; process.Start();

      Mircea PuiuM Offline
      Mircea PuiuM Offline
      Mircea Puiu
      wrote on last edited by
      #2

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Diagnostics;

      namespace ConsoleApplication1
      {
      class Program
      {
      static void Main(string[] args)
      {
      Process p = new Process();
      p.StartInfo.FileName = "notepad.exe";
      //p.StartInfo.Arguments = "yourFileName.txt";
      p.Start();
      }
      }
      }

      SkyWalker

      S 1 Reply Last reply
      0
      • Mircea PuiuM Mircea Puiu

        using System;
        using System.Collections.Generic;
        using System.Text;
        using System.Diagnostics;

        namespace ConsoleApplication1
        {
        class Program
        {
        static void Main(string[] args)
        {
        Process p = new Process();
        p.StartInfo.FileName = "notepad.exe";
        //p.StartInfo.Arguments = "yourFileName.txt";
        p.Start();
        }
        }
        }

        SkyWalker

        S Offline
        S Offline
        Stu Richardson
        wrote on last edited by
        #3

        This is how you would open Notepad from a win forms application however this won't work from a windows service. A service does not have a "display" to show the notepad application. I have had a look around and had a play but I cant get this to work. Sorry. :( Hopefully someone else might have some better ideas. Also be sure to consider the account the service is running under, some accounts will restrict such activities.

        Mircea PuiuM 1 Reply Last reply
        0
        • S Stu Richardson

          This is how you would open Notepad from a win forms application however this won't work from a windows service. A service does not have a "display" to show the notepad application. I have had a look around and had a play but I cant get this to work. Sorry. :( Hopefully someone else might have some better ideas. Also be sure to consider the account the service is running under, some accounts will restrict such activities.

          Mircea PuiuM Offline
          Mircea PuiuM Offline
          Mircea Puiu
          wrote on last edited by
          #4

          it was no win form outhere :sigh: It was a console example. ... and starting a process has nothing to do with any display ...

          SkyWalker

          J 1 Reply Last reply
          0
          • Mircea PuiuM Mircea Puiu

            it was no win form outhere :sigh: It was a console example. ... and starting a process has nothing to do with any display ...

            SkyWalker

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Mircea Puiu wrote:

            . and starting a process has nothing to do with any display ...

            It does if its from a windows service which is not marked as "Interactive". Without a display to start a windowed process like notepad, it will not show up if started from the service.

            Mircea PuiuM 1 Reply Last reply
            0
            • J J4amieC

              Mircea Puiu wrote:

              . and starting a process has nothing to do with any display ...

              It does if its from a windows service which is not marked as "Interactive". Without a display to start a windowed process like notepad, it will not show up if started from the service.

              Mircea PuiuM Offline
              Mircea PuiuM Offline
              Mircea Puiu
              wrote on last edited by
              #6

              of course :-)

              SkyWalker

              1 Reply Last reply
              0
              • R ravindradonkada

                Hi , This is ravindra,currently working on Windows Service. I implemented the following two methods to call notepad.exe from windows service,but its not working.So,please anybody help to complete my task. Method1: ======= STARTUPINFO si = new STARTUPINFO(); PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); CreateProcess("C:\\WINDOWS\\SYSTEM32\\notepad.exe", null, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi); EventLog.WriteEntry("After notepad"); } [DllImport("kernel32.dll")] static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); } } Method2: ======== EventLog.WriteEntry("In the notepad"); ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "notepad.exe"; psi.UseShellExecute = false; psi.Domain = "sys43."; psi.UserName = "Acer OEM User"; SecureString secure = new SecureString(); string password = "nopassword"; foreach (char c in password) { secure.AppendChar(c); } psi.Password = secure; Process process = new Process(); process.StartInfo = psi; process.Start();

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #7

                Windows services generally are not required to show other applications. You can check the box to mark the service as "Interactive" which will allow it limited use of the logged-in users screen. Without testing, I would think that marking it interactive and using the standard Process.Start code that is ubiquitous would result in the behaviour you are expecting.

                S 1 Reply Last reply
                0
                • R ravindradonkada

                  Hi , This is ravindra,currently working on Windows Service. I implemented the following two methods to call notepad.exe from windows service,but its not working.So,please anybody help to complete my task. Method1: ======= STARTUPINFO si = new STARTUPINFO(); PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); CreateProcess("C:\\WINDOWS\\SYSTEM32\\notepad.exe", null, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi); EventLog.WriteEntry("After notepad"); } [DllImport("kernel32.dll")] static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); } } Method2: ======== EventLog.WriteEntry("In the notepad"); ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "notepad.exe"; psi.UseShellExecute = false; psi.Domain = "sys43."; psi.UserName = "Acer OEM User"; SecureString secure = new SecureString(); string password = "nopassword"; foreach (char c in password) { secure.AppendChar(c); } psi.Password = secure; Process process = new Process(); process.StartInfo = psi; process.Start();

                  Mircea PuiuM Offline
                  Mircea PuiuM Offline
                  Mircea Puiu
                  wrote on last edited by
                  #8

                  You do not normally start an interactive process from a Windows Service. Probably, it is an option in your case when debugging something (then you must enable the service to interact with the desktop, as already said by someone here). The reason is that each service runs in an invisible secured desktop environment, therefore no user interface is shown to users.

                  SkyWalker

                  1 Reply Last reply
                  0
                  • J J4amieC

                    Windows services generally are not required to show other applications. You can check the box to mark the service as "Interactive" which will allow it limited use of the logged-in users screen. Without testing, I would think that marking it interactive and using the standard Process.Start code that is ubiquitous would result in the behaviour you are expecting.

                    S Offline
                    S Offline
                    Stu Richardson
                    wrote on last edited by
                    #9

                    It does - this was the step I was missing when I attempted it.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups