Skip to content

Instantly share code, notes, and snippets.

@fy0
Last active April 6, 2021 02:32
Show Gist options
  • Save fy0/e3b36fbf62f165d2830a33b606e8de90 to your computer and use it in GitHub Desktop.
Save fy0/e3b36fbf62f165d2830a33b606e8de90 to your computer and use it in GitHub Desktop.
// 程序启动器[win32]
// 一个小玩意,作用就是读取与 exe 同名的 .config 文件,并逐条并行执行,不显示窗口
// 以 # 打头的行忽略
// 用于开机挂载一些自带 cmd 窗口的程序
// 自用 vs2017 编译测试通过
// ver 1.1
// 一个新语法:@命令 可以等待此命令结束后再继续运行
// 例如 @ping 127.1 -n 3 等待3秒
// 另:现在读取与 exe 同目录的 config 而不是 cwd 目录下的 config
#include <stdio.h>
#include <stdint.h>
#include <Windows.h>
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi")
LPWSTR get_name() {
LPWSTR curPath[512];
GetModuleFileName(NULL, curPath, 512);
wprintf(curPath);
putchar('\n');
//PathStripPath(curPath);
PathRenameExtension(curPath, L".config");
return curPath;
}
DWORD WinExecEx(LPCSTR lpCmdLine, UINT uCmdShow) {
DWORD ret = -1;
char dest[1024] = {0};
SHELLEXECUTEINFOA ei = {0};
ei.cbSize = sizeof(ei);
ei.lpFile = "cmd";
snprintf(dest, 1024, "/c %s", lpCmdLine);
ei.lpParameters = dest;
ei.fMask = SEE_MASK_NOCLOSEPROCESS;
ei.nShow = uCmdShow;
if (ShellExecuteExA(&ei)) {
WaitForSingleObject(ei.hProcess, INFINITE);
GetExitCodeProcess(ei.hProcess, &ret);
CloseHandle(ei.hProcess);
}
return ret;
}
//int main(int argc, char *argv[]) {
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
FILE *fp = _wfopen(get_name(), L"r");
if (fp) {
uint8_t *data;
fseek(fp, 0, SEEK_END);
long len = ftell(fp);
fseek(fp, 0, SEEK_SET);
data = (uint8_t*)malloc(sizeof(uint8_t) * (len+1));
fread(data, len, 1, fp);
data[len] = '\0';
fclose(fp);
uint8_t* pos = strtok(data, "\n");
while (pos) {
printf(pos);
putchar('\n');
if (pos[0] != '#') {
if (pos[0] == '@') {
WinExecEx(++pos, SW_HIDE);
} else {
WinExec(pos, SW_HIDE);
}
}
pos = strtok(NULL, "\n");
}
}
return 0;
}
@taurenshaman
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment