MsgWaitForMultipleObjects返回访问被拒绝

问题描述:

我有一个创建一个进程后续功能:MsgWaitForMultipleObjects返回访问被拒绝

function .CreateProcess(aAppletPath: string; var aError : string; aProcessInfo: TProcessInformation): Boolean; 
var 
    StartInfo: TStartupInfo; 
begin 
    FillChar(StartInfo, SizeOf(TStartupInfo),#0); 
    FillChar(aProcessInfo, SizeOf(TProcessInformation),#0); 
    StartInfo.cb := SizeOf(TStartupInfo); 
    if False then begin 
    StartInfo.dwFlags := STARTF_USESHOWWINDOW; 
    StartInfo.wShowWindow := SW_HIDE; 
    end; 
    if Windows.CreateProcess(nil, PChar(aAppletPath), nil, nil, False, CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil, nil, StartInfo, aProcessInfo) then begin 
    Result := True; 
    WaitForInputIdle(aProcessInfo.hProcess, oTimeOutSecs * 1000); 
    end 
    else begin 
    Result := False; 
    end; 
end; 

而且我有这个方法等待应用程序终止:

function WaitForProcessTerminate(aHandle: THandle) : Boolean; 
var 
    vResult : LongWord; 
    Msg: TMsg; 
    PHandles: Pointer; 
begin 
    vResult := 0; 
    PHandles := @aHandle; 
    PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE); 
    while True do begin 
    vResult := Windows.MsgWaitForMultipleObjects(1, PHandles^, False, oTimeOutSecs * 1000, QS_ALLINPUT); 
    if vResult = WAIT_OBJECT_0 + 1 then begin 
     if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then begin 
     TranslateMessage(Msg); 
     DispatchMessage(Msg); 
     end; 
    end 
    else begin 
     Break; 
    end; 
    end; 
    case vResult of 
     WAIT_ABANDONED: Result := False; 
     WAIT_OBJECT_0: Result := True; 
     WAIT_TIMEOUT: Result := False; 
    else begin 
     Result := False; 
    end; 
    end; 
    if not Result then begin 
    ShowMessage(SystemErrorMessage); 
    end; 
end; 

的问题是等待功能总是返回WAIT_FAILEDAccess denied消息。我究竟做错了什么?此代码是德尔福2010年和我打电话的应用程序是一个Java应用程序。

+1

请在调用'CreateProcess'之前调用'UniqueString(aAppletPath)'; API可以修改你传递给它的字符串,所以你应该确保*你的*调用者不会得到一个修改后的值。 – 2011-06-14 21:09:41

没关系的家伙。这是我的错误。功能:

function .CreateProcess(aAppletPath: string; var aError : string; aProcessInfo: TProcessInformation): Boolean; 

应该是:

function .CreateProcess(aAppletPath: string; var aError : string; var {should be var!!} aProcessInfo: TProcessInformation): Boolean; 

我的道歉。

+1

更好的是,在调用函数之前,使用'out'向调用者表明他们不需要分配任何东西。 – 2011-06-14 21:07:16

+0

感谢您的提示 – 2011-06-14 21:08:28