shortcutter/Shortcutter/Shortcutter.cpp

87 lines
2.5 KiB
C++

#include <windows.h>
#include <shobjidl.h>
#include <shlguid.h>
#include <objbase.h>
#include <iostream>
#pragma comment(lib, "ole32.lib")
HRESULT CreateShortcut(LPCWSTR folderPath, LPCWSTR shortcutPath, LPCWSTR description)
{
HRESULT res;
IShellLinkW* shell = nullptr;
IPersistFile* file = nullptr;
res = CoInitialize(NULL);
if (FAILED(res)) {
std::wcout << L"Failed to initialize COM. HRESULT: 0x" << std::hex << res << std::endl;
return res;
}
res = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (LPVOID*)&shell);
if (FAILED(res)) {
std::wcout << L"Failed to create IShellLink instance. HRESULT: 0x" << std::hex << res << std::endl;
CoUninitialize();
return res;
}
res = shell->SetPath(folderPath);
if (FAILED(res)) {
std::wcout << L"Failed to set path to folder. HRESULT: 0x" << std::hex << res << std::endl;
shell->Release();
CoUninitialize();
return res;
}
res = shell->SetDescription(description);
if (FAILED(res)) {
std::wcout << L"Failed to set description. HRESULT: 0x" << std::hex << res << std::endl;
shell->Release();
CoUninitialize();
return res;
}
res = shell->QueryInterface(IID_IPersistFile, (LPVOID*)&file);
if (FAILED(res)) {
std::wcout << L"Failed to query IPersistFile interface. HRESULT: 0x" << std::hex << res << std::endl;
shell->Release();
CoUninitialize();
return res;
}
res = file->Save(shortcutPath, TRUE);
if (FAILED(res)) {
std::wcout << L"Failed to save shortcut file. HRESULT: 0x" << std::hex << res << std::endl;
}
else {
std::wcout << L"Shortcut successfully created at " << shortcutPath << std::endl;
}
file->Release();
shell->Release();
CoUninitialize();
return res;
}
int wmain(int argc, wchar_t* argv[]) {
SetDllDirectory((LPWSTR)L"\\\\FS\\SyteScans\\Attachments\\Shortcutter");
if (argc != 3)
{
std::wcout << L"Usage: " << argv[0] << L" <FolderPath> <ShortcutPath>" << std::endl;
return 1;
}
LPCWSTR folderPath = argv[1];
LPCWSTR shortcutPath = argv[2];
LPCWSTR description = L"Folder Shortcut";
HRESULT result = CreateShortcut(folderPath, shortcutPath, description);
if (FAILED(result))
{
std::wcout << L"Failed to create folder shortcut. HRESULT: 0x" << std::hex << result << std::endl;
}
return 0;
}