Jump to content

Featured Replies

Posted

i was bored so i decided to write this. it helps me keep a list of programs i want to run as my computer starts up. this is the only key in the RUN key so any other ones are either viruses or newly installed software

 

you also need a text file name Process.txt in the same directory as this program

-the first line of that file is the number of programs you want to start up

-the following lines are the paths to the programs

-each line can only have 1 path

 

If there is anything wrong, plz PM me or post the error here

 

Enjoy

 

/*
 Name: ProcessStarter
 Copyright: 2005
 Author: darkurza
 Date: 19/07/05 12:42
 Description: *This program is written
              by darkurza. No one else
              may take credit for this!*
              
              The only program that
              runs on startup.  Once
              it runs, it reads from
              a text file and gathers
              the paths of other 
              programs needed to run.
              To run more programs at
              startup, add their paths
              into the text file.
*/

#include <windows.h>
#include <stdio.h>
#include <string.h>

#define TIMER 5000

void ReplaceSpace(char *Input);

int main()
{
   /* Variables */
   int NumberOfProcesses;
   int ProcessCounter = 0;
   char ProcessPath[256];
   char ProcessFilePath[256];
   char ExecutablePath[256];
   
   /* Find the path to
      the process file.
   */
   GetCurrentDirectory(256, ProcessFilePath);
   strcat(ProcessFilePath, "\\Process.txt");
   
   /* Open Process.txt
      and run all 
      programs listed.
   */
   FILE *READ;
   READ = fopen(ProcessFilePath, "r");
   if (READ != NULL) //If Process.txt Exists...
   {
       /* Reads the number
          of processes
          listed in file.
       */
       fscanf(READ, "%d", &NumberOfProcesses);
       /* Runs the number 
          of processes
          obtained in
          'NumberOfProcesses'.
       */
       printf("\t -Darkurza's ProcessStarter!- \n\n");
       printf("\n -Waiting for computer to fully load up.\n\n");
       
       Sleep(TIMER);
       
       while(ProcessCounter != NumberOfProcesses)
       {
           ProcessCounter++;
           fscanf(READ, "%s\n", &ProcessPath);
           
           /* Process the 'ProcessPath'
              because some of the paths
              will contain spaces. Scanf()
              will stop inputting a string
              when it reaches a Null
              character ('\0') or a 
              white space (' '). Therefore
              we need to fill that space
              with a character that is not
              legal in file names ('?') so 
              that it will not get confused
              with the file name and the
              replacement space.
           */
           
           ReplaceSpace(ProcessPath);
           printf("Executing %s\n", ProcessPath);
           Sleep(TIMER);
       };
       
       printf("\nExiting");
       for(int x = 0; x != 5; x++)
       {
           Sleep(700);
           printf(".");
       }
   }
   else //If Process.txt Does Not Exist...
   {
       printf("\t -Darkurza's ProcessStarter!- \n\n");
       printf("\n\tError! File Not Found!\n\n");
       Sleep(TIMER);
       printf("Exiting");
       for(int x = 0; x != 5; x++)
       {
           Sleep(700);
           printf(".");
       }
   }
   return 0;
}    

void ReplaceSpace(char *Input)
{
   /* This is the actual
      part of the program
      that does processing.
      Read about 'stdio.h',
      'string.h', 'stdlib.h'
      from www.cplusplus.com.
   */
   
   char *Space;
   int SpaceLocation;
   
   char Output[256];
   
   Space = strchr(Input, '?');
   /* If a '?' is 
      found,
      replace it 
      with a space.
   */
   
   strcpy(Output, Input);
   while (Space != NULL)
   {
       SpaceLocation = Space - Input;
       Output[spaceLocation] = ' ';
       Space = strchr(Space + 1, '?');
   }
   
   /* Execute Process */
   WinExec(Output, SW_SHOW);
}

dark_urza,

From first glance it seems quite a usefull application, congratulations.

 

Obviously, I have a quarrels with the code, which you can happily ignore;

 

#1: Why are you using a mix of the Windows API _and_ the Standard Library ? Usually the reason for using the 'Std Lib' is to provide a very primitive level of platform-independence. but due to your implementation of Windows-specific routines, it seems trivial.

 

