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 / C++ / MFC
  4. CreateProcess to launch command prompt

CreateProcess to launch command prompt

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 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.
  • I Offline
    I Offline
    Imtiaz Murtaza
    wrote on last edited by
    #1

    Friends, I want to launch command prompt window from my application running on Windows Vista. For this purpose i am using CreateProcess. What i want is that command prompt window shoul be launched with Administrative priviliges. Please tell me how can i do so ?

    Imtiaz

    M S R 3 Replies Last reply
    0
    • I Imtiaz Murtaza

      Friends, I want to launch command prompt window from my application running on Windows Vista. For this purpose i am using CreateProcess. What i want is that command prompt window shoul be launched with Administrative priviliges. Please tell me how can i do so ?

      Imtiaz

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      I'm not sure about CreateProcess(). But maybe this:

      ::ShellExecute(NULL, _T("runas"), _T("c:\\Windows\\System32\\cmd.exe"), _T(""), _T(""), SW_SHOWNORMAL);

      Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        I'm not sure about CreateProcess(). But maybe this:

        ::ShellExecute(NULL, _T("runas"), _T("c:\\Windows\\System32\\cmd.exe"), _T(""), _T(""), SW_SHOWNORMAL);

        Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        Not counting the run as administrator part, I'd do it like this:

        // Win32.cpp : Defines the entry point for the application.
        //

        #include "stdafx.h"
        #include <windows.h>
        #include <tchar.h>
        #include <shellapi.h>
        #include <malloc.h>
        #pragma comment(lib, "shell32.lib")
         
        int APIENTRY WinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR lpCmdLine,
        int nCmdShow)
        {
        DWORD size = GetEnvironmentVariable(_T("ComSpec"), NULL, 0);
        LPTSTR pPath = static_cast<LPTSTR>(_alloca(size*sizeof(TCHAR)));
        GetEnvironmentVariable(_T("ComSpec"), pPath, size);
         
        ShellExecute(NULL, NULL, pPath, NULL, NULL, SW_SHOWNORMAL);

        return 0;
        

        }

        This code makes no assumptions about the location of the windows folder or the location and name of the command interpreter.

        Steve

        modified on Wednesday, March 5, 2008 10:08 PM

        M 1 Reply Last reply
        0
        • I Imtiaz Murtaza

          Friends, I want to launch command prompt window from my application running on Windows Vista. For this purpose i am using CreateProcess. What i want is that command prompt window shoul be launched with Administrative priviliges. Please tell me how can i do so ?

          Imtiaz

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          In addition to Mark's reply, see here[^].

          Steve

          1 Reply Last reply
          0
          • S Stephen Hewitt

            Not counting the run as administrator part, I'd do it like this:

            // Win32.cpp : Defines the entry point for the application.
            //

            #include "stdafx.h"
            #include <windows.h>
            #include <tchar.h>
            #include <shellapi.h>
            #include <malloc.h>
            #pragma comment(lib, "shell32.lib")
             
            int APIENTRY WinMain(HINSTANCE hInstance,
            HINSTANCE hPrevInstance,
            LPSTR lpCmdLine,
            int nCmdShow)
            {
            DWORD size = GetEnvironmentVariable(_T("ComSpec"), NULL, 0);
            LPTSTR pPath = static_cast<LPTSTR>(_alloca(size*sizeof(TCHAR)));
            GetEnvironmentVariable(_T("ComSpec"), pPath, size);
             
            ShellExecute(NULL, NULL, pPath, NULL, NULL, SW_SHOWNORMAL);

            return 0;
            

            }

            This code makes no assumptions about the location of the windows folder or the location and name of the command interpreter.

            Steve

            modified on Wednesday, March 5, 2008 10:08 PM

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            Sweet!  Thanks for that!! Just add the runas verb and it will prompt for elevation...

            ShellExecute(NULL, _T("runas"), pPath, NULL, NULL, SW_SHOWNORMAL);

            Filed for future reference, thanks! Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            1 Reply Last reply
            0
            • I Imtiaz Murtaza

              Friends, I want to launch command prompt window from my application running on Windows Vista. For this purpose i am using CreateProcess. What i want is that command prompt window shoul be launched with Administrative priviliges. Please tell me how can i do so ?

              Imtiaz

              R Offline
              R Offline
              Rajkumar R
              wrote on last edited by
              #6

              Imtiaz Murtaza wrote:

              For this purpose i am using CreateProcess. What i want is that command prompt window shoul be launched with Administrative priviliges

              have a look at CreateProcessWithLogon, CreateProcessWithToken, CreateProcessAsUser.

              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