Created
June 28, 2015 08:27
-
-
Save informationsea/d161398ff34888bbb1c2 to your computer and use it in GitHub Desktop.
Quit All Portable Apps before launch UnplugDrive http://homepage3.nifty.com/yamakox/UnplugDrivePortable/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <windows.h> | |
#include <tlhelp32.h> | |
#include <tchar.h> | |
#include <stdbool.h> | |
#include <process.h> | |
#include <errno.h> | |
#ifdef _DEBUG | |
void printError( TCHAR* msg ); | |
#else | |
#define printError(...) | |
#endif | |
int main(int argc,char **argv) | |
{ | |
HANDLE hProcessSnap; | |
HANDLE hProcess; | |
PROCESSENTRY32 pe32; | |
printf("Quit Laucher version 0.1\n" | |
"Okamura Yasunobu All Rights Reserved.\n\n"); | |
TCHAR removablePath[128]; | |
TCHAR systemPath[128]; | |
GetEnvironmentVariable(_T("SYSTEMROOT"),systemPath,sizeof(systemPath)); | |
GetModuleFileName(NULL,removablePath,sizeof(removablePath)); | |
TCHAR removableDriveLetter = _totupper(removablePath[0]); | |
if(removableDriveLetter == _totupper(systemPath[0])){ | |
MessageBox(NULL,_T("This drive is system drive."),_T("Quit and Lauch"),MB_OK|MB_ICONINFORMATION); | |
return -1; | |
} | |
_tprintf(_T("\nExepath : %s\nSystem path : %s\nDrive Letter %c\n"),removablePath,systemPath,removableDriveLetter); | |
// Take a snapshot of all processes in the system. | |
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); | |
if( hProcessSnap == INVALID_HANDLE_VALUE ) { | |
printError( TEXT("CreateToolhelp32Snapshot (of processes)") ); | |
return( FALSE ); | |
} | |
// Set the size of the structure before using it. | |
pe32.dwSize = sizeof( PROCESSENTRY32 ); | |
// Retrieve information about the first process, | |
// and exit if unsuccessful | |
if( !Process32First( hProcessSnap, &pe32 ) ) { | |
printError( TEXT("Process32First") ); // show cause of failure | |
CloseHandle( hProcessSnap ); // clean the snapshot object | |
return( FALSE ); | |
} | |
// Now walk the snapshot of processes, and | |
// display information about each process in turn | |
do | |
{ | |
HANDLE hModuleSnap = INVALID_HANDLE_VALUE; | |
MODULEENTRY32 me32; | |
// Take a snapshot of all modules in the specified process. | |
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pe32.th32ProcessID ); | |
if( hModuleSnap == INVALID_HANDLE_VALUE ) | |
{ | |
printError( TEXT("CreateToolhelp32Snapshot (of modules)") ); | |
continue; | |
} | |
// Set the size of the structure before using it. | |
me32.dwSize = sizeof( MODULEENTRY32 ); | |
// Retrieve information about the first module, | |
// and exit if unsuccessful | |
if( !Module32First( hModuleSnap, &me32 ) ) | |
{ | |
printError( TEXT("Module32First") ); // show cause of failure | |
CloseHandle( hModuleSnap ); // clean the snapshot object | |
continue; | |
//return( FALSE ); | |
} | |
if(_totupper(me32.szExePath[0]) == removableDriveLetter){ | |
_tprintf( TEXT("\n\n MODULE NAME: %s"), me32.szModule ); | |
_tprintf( TEXT("\n Executable = %s"), me32.szExePath ); | |
printf( "\n Process ID = 0x%08X", (unsigned int)me32.th32ProcessID ); | |
bool shouldQuit = false; | |
if(_tcscmp(me32.szExePath,removablePath) == 0){}else | |
if(_tcscmp(_T("PortableAppsPlatform.exe"),me32.szModule) == 0){ | |
shouldQuit = true; | |
}else{ | |
TCHAR message[256]; | |
_sntprintf(message,sizeof(message)-1,_T("Are you sure to quit %s?"),me32.szModule); | |
message[sizeof(message)-1] = 0; | |
if(MessageBox(NULL,message,_T("Quit Information"),MB_OKCANCEL|MB_ICONQUESTION) == IDOK){ | |
shouldQuit = true; | |
} | |
} | |
if(shouldQuit){ | |
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID ); | |
if( hProcess == NULL ) { | |
printError( TEXT("OpenProcess") ); | |
} else { | |
TerminateProcess(hProcess,-1); | |
CloseHandle(hProcess); | |
} | |
} | |
} | |
CloseHandle( hModuleSnap ); | |
} while( Process32Next( hProcessSnap, &pe32 ) ); | |
CloseHandle( hProcessSnap ); | |
{ | |
int length = _tcslen(removablePath); | |
int i; | |
for(i = length-1;i >= 0;i--){ | |
if(removablePath[i] == _T('\\')){ | |
removablePath[i+1] = 0; | |
break; | |
} | |
} | |
//if(argc == 2){ | |
//}else{ | |
_tcsncat(removablePath,_T("UnplugDrive.exe"),sizeof(removablePath)-1); | |
//} | |
_tprintf(_T("Unplug : %s\n\n"),removablePath); | |
execl(removablePath,NULL); | |
printf("Unable to launch.\n\n"); | |
TCHAR *message; | |
switch(errno){ | |
case ENOENT: | |
message = _T("UnplugDrive.exe is not found."); | |
break; | |
case EINVAL: | |
message = _T("Arguments is not vaild."); | |
break; | |
default: | |
message = _T("Unknown error."); | |
break; | |
} | |
MessageBox(NULL,message,_T("Quit and Lauch"),MB_OK|MB_ICONINFORMATION); | |
} | |
return 0; | |
} | |
#ifdef _DEBUG | |
void printError( TCHAR* msg ) | |
{ | |
DWORD eNum; | |
TCHAR sysMsg[256]; | |
TCHAR* p; | |
eNum = GetLastError( ); | |
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | |
NULL, eNum, | |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language | |
sysMsg, 256, NULL ); | |
// Trim the end of the line and terminate it with a null | |
p = sysMsg; | |
while( ( *p > 31 ) || ( *p == 9 ) ) | |
++p; | |
do { *p-- = 0; } while( ( p >= sysMsg ) && | |
( ( *p == '.' ) || ( *p < 33 ) ) ); | |
// Display the message | |
_tprintf( TEXT("\n WARNING: %s failed with error %d (%s)"), msg, eNum, sysMsg ); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment