CreateProcess to launch command prompt
-
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
-
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
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:
-
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:
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
-
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
-
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
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:
-
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
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.