Skip to content

Instantly share code, notes, and snippets.

@shotadft
Last active January 8, 2025 08:01
Show Gist options
  • Select an option

  • Save shotadft/559e2c3344f26ee3f65db2f2b8d1ae84 to your computer and use it in GitHub Desktop.

Select an option

Save shotadft/559e2c3344f26ee3f65db2f2b8d1ae84 to your computer and use it in GitHub Desktop.
デスクトップにショートカットを作るだけのプログラムです。 (要C++ 17以上)(要最適化)
/**
* @file createDesktopSC.h
* @brief The additional stringEx type extends the string type and adds various functions.
* @author Shotadft
* @license MIT License, Copyright 2024 Shotadft.
* @date 2024:08:03::20:35
*/
#ifndef CREATE_DESKTOPSC_H
#define CREATE_DESKTOPSC_H
#include <windows.h>
#include <shlobj.h>
#include <shlobj_core.h>
#include <string>
#include <tchar.h>
/**
* @brief Create a shortcut on the desktop.
* @param (scFilename) Shortcut FileName
*/
void CreateDesktopSC(std::wstring scFilename) {
HRESULT hre = CoInitialize(NULL);
wchar_t szBuff[_MAX_PATH];
if (GetModuleFileName(NULL, szBuff, _MAX_PATH) == 0) {
CoUninitialize();
return;
}
std::wstring filename = szBuff;
wchar_t desktopPath[MAX_PATH];
if (FAILED(SHGetFolderPath(NULL, CSIDL_DESKTOPDIRECTORY, NULL, 0, desktopPath))) {
CoUninitialize();
return;
}
std::wstring shortcutPath = std::wstring(desktopPath) + L"\\" + scFilename + L".lnk";
IShellLink* pShellLink;
if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&pShellLink))) {
pShellLink->SetPath(filename.c_str());
IPersistFile* pPersistFile;
if (SUCCEEDED(pShellLink->QueryInterface(IID_IPersistFile, (LPVOID*)&pPersistFile))) {
if (SUCCEEDED(pPersistFile->Save(shortcutPath.c_str(), TRUE))) {
pPersistFile->Release();
}
pShellLink->Release();
}
else {
pShellLink->Release();
}
}
CoUninitialize();
}
#endif // !CREATE_DESKTOPSC_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment