本帖最后由 zghwelcome 于 2019-12-20 17:29 编辑
我要实现的方式和这个帖子的类似,帖子中的解决方案在win7中运行失败,可能仅适用于XP系统
http://www.autoitx.com/forum.php?mod=viewthread&tid=32772
网上找到的参考代码:/* 根据进程名获取任意进程Id */
DWORD pid = GetProcessIDFromName("services.exe");//遍历进程快照获取进程ID
/* 已全部权限打开services.exe 进程 */
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);
/* 创建启动信息结构体 */
STARTUPINFOEXA si;
/* 初始化结构体 */
ZeroMemory(&si,sizeof(si));
/* 设置结构体成员 */
si.StartupInfo.cb = sizeof(si);
SIZE_T lpsize = 0;
/* 用微软规定的特定的函数初始化结构体 */
InitializeProcThreadAttributeList(NULL,1,0,&lpsize);
/* 转换指针到正确类型 */
char * temp = new char[lpsize];
LPPROC_THREAD_ATTRIBUTE_LIST AttributeList = (LPPROC_THREAD_ATTRIBUTE_LIST)temp;
/* 真正为结构体初始化属性参数 */
InitializeProcThreadAttributeList(AttributeList,1,0,&lpsize);
/* 用已构造的属性结构体更新属性表 */
if (!UpdateProcThreadAttribute(AttributeList,0,
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, &handle,sizeof(HANDLE),NULL,NULL))
{
Write_Log(LOG_TYPE_INFO,"Fail to update attributes");
return 0;
}
/* 移交指针,这里已更换了父进程的属性表是 services.exe */
si.lpAttributeList = AttributeList;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
if (CreateProcessAsUserA(NULL,0,lpExePath,0, 0, 0, EXTENDED_STARTUPINFO_PRESENT,
0, 0, (LPSTARTUPINFOA)&si, &pi))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
/* 收尾处理 */
DeleteProcThreadAttributeList(AttributeList);
delete temp;
return pi.dwProcessId;
}
/* 收尾处理 */
DeleteProcThreadAttributeList(AttributeList);
delete temp;
我已经尝试转换成au3,但是失败了,求各位大佬赐教
#include <WinAPI.au3>
#include <ProcessConstants.au3>
#AutoIt3Wrapper_UseX64 = n
;/* 根据进程名获取任意进程Id */
;DWORD pid = GetProcessIDFromName("services.exe");//遍历进程快照获取进程ID
Local $pid = ProcessExists('explorer.exe')
;/* 已全部权限打开services.exe 进程 */
;HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);
Local $handle = _WinAPI_OpenProcess($PROCESS_ALL_ACCESS, False, $pid)
ConsoleWrite('Open Process Error: ' & @error & @CRLF)
;/* 创建启动信息结构体 */
;STARTUPINFOEXA si; ;https://docs.microsoft.com/zh-cn ... base-startupinfoexa
;~ https://docs.microsoft.com/zh-cn ... adsapi-startupinfoa
#Cs
Global Const $tagSTARTUPINFO = "dword Size;ptr Reserved1;ptr Desktop;ptr Title;dword X;dword Y;dword XSize;dword YSize;dword XCountChars;" & _
"dword YCountChars;dword FillAttribute;dword Flags;word ShowWindow;word Reserved2;ptr Reserved3;handle StdInput;" & _
"handle StdOutput;handle StdError"
;// STARTUPINFOA 结构体原型
typedef struct _STARTUPINFOA {
DWORD cb;
LPSTR lpReserved;
LPSTR lpDesktop;
LPSTR lpTitle;
DWORD dwX;
DWORD dwY;
DWORD dwXSize;
DWORD dwYSize;
DWORD dwXCountChars;
DWORD dwYCountChars;
DWORD dwFillAttribute;
DWORD dwFlags;
WORD wShowWindow;
WORD cbReserved2;
LPBYTE lpReserved2;
HANDLE hStdInput;
HANDLE hStdOutput;
HANDLE hStdError;
} STARTUPINFOA, *LPSTARTUPINFOA;
;// STARTUPINFOEXA 结构体原型
typedef struct _STARTUPINFOEXA {
STARTUPINFOA StartupInfo;
LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList;
} STARTUPINFOEXA, *LPSTARTUPINFOEXA;
#Ce
Global Const $tagSTARTUPINFOEXA = $tagSTARTUPINFO & ';dword lpAttributeList' ;<<<<<<<<<<<<<<<<<< autoit结构体定义可能有问题
;/* 初始化结构体 */
;ZeroMemory(&si,sizeof(si));
Local $tStruct = DllStructCreate($tagSTARTUPINFOEXA)
If @error Then MsgBox(0, 0, '创建结构体失败')
;/* 设置结构体成员 */
;si.StartupInfo.cb = sizeof(si);
DllStructSetData($tStruct, 'Size', DllStructGetSize($tStruct))
;SIZE_T lpsize = 0;
Local $lpsize = 0
;/* 用微软规定的特定的函数初始化结构体 */
;InitializeProcThreadAttributeList(NULL,1,0,&lpsize);
$aRet = DllCall('Kernel32.dll', 'BOOL', 'InitializeProcThreadAttributeList', 'dword', 0, 'dword', 1, 'dword', 0, 'dword_ptr*', $lpsize)
MsgBox(0, 0, _WinAPI_GetLastErrorMessage()) ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< dllcall失败
$tStruct = 0
_WinAPI_CloseHandle($handle)
|