My self install codes using startup folder - please comment


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: My self install codes using startup folder - please comment

  1. #1
    Join Date
    May 2004
    Posts
    70

    My self install codes using startup folder - please comment

    guys here are my self install codes for any applications
    it will copy itself to the startup folder and run
    it's not very efficient so i need u guys to advise on improvements
    works on win98,winxp
    codes that have been commented out are some of the stuff i previously tested and found to be unsuccessful
    :)

    Code:
    char *current_directory = new char[MAX_PATH]; // rem to delete this
    	char *window_dir = new char[MAX_PATH]; // rem to delete this
    	GetModuleFileName(theApp.m_hInstance, current_directory, MAX_PATH);
    
    
    
    
    
    	SHGetSpecialFolderPath(NULL,window_dir, CSIDL_STARTUP,TRUE);
    	strcat(window_dir,"\\sev.exe");
    
    
    // FORCE IT TO BE IN STARTUP FOLDER	
    //	CopyFile(current_directory,window_dir,FALSE ) ;
    
    
    	if( stricmp (current_directory , window_dir) != 0)   // i am not in the correct dir
    	{
    		int set_file_result = SetFileAttributes(window_dir,FILE_ATTRIBUTE_NORMAL);
    		if (set_file_result == 0)// if i cant set attrib, means wind_dir is empty, if i can set, means wind_dir not empty and i gotta del
    		{
    			CopyFile(current_directory,window_dir,FALSE ) ;
    			ShellExecute(0,0,window_dir,0,0,0);
    			delete[] window_dir;
    			return 55;
    			return 0;
    		}
    
    		BOOL delete_result = DeleteFile( window_dir);
    		int bb = GetLastError();
    		if(delete_result == 0) // delete fails because of access denied
    		{
    
    
    
    
    
    
    //BOOL seek_and_kill(char *window_dir ) , to kill the currently process so as to allow me to run
    
    			{
    				HANDLE hProcessSnap;
    				PROCESSENTRY32 pe32;
    
      // Take a snapshot of all processes in the system.
    				hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
    				if( hProcessSnap == INVALID_HANDLE_VALUE )
    				{
    					return( FALSE );
    				}
    				pe32.dwSize = sizeof( PROCESSENTRY32 );
    
      // Retrieve information about the first process,
      // and exit if unsuccessful
    			
    				if( !Process32First( hProcessSnap, &pe32 ) )
    				{
    					CloseHandle( hProcessSnap );     // Must clean up the snapshot object!
    					return( FALSE );
    				}
    				BOOL next_result = 1;
     // TCHAR *szExeFile = {pe32.szExeFile};
      
    				while(    ( stricmp( pe32.szExeFile , "sev.exe") != 0) &&  (next_result != FALSE )    )
    				{
    					  next_result = Process32Next(hProcessSnap, &pe32);
    				}
      
    				HANDLE kill_this_process = OpenProcess( PROCESS_ALL_ACCESS,0, pe32.th32ProcessID);
    
    				TerminateProcess(kill_this_process , 999);
    				// return 1;
    
    			}
    
    
    
    
    			while(!DeleteFile( window_dir) )
    			{};
    		}
    		CopyFile(current_directory,window_dir,FALSE ) ;
    		ShellExecute(0,0,window_dir,0,0,0);
    		delete[] window_dir;
    		return 55;
    
    	}
    	else // else if i am in the correct dir, i dont want 2 instance of the same thing
    	{
    		HANDLE hProcessSnap;
    		PROCESSENTRY32 pe32;
    
      // Take a snapshot of all processes in the system.
    		hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
    		if( hProcessSnap == INVALID_HANDLE_VALUE )
    		{
    			return( FALSE );
    		}
    		pe32.dwSize = sizeof( PROCESSENTRY32 );
      // Retrieve information about the first process,
      // and exit if unsuccessful
    			
    		if( !Process32First( hProcessSnap, &pe32 ) )
    		{
    			CloseHandle( hProcessSnap );     // Must clean up the snapshot object!
    			return( FALSE );
    		}
    		BOOL next_result = 1;
    		BOOL duplicate = FALSE;
     // TCHAR *szExeFile = {pe32.szExeFile};
      
    		while(   next_result != FALSE )
    		{
    			if(  (stricmp( pe32.szExeFile , "sev.exe") == 0)   &&(duplicate == TRUE))
    			{
    				return 55;
    			}
    			if( ( stricmp( pe32.szExeFile , "sev.exe") == 0)    &&(duplicate == FALSE))
    			{
    				duplicate = TRUE;
    			}
    			next_result = Process32Next(hProcessSnap, &pe32);
    		}
    					
    			
    			if(  (stricmp( pe32.szExeFile , "sev.exe") == 0)   &&(duplicate == TRUE))
    			{
    			//	return 55;
    			}
    }	
    	delete[] window_dir;
    	delete[] current_directory;

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    First off' get rid of the dynamic memory allocations of characters. You know the max size of the directory at compile time so why not use

    char dirname[MAX_DIR]; ?

    As for the rest, you want to use constants instead of magic numbers such as 55. That doesn't mean that this code works correctly but at least it will have fewer bugs and less lines of code.
    Danny Kalev

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    I think most programs these days avoid the crazy logic by simply setting startup in the registry -- I dont use the registry and cannot show you how - but this is what you should do.

  4. #4
    Join Date
    May 2004
    Posts
    70
    - copy itself to the startup folder, execute the copy and return
    - if startup folder already has a copy of itself, delete the one in startup folder
    - if unable to delete, it's assumed that it is running because it is running and gets write protected
    - search for it's process and kill it so that it can be deleted
    - but we dont want 2 instance of it, hence if it is already running and the startup folder, return the latter instance

  5. #5
    Join Date
    May 2004
    Posts
    70
    Quote Originally Posted by Danny
    First off' get rid of the dynamic memory allocations of characters. You know the max size of the directory at compile time so why not use

    char dirname[MAX_DIR]; ?

    As for the rest, you want to use constants instead of magic numbers such as 55. That doesn't mean that this code works correctly but at least it will have fewer bugs and less lines of code.
    what is "dynamic memory allocations of characters."
    Code:
    char *current_directory = new char[MAX_PATH]; // rem to delete this
    char *window_dir = new char[MAX_PATH]; // rem to delete this
    i dont believe the above is dynamic ?
    MAX_PATH is a constant

  6. #6
    Join Date
    May 2004
    Posts
    70
    Quote Originally Posted by jonnin
    I think most programs these days avoid the crazy logic by simply setting startup in the registry -- I dont use the registry and cannot show you how - but this is what you should do.
    i dont like to mess with the registry
    without an installer, it makes the program difficult to uninstall for the user
    i prefer to use startup folder for my simple single exe file program

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    I do too. But it does simplify things. Besides, if you leave garbage in the registry regclean (free from microsoft) will fix it, and all users know to run this from time to time, (or should know that). (I actually use regscrubxp now).

  8. #8
    Join Date
    May 2004
    Posts
    70
    Quote Originally Posted by jonnin
    I do too. But it does simplify things. Besides, if you leave garbage in the registry regclean (free from microsoft) will fix it, and all users know to run this from time to time, (or should know that). (I actually use regscrubxp now).
    great tip!
    do these registry cleaners cause problems
    they dont seem to be confident with their task judging from the "we are not responsible for you not backing up your registry" disclaimer

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    They make a .reg file that you can double click to undo the changes. I have never had a problem, but someone out there probably has. The reg is a nice file -- each program has a location, and entries. The tools check that the program actually exists where the registry says it does, and if not, kills the tree for the missing program. I don't know what else they do but in many years of use I havent had any trouble. I started using them back when uninstallers left crap on the disk (some still do), so I deleted the folders manually and then had to clean the reg (you dont have to, but sometimes there are ghosts if you dont).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center