Click here to Skip to main content
15,663,872 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralCreateProcess to launch command prompt Pin
Imtiaz Murtaza5-Mar-08 14:54
Imtiaz Murtaza5-Mar-08 14:54 
GeneralRe: CreateProcess to launch command prompt Pin
Mark Salsbery5-Mar-08 15:35
Mark Salsbery5-Mar-08 15:35 
GeneralRe: CreateProcess to launch command prompt Pin
Stephen Hewitt5-Mar-08 15:57
Stephen Hewitt5-Mar-08 15:57 
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

GeneralRe: CreateProcess to launch command prompt Pin
Mark Salsbery5-Mar-08 16:07
Mark Salsbery5-Mar-08 16:07 
GeneralRe: CreateProcess to launch command prompt Pin
Stephen Hewitt5-Mar-08 15:59
Stephen Hewitt5-Mar-08 15:59 
AnswerRe: CreateProcess to launch command prompt Pin
Rajkumar R5-Mar-08 17:12
Rajkumar R5-Mar-08 17:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.