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. Execute an exe under particular user account?

Execute an exe under particular user account?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
9 Posts 5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi all, I want to execute an executable file under particular user account in my C# application. can any body tell me how to do the same? Thanking You, Sunil G.

    L T C 3 Replies Last reply
    0
    • L Lost User

      Hi all, I want to execute an executable file under particular user account in my C# application. can any body tell me how to do the same? Thanking You, Sunil G.

      T Offline
      T Offline
      toronja72
      wrote on last edited by
      #2

      you could check the "runas" command specify the executable and particular user as arguments.

      1 Reply Last reply
      0
      • L Lost User

        Hi all, I want to execute an executable file under particular user account in my C# application. can any body tell me how to do the same? Thanking You, Sunil G.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        using Process.Start[^], it supports launching a process under a particular user;

        MSDN states:

        Use this overload to create a new process and its primary thread by specifying its file name, command-line arguments, user name, password, and domain. The new process then runs the specified executable file in the security context of the specified credentials (user, domain, and password).

        I are Troll :suss:

        L 1 Reply Last reply
        0
        • L Lost User

          using Process.Start[^], it supports launching a process under a particular user;

          MSDN states:

          Use this overload to create a new process and its primary thread by specifying its file name, command-line arguments, user name, password, and domain. The new process then runs the specified executable file in the security context of the specified credentials (user, domain, and password).

          I are Troll :suss:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I want to execute using current logged on user token.

          L 1 Reply Last reply
          0
          • L Lost User

            Hi all, I want to execute an executable file under particular user account in my C# application. can any body tell me how to do the same? Thanking You, Sunil G.

            C Offline
            C Offline
            Covean
            wrote on last edited by
            #5

            You have got an eMail. (I hope :laugh: ). Hope that helps you.

            Greetings Covean

            S L 2 Replies Last reply
            0
            • L Lost User

              I want to execute using current logged on user token.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Then you'd only need to start it, without giving credentials at all - and the process will be started under the credentials of the current (signed in) user :)

              I are Troll :suss:

              1 Reply Last reply
              0
              • C Covean

                You have got an eMail. (I hope :laugh: ). Hope that helps you.

                Greetings Covean

                S Offline
                S Offline
                Sunil G 3
                wrote on last edited by
                #7

                Yeh i got ur mail. Thank u very much.

                1 Reply Last reply
                0
                • C Covean

                  You have got an eMail. (I hope :laugh: ). Hope that helps you.

                  Greetings Covean

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  questions should be answered within the thread, and not by e-mail. That way anyone can benefit. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                  C 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    questions should be answered within the thread, and not by e-mail. That way anyone can benefit. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                    C Offline
                    C Offline
                    Covean
                    wrote on last edited by
                    #9

                    Yes there you are right but I send him more than 300 lines of code. I think this is tooo long for this forum. But here a short version (with out DLLImports, structs, ...):

                        public static void StartProcessOnActiveConsole(string szApplication, string szArguments, string szDirectory)
                        {
                            IntPtr hUserToken = IntPtr.Zero;
                            IntPtr hDuplicatedUserToken = IntPtr.Zero;
                            IntPtr lpEnvironment = IntPtr.Zero;
                            IntPtr hProcess = IntPtr.Zero;
                            IntPtr hThread = IntPtr.Zero;
                            try
                            {
                                uint dwSessionId = 0xFFFFFFFF;
                                dwSessionId = WTSGetActiveConsoleSessionId();
                                if (dwSessionId == 0xFFFFFFFF)  // no active session
                                    return;
                    
                                 if(!WTSQueryUserToken(dwSessionId, ref hUserToken))
                                    throw new Exception("WTSQueryUserToken failed. GetLastError() = " + Marshal.GetLastWin32Error());
                    
                                if(!DuplicateTokenEx(hUserToken, 0x02000000 /\*MAXIMUM\_ALLOWED\*/, IntPtr.Zero, SECURITY\_IMPERSONATION\_LEVEL.SecurityImpersonation, TOKEN\_TYPE.TokenPrimary, ref hDuplicatedUserToken))
                                    throw new Exception("DuplicateTokenEx failed. GetLastError() = " + Marshal.GetLastWin32Error());
                    
                                if(!CreateEnvironmentBlock(ref lpEnvironment, hDuplicatedUserToken, false))
                                    throw new Exception("CreateEnvironmentBlock failed. GetLastError() = " + Marshal.GetLastWin32Error());
                    
                                STARTUPINFO startupInfo = new STARTUPINFO();
                                startupInfo.dwSize = (uint)Marshal.SizeOf(startupInfo);
                                startupInfo.pReserved = IntPtr.Zero;
                                startupInfo.szDesktop = "winsta0\\\\default";
                                startupInfo.pTitle = IntPtr.Zero;
                                startupInfo.dwX = 0;
                                startupInfo.dwY = 0;
                                startupInfo.dwXSize = 0;
                                startupInfo.dwYSize = 0;
                                startupInfo.dwXCountChars = 0;
                                startupInfo.dwYCountChars = 0;
                                startupInfo.dwFillAttribute = 0;
                                startupInfo.dwFlags = 128 /\*STARTF\_FORCEOFFFEEDBACK\*/;
                                startupInfo.wShowWindow = 0;
                                startupInfo.wReserved2 = 0;
                                startupInfo.lpReserved2 = IntPtr.Zero;
                                startupInfo.hStdInput = IntPtr.Zero;
                                startupInfo.hStdOutput = IntPtr.Zero;
                                startupInfo.hStdError = IntPtr.Zero;
                    
                                PROCESS\_INFORMATION processIn
                    
                    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