Reputation: 41
I wrote this code to launch processes by user choice. I want to change the environment variable for the cmd shell so in the window it shows "speak to me>" instead of "C:\path>" but also can access cmd commands. I know I have to set some value instead of NULL using getenv() or putenv() function. I wrote lib function for that but I don't know how to implement this in place of Null in the create process.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
// function prototype
void printError(char* functionName);
void lib(char* myFunction);
void lib(char* myFunction)
{
char *lib;
lib = getenv( "LIB" );
if( lib != NULL )
printf( "Original LIB variable is: %s\n", lib );
_putenv( "LIB=speak to me>" );
lib = getenv( "LIB" );
if( lib != NULL )
printf( " %s\n", lib );
}
int main()
{
int i, choice;
#define NUMBER_OF_PROCESSES 2
LPTSTR lpCommandLine[NUMBER_OF_PROCESSES];
PROCESS_INFORMATION processInfo[NUMBER_OF_PROCESSES];
STARTUPINFO startInfo;
ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.cb = sizeof(startInfo);
startInfo.lpTitle = "What is your Command?";
startInfo.dwFillAttribute = FOREGROUND_BLUE| BACKGROUND_RED| BACKGROUND_GREEN| BACKGROUND_BLUE| BACKGROUND_INTENSITY;
startInfo.dwFlags = STARTF_USEFILLATTRIBUTE;
/* set up the command lines */
lpCommandLine[0] = "C:\\Windows\\notepad.exe";
lpCommandLine[1] = "C:\\Windows\\system32\\cmd.exe";
/* create the processes with user choice */
while(1)
{
printf("\n Please choose from the following list\n");
printf(" 0.Quit\n");
printf(" 1.Run Notepad\n");
printf(" 2.Run Cmd shell\n");
printf(" enter number:");
scanf("%d",&choice);
switch(choice)
{
case 1:
if( !CreateProcess(NULL, lpCommandLine[0], NULL, NULL, FALSE,
HIGH_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
NULL, NULL, &startInfo, &processInfo[0]) )
{
printError("CreateProcess");
}
else
{
printf("Started program %d with pid = %d\n\n",1 , (int)processInfo[0].dwProcessId);
}
break;
case 2:
if( !CreateProcess(NULL, lpCommandLine[1], NULL, NULL, FALSE,
HIGH_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
NULL, NULL, &startInfo, &processInfo[1]) )
{
printError("CreateProcess");
}
else
{
printf("Started program %d with pid = %d\n\n",2 , (int)processInfo[1].dwProcessId);
}
break;
case 0:
exit(0);
break;
default: printf("wrong choice");
break;
}
}
/* close all the handles */
for (i = 0; i < NUMBER_OF_PROCESSES; i++)
{
CloseHandle(processInfo[i].hThread);
CloseHandle(processInfo[i].hProcess);
}
return 0;
}
/****************************************************************
The following function can be used to print out "meaningfull"
error messages. If you call a Win32 function and it returns
with an error condition, then call this function right away and
pass it a string containing the name of the Win32 function that
failed. This function will print out a reasonable text message
explaining the error and then (if chosen) terminate the program.
*/
void printError(char* functionName)
{
LPVOID lpMsgBuf;
int error_no;
error_no = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error_no,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the string.
fprintf(stderr, "\n%s failed on error %d: ", functionName, error_no);
fprintf(stderr, (const char*)lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
//ExitProcess(1); // terminate the program
}//printError
Upvotes: 1
Views: 2948
Reputation: 21542
The PROMPT
environment variable can be set to something other than the default $p$g
if you like; here is its usage:
PROMPT [text]
text Specifies a new command prompt.
Prompt can be made up of normal characters and the following special codes:
$A & (Ampersand)
$B | (pipe)
$C ( (Left parenthesis)
$D Current date
$E Escape code (ASCII code 27)
$F ) (Right parenthesis)
$G > (greater-than sign)
$H Backspace (erases previous character)
$L < (less-than sign)
$N Current drive
$P Current drive and path
$Q = (equal sign)
$S (space)
$T Current time
$V Windows version number
$_ Carriage return and linefeed
$$ $ (dollar sign)
To pass this variable in a call to CreateProcess
you can use the lpEnvironment
parameter:
CreateProcess(NULL, lpCommandLine[1], NULL, NULL, TRUE,
HIGH_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
(LPVOID) _T("PROMPT=speak to me$g\0\0"), NULL, &startInfo, &processInfo[1]);
Upvotes: 1