run.bat

1
start "" %1 

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "windows.h"
#include <WtsApi32.h>
#include <UserEnv.h>
#include <tlhelp32.h>
#include "base/charset.h"
#include <tchar.h>
#include <winbase.h>
#pragma comment(lib, "WtsApi32.lib")
#pragma comment(lib, "UserEnv.lib")

BOOL RunExec(const TCHAR* path)
{
HANDLE hToken = 0;
HANDLE hProcessSnap = 0;
PROCESSENTRY32 pe32 = { 0 };
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pe32.dwSize = sizeof(PROCESSENTRY32);
wchar_t name[] = L"explorer.exe";
for (Process32First(hProcessSnap, &pe32); Process32Next(hProcessSnap, &pe32);)
{
if (_wcsicmp((pe32.szExeFile), (name)))
continue;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pe32.th32ProcessID);
auto res = OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken);
CloseHandle(hProcessSnap);
}
if (hToken == 0) return 0;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
wchar_t aa[] = L"winsta0\\default";
si.lpDesktop = aa;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;

#ifdef UNICODE
int l = wcslen(path);
#else
int l = strlen(path);
#endif // UNICODE
TCHAR* cmd = new TCHAR[l+1];
cmd[l] = 0;
memcpy(cmd, path, l*sizeof(TCHAR));
BOOL res{ FALSE };
DWORD err{ 0 };
res = CreateProcessAsUser(hToken, L"run.bat", cmd, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, &si, &pi);
if (!res)
{
LOG_ERROR("run error! LastError=%ld", GetLastError());
}
delete[] cmd;
return res;
}