我没有设法CreateProcess()
从安装程序成功调用。
虽然以下 C 代码(在 Win7 上使用 MinGW 编译)可以notepad.exe
毫无问题地启动:
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[]){
BOOL result;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
memset(&startupInfo,0, sizeof(STARTUPINFO));
startupInfo.cb = sizeof(startupInfo);
memset(&processInformation, 0, sizeof(PROCESS_INFORMATION));
//these 2 values are used for NSIS
printf("sizeof(startupInfo) = %d\n",sizeof(startupInfo));
printf("sizeof(processInformation) = %d\n",sizeof(processInformation));
result = CreateProcess(NULL, "NOTEPAD.exe",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&startupInfo,
&processInformation
);
if(result == 0)
printf("Could not create process, lasterr = %ld\n", GetLastError());
}
这个等效的 NSIS 脚本在 WinXp 和 Win7 上都失败了:
!include "TextFunc.nsh"
!include "logiclib.nsh"
outfile "process.exe"
!define DEBUG `System::Call kernel32::OutputDebugString(ts)`
Var cmd
Section
StrCpy $cmd "notepad.exe"
;System::Alloc 72 ;// $1 = struct STARTUPINFO
;Pop $1
System::Call "*(i 68, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, &i2 0, &i2 0, i 0, i 0, i 0, i 0) p.r1" ;// StartUp.cb=sizeof(StartUp);
;System::Alloc 16 ;// $2 = struct PROCESS_INFORMATION
;Pop $2
System::Call "*(i 0, i 0, i 0, i 0) p.r2"
;System::Call "*$1(i 68)" ;// StartUp.cb=sizeof(StartUp);
System::Call /NOUNLOAD 'kernel32::CreateProcess(i 0, t $cmd, i 0, i 0, i 0, i 0, i 0, i 0, i r1, i.r2)i.r0 ?e'
Pop $9
${debug} "$0 lasterr=$9"
StrCmp $0 "0" 0 Good
MessageBox MB_OK "CreateProcess failed"
${debug} "CreateProcess failed"
Goto Free
Good:
${debug} "get infos"
System::Call "*$2(i.r3,i.r4,i.r5,i.r6) ?!e"
${debug} "PHND=$3 PID=$5"
System::Call 'kernel32::CloseHandle(i $3)'
System::Call 'kernel32::CloseHandle(i $4)'
Free:
System::Free $1
System::Free $2
SectionEnd
当我替换System::Cal
for结构分配并System::Alloc
改为使用时,没有崩溃但CreateProcess
失败并GetLastError
返回87(ERROR_INVALID_PARAMETER
)
我缺少什么来解决这个问题CreateProcess
?我不想使用内置Exec
的,因为我想使用CreateProcess
nsis 脚本无法访问的标志。
编辑:我得到我的错误:在对CreateProcess
最后一个参数的调用中i.r2
必须是ir2
:内存块PROCESS_INFORMATION
已经分配并且它的指针必须传递给CreateProcess
,而i.r2
忽略调用之前的值并且只在调用之后使用它。感谢安德斯的帮助:)