#2: No error handling? Although we all tend to assume some routines will never fail, (Such as "GetSystemDirectory" which has never failed once on record), it's good practice to atleast check the return values to detect errors.

 

#3: There's a small compatibility issue, you're mixing 32-bit calls with 16-bit (Namely; "WinExec") ? If you want other users, with possibly older machines, to run your application you can use the independent "CreateProcess" routine instead of "WinExec".

It does not _usually_ cause a problem, but there's always exceptions.

 

 

 

Regards,

Matt J.

  • Author

#2: No error handling? Although we all tend to assume some routines will never fail, (Such as "GetSystemDirectory" which has never failed once on record), it's good practice to atleast check the return values to detect errors.

 

i know error checking is good practice but this isnt a huge project. whenever i used it, i never had an error with it

 

#3: There's a small compatibility issue, you're mixing 32-bit calls with 16-bit (Namely; "WinExec") ? If you want other users, with possibly older machines, to run your application you can use the independent "CreateProcess" routine instead of "WinExec".

It does not _usually_ cause a problem, but there's always exceptions.

 

i was going to use CreateProcess() instead of WinExec() but a few days before i started this i tried to learn how to use CreateProcess() and got lost at 'lpStartupInfo'

i know error checking is good practice but this isnt a huge project. whenever i used it, i never had an error with it

I figured as much, it makes sense if the application is not very large. I was just pointing it out as a future reminder.

 

 

i was going to use CreateProcess() instead of WinExec() but a few days before i started this i tried to learn how to use CreateProcess() and got lost at 'lpStartupInfo'

Well I'll clear things up for you, here's a quick example on how to use "CreateProcess":

 

#include <windows.h>
#define CALCULATOR_FILE_NAME  "C:\\WINDOWS\\System32\\Calc"


INT main()
{
STARTUPINFO         CalcStartupInfo; // Buffer for the new Calc.exe startup information.
PROCESS_INFORMATION CalcProcInfo;    // Buffer for the new Calc.exe process information.


//
// Nullify both process informational structures to ensure no conflicts.
//
ZeroMemory(&CalcStartupInfo, sizeof(STARTUPINFO));
ZeroMemory(&CalcProcInfo, sizeof(PROCESS_INFORMATION));


//
// Initialize the startup information construct.
// We just have to specify the structure size, in bytes in it's 'cb' member.
//
CalcStartupInfo.cb = sizeof(STARTUPINFO);


//
// Create a "Calc.exe"'s process.
// We'll be using the 'lpCommandLine' to specify the file-name, this will
// allow the call to work on both 16 and 32-bit systems.
//
if(CreateProcess(NULL, CALCULATOR_FILE_NAME, NULL, NULL, NULL, NULL, NULL, NULL, &CalcStartupInfo, &CalcProcInfo))
{
	//
	// Wait for the child process (Calc.exe) to complete.
	//
	WaitForSingleObject(CalcProcInfo.hProcess, INFINITE);


	//
	// Close our handle to the child process.
	//
	if(CloseHandle(CalcProcInfo.hProcess))
	{
		//
		// Close our handle to the child process's primary thread.
		//
		if(CloseHandle(CalcProcInfo.hThread))
		{
			//
			// Return TRUE, indicating an operation success.
			//
			return TRUE;
		}

		else
		{
			//
			// ERROR:
			// Failed to close thread handle.
			//
			MessageBox(NULL, "Failed to close handle to Calc.exe's primary thread.", "ERROR", MB_OK);
		}
	}

	else
	{
		//
		// ERROR:
		// Failed to close process handle.
		//
		MessageBox(NULL, "Failed to close handle to Calc.exe's process.", "ERROR", MB_OK);
	}
}

else
{
	//
	// ERROR:
	// Failed to create a new process instance from Calc.exe
	//
	MessageBox(NULL, "Failed to create a new instance of Calc.exe", "ERROR", MB_OK);
}


//
// Return FALSE, indicating an operation failure.
//
return FALSE;
}

 

As you can see, all you really need to do is pass the file-name to the 'lpCommandLine' parameter and ensure the 'lpStartupInfo' and 'lpProcessInformation' pointers are valid.

Aswell just ensure you initialize the informational constructs as I have showed, and you'll be fine.

 

 

Regards,

Matt J.

Guest
This topic is now closed to further replies.