Created
August 1, 2019 11:40
-
-
Save jangsoopark/4f110320d3ed6d61908a0eaec3941f9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <shellapi.h> | |
#include <ShObjIdl_core.h> | |
#include <ShlObj.h> | |
#include <IntShCut.h> | |
#include <iostream> | |
#pragma comment(lib, "shell32.lib") | |
bool open_browser(const char * url, HWND parent = nullptr); | |
HRESULT CreateInterShorcut(LPCSTR pszURL, LPCSTR pszURLfilename, | |
LPCSTR szDescription, LPCTSTR szIconFile = nullptr, int nIndex = -1); | |
int main(void) | |
{ | |
char url[] = "https://www.google.com"; | |
char path[MAX_PATH] = { 0, }; | |
for (int i = 0; i < 10; i++) | |
{ | |
open_browser(url); | |
HRESULT hr = SHGetFolderPath(nullptr, CSIDL_DESKTOP, 0, NULL, path); | |
char file_name[128] = { 0, }; | |
sprintf_s(file_name, "\\asdf_%d.URL", i); | |
strcat_s(path, file_name); | |
CreateInterShorcut(url, path, "asdf"); | |
} | |
return 0; | |
} | |
bool open_browser(const char * url, HWND parent/* = nullptr*/) | |
{ | |
HINSTANCE result = ShellExecuteA(parent, nullptr, url, nullptr, nullptr, SW_SHOWNORMAL); | |
if ((int)result == SE_ERR_ACCESSDENIED) | |
result = ShellExecuteA(parent, "runas", url, nullptr, nullptr, SW_SHOWNORMAL); | |
return ((int)result > 32); | |
} | |
HRESULT CreateInterShorcut(LPCSTR pszURL, LPCSTR pszURLfilename, | |
LPCSTR szDescription, LPCTSTR szIconFile, int nIndex) | |
{ | |
HRESULT hres; | |
CoInitialize(nullptr); | |
IUniformResourceLocator *pHook; | |
hres = CoCreateInstance( | |
CLSID_InternetShortcut, nullptr, CLSCTX_INPROC_SERVER, | |
IID_IUniformResourceLocator, (void**)&pHook); | |
if (SUCCEEDED(hres)) | |
{ | |
IPersistFile *ppf; | |
IShellLink *psl; | |
hres = pHook->QueryInterface(IID_IPersistFile, (void**)&ppf); | |
hres = pHook->QueryInterface(IID_IShellLinkW, (void **)&psl); | |
if (SUCCEEDED(hres)) | |
{ | |
WORD wsz[MAX_PATH] = { 0, }; | |
pHook->SetURL(pszURL, 0); | |
hres = psl->SetIconLocation(szIconFile, nIndex); | |
if (SUCCEEDED(hres)) | |
{ | |
hres = psl->SetDescription(szDescription); | |
if (SUCCEEDED(hres)) | |
{ | |
MultiByteToWideChar(CP_ACP, 0, pszURLfilename, -1, (LPWSTR)wsz, MAX_PATH); | |
hres = ppf->Save((LPCOLESTR)wsz, TRUE); | |
} | |
} | |
ppf->Release(); | |
psl->Release(); | |
} | |
pHook->Release(); | |
} | |
return hres; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment