procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
sinfo:TSTARTUPINFO;
pinfo:TProcessInformation;
ct,et,kt,ut:Tfiletime;
hinputfile,houtputfile:THandle;
security:PSecurityAttributes;
begin
try
//CreatePipe(hReadPipe, hWritePipe, nil, 0);
security.nLength:=sizeof(security);
security.lpSecurityDescriptor:=NIL;
security.bInheritHandle:=TRUE;
hInputFile:=CreateFile(pchar('input.txt'),GENERIC_READ,FILE_SHARE_READ,security,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
houtputFile:=CreateFile(pchar('output.txt'),GENERIC_WRITE,FILE_SHARE_WRITE,security,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); FillChar(sInfo,sizeof(sInfo),#0);
sInfo.cb := SizeOf(sInfo); // CREATE_NEW_CONSOLE or
sinfo.dwFlags:=STARTF_USESTDHANDLES;
sinfo.hStdInput:=hInputFile;
sinfo.hStdOutput:=houtputFile; if not CreateProcess('xxx.exe',nil,nil,nil,false,NORMAL_PRIORITY_CLASS, nil, nil, sInfo, pInfo) then
MessageBox(Application.handle,'指定程序启动失败!','错误',MB_OK or MB_ICONSTOP)
else
begin
while (true) do
begin
sleep(1000);
//WaitForSingleObject(pInfo.hProcess,INFINITE);
getprocesstimes(pinfo.hProcess,ct,et,kt,ut);
if (ut.dwHighDateTime-ut.dwLowDateTime)>1000000000 then
begin
CloseHandle( pinfo.hProcess );
CloseHandle( pinfo.hThread );
CloseHandle(houtputFile);
CloseHandle(hInputFile);
TerminateProcess(pinfo.hProcess,1);
ShowMessage(IntToStr(GetLastError));
showmessage('运行超时');
edit6.Text:=inttostr(kt.dwHighDateTime-kt.dwLowDateTime);
exit;
end;
end;
end;
except
showmessage('测试失败');
end;
end;运行时下面错误:
---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 00419C97 in module 'Project1.exe'. Read of address 00000014'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------哪位大哥帮忙看看哪位了.可能问题是出现在CREATEFILE处.我的程序代码是用来调用外部可执行程序.测试外部可执行程序.测试数据在INPUT.TXT中.测试结果输出到OUTPUT.TXT中.
var i:integer;
sinfo:TSTARTUPINFO;
pinfo:TProcessInformation;
ct,et,kt,ut:Tfiletime;
hinputfile,houtputfile:THandle;
security:PSecurityAttributes;
begin
try
//CreatePipe(hReadPipe, hWritePipe, nil, 0);
security.nLength:=sizeof(security);
security.lpSecurityDescriptor:=NIL;
security.bInheritHandle:=TRUE;
hInputFile:=CreateFile(pchar('input.txt'),GENERIC_READ,FILE_SHARE_READ,security,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
houtputFile:=CreateFile(pchar('output.txt'),GENERIC_WRITE,FILE_SHARE_WRITE,security,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); FillChar(sInfo,sizeof(sInfo),#0);
sInfo.cb := SizeOf(sInfo); // CREATE_NEW_CONSOLE or
sinfo.dwFlags:=STARTF_USESTDHANDLES;
sinfo.hStdInput:=hInputFile;
sinfo.hStdOutput:=houtputFile; if not CreateProcess('xxx.exe',nil,nil,nil,false,NORMAL_PRIORITY_CLASS, nil, nil, sInfo, pInfo) then
MessageBox(Application.handle,'指定程序启动失败!','错误',MB_OK or MB_ICONSTOP)
else
begin
while (true) do
begin
sleep(1000);
//WaitForSingleObject(pInfo.hProcess,INFINITE);
getprocesstimes(pinfo.hProcess,ct,et,kt,ut);
if (ut.dwHighDateTime-ut.dwLowDateTime)>1000000000 then
begin
CloseHandle( pinfo.hProcess );
CloseHandle( pinfo.hThread );
CloseHandle(houtputFile);
CloseHandle(hInputFile);
TerminateProcess(pinfo.hProcess,1);
ShowMessage(IntToStr(GetLastError));
showmessage('运行超时');
edit6.Text:=inttostr(kt.dwHighDateTime-kt.dwLowDateTime);
exit;
end;
end;
end;
except
showmessage('测试失败');
end;
end;运行时下面错误:
---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 00419C97 in module 'Project1.exe'. Read of address 00000014'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------哪位大哥帮忙看看哪位了.可能问题是出现在CREATEFILE处.我的程序代码是用来调用外部可执行程序.测试外部可执行程序.测试数据在INPUT.TXT中.测试结果输出到OUTPUT.TXT中.
hInputFile:=CreateFile(pchar('input.txt'),GENERIC_READ,FILE_SHARE_READ,security,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
houtputFile:=CreateFile(pchar('output.txt'),GENERIC_WRITE,FILE_SHARE_WRITE,security,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);的指针出了什么问题.好像.