1
0
Fork 0

LAUNCHER: register shell extension

This commit is contained in:
Mr. eXoDia 2014-08-01 04:41:46 +02:00
parent 5b6f71efba
commit d269db3a91
1 changed files with 60 additions and 51 deletions

View File

@ -4,6 +4,7 @@
enum arch enum arch
{ {
notfound,
invalid, invalid,
x32, x32,
x64 x64
@ -11,7 +12,7 @@ enum arch
static arch GetFileArchitecture(const char* szFileName) static arch GetFileArchitecture(const char* szFileName)
{ {
arch retval = invalid; arch retval = notfound;
HANDLE hFile = CreateFileA(szFileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); HANDLE hFile = CreateFileA(szFileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if(hFile != INVALID_HANDLE_VALUE) if(hFile != INVALID_HANDLE_VALUE)
{ {
@ -23,6 +24,7 @@ static arch GetFileArchitecture(const char* szFileName)
readSize = fileSize; readSize = fileSize;
if(ReadFile(hFile, data, readSize, &read, 0)) if(ReadFile(hFile, data, readSize, &read, 0))
{ {
retval = invalid;
IMAGE_DOS_HEADER* pdh = (IMAGE_DOS_HEADER*)data; IMAGE_DOS_HEADER* pdh = (IMAGE_DOS_HEADER*)data;
if(pdh->e_magic == IMAGE_DOS_SIGNATURE && (size_t)pdh->e_lfanew < readSize) if(pdh->e_magic == IMAGE_DOS_SIGNATURE && (size_t)pdh->e_lfanew < readSize)
{ {
@ -82,33 +84,51 @@ static bool BrowseFileOpen(HWND owner, const char* filter, const char* defext, c
return !!GetOpenFileNameA(&ofstruct); return !!GetOpenFileNameA(&ofstruct);
} }
#define SHELLEXT_EXE_KEY "exefile\\shell\\Debug with x64_dbg\\Command"
#define SHELLEXT_DLL_KEY "dllfile\\shell\\Debug with x64_dbg\\Command"
void RegisterShellExtension(const char* key, const char* command)
{
HKEY hKey;
if(RegCreateKeyA(HKEY_CLASSES_ROOT, key, &hKey) != ERROR_SUCCESS)
{
MessageBoxA(0, "RegCreateKeyA failed!", "Running as Admin?", MB_ICONERROR);
return;
}
if(RegSetValueExA(hKey, 0, 0, REG_EXPAND_SZ, (LPBYTE)command, strlen(command) + 1) != ERROR_SUCCESS)
MessageBoxA(0, "RegSetValueExA failed!", "Running as Admin?", MB_ICONERROR);
RegCloseKey(hKey);
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{ {
//Get INI file path //Get INI file path
char szIniFile[MAX_PATH] = ""; char szModulePath[MAX_PATH] = "";
if(!GetModuleFileNameA(0, szIniFile, MAX_PATH)) if(!GetModuleFileNameA(0, szModulePath, MAX_PATH))
{ {
MessageBoxA(0, "Error getting module path!", "Error", MB_ICONERROR|MB_SYSTEMMODAL); MessageBoxA(0, "Error getting module path!", "Error", MB_ICONERROR|MB_SYSTEMMODAL);
return 0; return 0;
} }
char szIniPath[MAX_PATH] = "";
strcpy(szIniPath, szModulePath);
char szCurrentDir[MAX_PATH] = ""; char szCurrentDir[MAX_PATH] = "";
strcpy(szCurrentDir, szIniFile); strcpy(szCurrentDir, szModulePath);
int len = (int)strlen(szCurrentDir); int len = (int)strlen(szCurrentDir);
while(szCurrentDir[len] != '\\' && len) while(szCurrentDir[len] != '\\' && len)
len--; len--;
if(len) if(len)
szCurrentDir[len] = '\0'; szCurrentDir[len] = '\0';
len = (int)strlen(szIniFile); len = (int)strlen(szIniPath);
while(szIniFile[len] != '.' && szIniFile[len] != '\\' && len) while(szIniPath[len] != '.' && szIniPath[len] != '\\' && len)
len--; len--;
if(szIniFile[len] == '\\') if(szIniPath[len] == '\\')
strcat(szIniFile, ".ini"); strcat(szIniPath, ".ini");
else else
strcpy(&szIniFile[len], ".ini"); strcpy(&szIniPath[len], ".ini");
//Load settings //Load settings
char sz32Path[MAX_PATH] = ""; char sz32Path[MAX_PATH] = "";
GetPrivateProfileStringA("Launcher", "x32_dbg", "", sz32Path, MAX_PATH, szIniFile); GetPrivateProfileStringA("Launcher", "x32_dbg", "", sz32Path, MAX_PATH, szIniPath);
char sz32Dir[MAX_PATH] = ""; char sz32Dir[MAX_PATH] = "";
strcpy(sz32Dir, sz32Path); strcpy(sz32Dir, sz32Path);
len = (int)strlen(sz32Dir); len = (int)strlen(sz32Dir);
@ -117,7 +137,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
if(len) if(len)
sz32Dir[len] = '\0'; sz32Dir[len] = '\0';
char sz64Path[MAX_PATH] = ""; char sz64Path[MAX_PATH] = "";
GetPrivateProfileStringA("Launcher", "x64_dbg", "", sz64Path, MAX_PATH, szIniFile); GetPrivateProfileStringA("Launcher", "x64_dbg", "", sz64Path, MAX_PATH, szIniPath);
char sz64Dir[MAX_PATH] = ""; char sz64Dir[MAX_PATH] = "";
strcpy(sz64Dir, sz64Path); strcpy(sz64Dir, sz64Path);
len = (int)strlen(sz64Dir); len = (int)strlen(sz64Dir);
@ -131,17 +151,20 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
char** argv=commandlineparse(&argc); char** argv=commandlineparse(&argc);
if(argc <= 1) //no arguments -> set configuration if(argc <= 1) //no arguments -> set configuration
{ {
//TODO: Shell Extension
//TODO: JIT Debugger
if(BrowseFileOpen(0, "x32_dbg.exe\0x32_dbg.exe\0\0", 0, sz32Path, MAX_PATH, szCurrentDir)) if(BrowseFileOpen(0, "x32_dbg.exe\0x32_dbg.exe\0\0", 0, sz32Path, MAX_PATH, szCurrentDir))
WritePrivateProfileStringA("Launcher", "x32_dbg", sz32Path, szIniFile); WritePrivateProfileStringA("Launcher", "x32_dbg", sz32Path, szIniPath);
if(BrowseFileOpen(0, "x64_dbg.exe\0x64_dbg.exe\0\0", 0, sz64Path, MAX_PATH, szCurrentDir)) if(BrowseFileOpen(0, "x64_dbg.exe\0x64_dbg.exe\0\0", 0, sz64Path, MAX_PATH, szCurrentDir))
WritePrivateProfileStringA("Launcher", "x64_dbg", sz64Path, szIniFile); WritePrivateProfileStringA("Launcher", "x64_dbg", sz64Path, szIniPath);
if(MessageBoxA(0, "Do you want to register a shell extension?", "Question", MB_YESNO|MB_ICONQUESTION) == IDYES)
{
char szLauncherCommand[MAX_PATH] = "";
sprintf_s(szLauncherCommand, "\"%s\" \"%%1\"", szModulePath);
RegisterShellExtension(SHELLEXT_EXE_KEY, szLauncherCommand);
RegisterShellExtension(SHELLEXT_DLL_KEY, szLauncherCommand);
}
MessageBoxA(0, "New configuration written!", "Done!", MB_ICONINFORMATION); MessageBoxA(0, "New configuration written!", "Done!", MB_ICONINFORMATION);
} }
if(argc == 2) //one argument -> execute debugger if(argc == 2) //one argument -> execute debugger
{
if(argv[1][1] == ':' && argv[1][2] == '\\') // check for "?:\"
{ {
std::string cmdLine = "\""; std::string cmdLine = "\"";
cmdLine += argv[1]; cmdLine += argv[1];
@ -149,40 +172,26 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
switch(GetFileArchitecture(argv[1])) switch(GetFileArchitecture(argv[1]))
{ {
case x32: case x32:
{
if(sz32Path[0]) if(sz32Path[0])
ShellExecuteA(0, "open", sz32Path, cmdLine.c_str(), sz32Dir, SW_SHOWNORMAL); ShellExecuteA(0, "open", sz32Path, cmdLine.c_str(), sz32Dir, SW_SHOWNORMAL);
else else
MessageBoxA(0, "Path to x32_dbg not specified in launcher configuration...", "Error!", MB_ICONERROR); MessageBoxA(0, "Path to x32_dbg not specified in launcher configuration...", "Error!", MB_ICONERROR);
}
break; break;
case x64: case x64:
{
if(sz64Path[0]) if(sz64Path[0])
ShellExecuteA(0, "open", sz64Path, cmdLine.c_str(), sz64Dir, SW_SHOWNORMAL); ShellExecuteA(0, "open", sz64Path, cmdLine.c_str(), sz64Dir, SW_SHOWNORMAL);
else else
MessageBoxA(0, "Path to x64_dbg not specified in launcher configuration...", "Error!", MB_ICONERROR); MessageBoxA(0, "Path to x64_dbg not specified in launcher configuration...", "Error!", MB_ICONERROR);
}
break; break;
case invalid: case invalid:
{
MessageBoxA(0, argv[1], "Invalid PE File!", MB_ICONERROR); MessageBoxA(0, argv[1], "Invalid PE File!", MB_ICONERROR);
}
break; break;
}
} case notfound:
} MessageBoxA(0, argv[1], "File not found or in use!", MB_ICONERROR);
else if(argc == 3) //two arguments -> JIT break;
{
if(!_stricmp(argv[1], "-a"))
{
//TODO: Handle JIT Debugger
std::string cmdLine(argv[1]);
cmdLine += " ";
cmdLine += argv[2];
MessageBoxA(0, cmdLine.c_str(), "JIT Debugger", MB_ICONINFORMATION);
} }
} }
commandlinefree(argc, argv); commandlinefree(argc, argv);