1
0
Fork 0

Merge pull request #556 from blaquee/shortcuts

add desktop shortcuts to launcher
This commit is contained in:
Duncan Ogilvie 2016-02-24 00:47:58 +01:00
commit 6c807c1fe3
1 changed files with 168 additions and 140 deletions

View File

@ -153,7 +153,7 @@ static bool BrowseFileOpen(HWND owner, const wchar_t* filter, const wchar_t* def
*/ */
#define SHELLEXT_EXE_KEY L"exefile\\shell\\Debug with x64dbg\\Command" #define SHELLEXT_EXE_KEY L"exefile\\shell\\Debug with x64dbg\\Command"
#define SHELLEXT_ICON_EXE_KEY L"exefile\\shell\\Debug with x64dbg"
/** /**
@def SHELLEXT_DLL_KEY @def SHELLEXT_DLL_KEY
@ -161,46 +161,60 @@ static bool BrowseFileOpen(HWND owner, const wchar_t* filter, const wchar_t* def
*/ */
#define SHELLEXT_DLL_KEY L"dllfile\\shell\\Debug with x64dbg\\Command" #define SHELLEXT_DLL_KEY L"dllfile\\shell\\Debug with x64dbg\\Command"
#define SHELLEXT_ICON_DLL_KEY L"dllfile\\shell\\Debug with x64dbg"
/**
@fn static void RegisterShellExtension(const wchar_t* key, const wchar_t* command)
@brief Registers the shell extension. static wchar_t* GetDesktopPath()
{
static wchar_t path[MAX_PATH + 1];
if (SHGetSpecialFolderPath(HWND_DESKTOP, path, CSIDL_DESKTOPDIRECTORY, FALSE))
return path;
else
return NULL;
}
@param key The key. static HRESULT AddDesktopShortcut(wchar_t* szPathOfFile, const wchar_t* szNameOfLink)
@param command The command. {
*/ HRESULT hRes = NULL;
IShellLink* psl;
static BOOL RegisterShellExtension(const wchar_t* key, const wchar_t* command) //Get the working directory
wchar_t pathFile[MAX_PATH + 1];
wcscpy(pathFile, szPathOfFile);
PathRemoveFileSpec(pathFile);
hRes = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hRes))
{
IPersistFile* ppf;
psl->SetPath(szPathOfFile);
psl->SetDescription(L"A Debugger for the future!");
psl->SetIconLocation(szPathOfFile, 0);
psl->SetWorkingDirectory(pathFile);
hRes = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hRes))
{
wchar_t path[MAX_PATH + 1];
_wmakepath_s(path, _MAX_PATH, NULL, GetDesktopPath(), szNameOfLink, L"lnk");
hRes = ppf->Save(path, TRUE);
ppf->Release();
}
psl->Release();
}
return hRes;
}
static void RegisterShellExtension(const wchar_t* key, const wchar_t* command)
{ {
HKEY hKey; HKEY hKey;
BOOL result = TRUE;
if (RegCreateKeyW(HKEY_CLASSES_ROOT, key, &hKey) != ERROR_SUCCESS) if (RegCreateKeyW(HKEY_CLASSES_ROOT, key, &hKey) != ERROR_SUCCESS)
{ {
MessageBoxW(0, L"RegCreateKeyA failed!", L"Running as Admin?", MB_ICONERROR); MessageBoxW(0, L"RegCreateKeyA failed!", L"Running as Admin?", MB_ICONERROR);
return !result; return;
} }
if (RegSetValueExW(hKey, 0, 0, REG_EXPAND_SZ, (LPBYTE)command, (wcslen(command) + 1) * sizeof(wchar_t)) != ERROR_SUCCESS) if (RegSetValueExW(hKey, 0, 0, REG_EXPAND_SZ, (LPBYTE)command, (wcslen(command) + 1) * sizeof(wchar_t)) != ERROR_SUCCESS)
{
MessageBoxW(0, L"RegSetValueExA failed!", L"Running as Admin?", MB_ICONERROR); MessageBoxW(0, L"RegSetValueExA failed!", L"Running as Admin?", MB_ICONERROR);
result = !result;
}
RegCloseKey(hKey); RegCloseKey(hKey);
return result;
}
static void AddIcon(const wchar_t* key, const wchar_t* command)
{
HKEY pKey;
if(RegOpenKeyExW(HKEY_CLASSES_ROOT, key, 0, KEY_ALL_ACCESS, &pKey) != ERROR_SUCCESS)
MessageBoxW(0, L"RegOpenKeyExW Failed!", L"Running as Admin?", MB_ICONERROR);
if(RegSetValueExW(pKey, L"Icon", 0, REG_SZ, (LPBYTE)command, (wcslen(command) + 1) * sizeof(wchar_t)) != ERROR_SUCCESS)
MessageBoxW(0, L"RegSetValueExA failed!", L"Running as Admin?", MB_ICONERROR);
RegCloseKey(pKey);
return;
} }
static void CreateUnicodeFile(const wchar_t* file) static void CreateUnicodeFile(const wchar_t* file)
@ -219,19 +233,6 @@ static void CreateUnicodeFile(const wchar_t* file)
CloseHandle(hFile); CloseHandle(hFile);
} }
/**
@fn int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
@brief Window main.
@param hInstance The instance.
@param hPrevInstance The previous instance.
@param lpCmdLine The command line.
@param nShowCmd The show command.
@return An APIENTRY.
*/
//Taken from: http://www.cplusplus.com/forum/windows/64088/ //Taken from: http://www.cplusplus.com/forum/windows/64088/
static bool ResolveShortcut(HWND hwnd, const wchar_t* szShortcutPath, char* szResolvedPath, size_t nSize) static bool ResolveShortcut(HWND hwnd, const wchar_t* szShortcutPath, char* szResolvedPath, size_t nSize)
{ {
@ -262,12 +263,38 @@ static bool ResolveShortcut(HWND hwnd, const wchar_t* szShortcutPath, char* szRe
if (SUCCEEDED(hres)) if (SUCCEEDED(hres))
{ {
//Get the path to the link target. //Get the path to the link target.
char szGotPath[MAX_PATH] = {0}; TCHAR szGotPath[MAX_PATH] = { 0 };
hres = psl->GetPath(szGotPath, _countof(szGotPath), NULL, SLGP_SHORTPATH); hres = psl->GetPath(szGotPath, _countof(szGotPath), NULL, SLGP_SHORTPATH);
if (SUCCEEDED(hres)) if (SUCCEEDED(hres))
{ {
strcpy_s(szResolvedPath, nSize, szGotPath); //or we could just use wide characters everywhere :)
int required_len = WideCharToMultiByte(CP_UTF8, 0, szGotPath, MAX_PATH, NULL, 0, NULL, NULL);
if (required_len <= 0)
{
MessageBox(NULL, L"Fail string convert", L"Error", MB_OK);
return false;
}
char* local_buffer = (char*)LocalAlloc(LMEM_FIXED, required_len + 1);
if (!local_buffer)
{
MessageBox(NULL, L"LocalAlloc failed", L"Error", MB_OK);
return false;
}
required_len = WideCharToMultiByte(CP_UTF8, 0, szGotPath, MAX_PATH, local_buffer, required_len, NULL, NULL);
if (required_len <= 0)
{
LocalFree(local_buffer);
MessageBox(NULL, L"Error in WideChartoMultiByte!", L"Error", MB_OK);
return false;
}
local_buffer[required_len] = '\0';
strcpy_s(szResolvedPath, nSize, local_buffer);
LocalFree(local_buffer);
} }
} }
} }
@ -370,13 +397,14 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
if (MessageBoxW(0, L"Do you want to register a shell extension?", L"Question", MB_YESNO | MB_ICONQUESTION) == IDYES) if (MessageBoxW(0, L"Do you want to register a shell extension?", L"Question", MB_YESNO | MB_ICONQUESTION) == IDYES)
{ {
wchar_t szLauncherCommand[MAX_PATH] = L""; wchar_t szLauncherCommand[MAX_PATH] = L"";
wchar_t szIconCommand[MAX_PATH] = L"";
swprintf_s(szLauncherCommand, _countof(szLauncherCommand), L"\"%s\" \"%%1\"", szModulePath); swprintf_s(szLauncherCommand, _countof(szLauncherCommand), L"\"%s\" \"%%1\"", szModulePath);
swprintf_s(szIconCommand, _countof(szIconCommand), L"\"%s\",0", szModulePath); RegisterShellExtension(SHELLEXT_EXE_KEY, szLauncherCommand);
if(RegisterShellExtension(SHELLEXT_EXE_KEY, szLauncherCommand)) RegisterShellExtension(SHELLEXT_DLL_KEY, szLauncherCommand);
AddIcon(SHELLEXT_ICON_EXE_KEY, szIconCommand); }
if(RegisterShellExtension(SHELLEXT_DLL_KEY, szLauncherCommand)) if (MessageBoxW(0, L"Do you want to create Desktop Shortcuts?", L"Question", MB_YESNO | MB_ICONQUESTION) == IDYES)
AddIcon(SHELLEXT_ICON_DLL_KEY, szIconCommand); {
AddDesktopShortcut(sz32Path, L"x32dbg");
AddDesktopShortcut(sz64Path, L"x64dbg");
} }
if (bDoneSomething) if (bDoneSomething)
MessageBoxW(0, L"New configuration written!", L"Done!", MB_ICONINFORMATION); MessageBoxW(0, L"New configuration written!", L"Done!", MB_ICONINFORMATION);