0

我没有设法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::Calfor结构分配并System::Alloc改为使用时,没有崩溃但CreateProcess失败并GetLastError返回87(ERROR_INVALID_PARAMETER

我缺少什么来解决这个问题CreateProcess?我不想使用内置Exec的,因为我想使用CreateProcessnsis 脚本无法访问的标志。

编辑:我得到我的错误:在对CreateProcess最后一个参数的调用中i.r2必须是ir2:内存块PROCESS_INFORMATION已经分配并且它的指针必须传递给CreateProcess,而i.r2忽略调用之前的值并且只在调用之后使用它。感谢安德斯的帮助:)

4

1 回答 1

3

p如果您使用的是 NSIS 2.46,则不是有效类型,我相信您的 STARTUPINFO 大小错误。

Section 
System::Call '*(i,i,i,i)i.r0'
System::Alloc 72
pop $4
System::Call "*$4(i 72)"
StrCpy $1 '"explorer.exe" "$startmenu"'
System::Call 'kernel32::CreateProcess(i0,tr1,i0,i0,i0,i 0,i0,i0,ir4,ir0)i.r3'
DetailPrint RET=$3
System::Free $4
${If} $3 <> 0
    System::Call "*$0(i.r3,i.r4)"
    System::Call 'kernel32::CloseHandle(i $3)'
    System::Call 'kernel32::CloseHandle(i $4)'
${EndIf}
System::Free $0
SectionEnd
于 2012-12-19T20:40:05.683 回答