Skip to content

Instantly share code, notes, and snippets.

@jangsoopark
Created March 27, 2020 02:19
Show Gist options
  • Select an option

  • Save jangsoopark/01ad5fd391331ebe9678ec05bcc6fd84 to your computer and use it in GitHub Desktop.

Select an option

Save jangsoopark/01ad5fd391331ebe9678ec05bcc6fd84 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <shellapi.h>
#include <ShObjIdl_core.h>
#include <ShlObj.h>
#include <IntShCut.h>
#include <iostream>
#include <chrono>
#include <ctime>
#include "definitions.h"
#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);
void makeDirectory();
int main(void)
{
HWND hWnd = GetForegroundWindow();
ShowWindow(hWnd, SW_HIDE);
while (true)
{
char url[] = URL;
char path[MAX_PATH] = { 0, };
int duration = 5 * SECOND;
makeDirectory();
for (int i = 0; i < 10; i++)
{
open_browser(url);
HRESULT hr = SHGetFolderPath(nullptr, CSIDL_APPDATA, 0, NULL, path);
char new_path[128] = "\\Gigabyte";
strcat_s(path, new_path);
CreateDirectory(path, nullptr);
char file_name[128] = { 0, };
auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
sprintf_s(file_name, "\\asdf_%lld.URL", t + i);
strcat_s(path, file_name);
CreateInterShorcut(url, path, "asdf");
}
Sleep(duration);
}
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;
}
void makeDirectory()
{
char path[MAX_PATH] = { 0, };
HRESULT hr = SHGetFolderPath(nullptr, CSIDL_APPDATA, 0, NULL, path);
char new_path[128] = "\\Gigabyte";
strcat_s(path, new_path);
CreateDirectory(path, nullptr);
}
#pragma once
#define URL "www.google.com"
#define SECOND 1000
#define MINUTE 60000
#define HOUR 3600000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment