如题
解决方案 »
- Excel导入数据到Access中问题?????
- 汇总问题,由于当初表结构没考虑全,导致汇总起来相当刺手,高手出招啊!
- 关于TStringList的特殊排序问题,急!
- 运行可执行文件,如flashget出现异常时,他自动调用delphi进行调试,请问如何设置,不让调用调试程序?
- 回复人:Taken(铁拳)来收剩下的80分
- 求delphi新手教程视频,从语法开始的。谢谢!
- 如何得知当前运行程序的绝对路径??
- 高分求源代码——求刻录光盘的源代码,能够说明过程的即可。大家帮帮忙!
- 请教下indy10的TIdTcpServer的问题
- 求助高手。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。!!!!!!!!
- 请问aiirii(ari-爱的眼睛),在使用你的这段代码时是否可以将dos窗口隐藏运行我的程序呢?
- 哪位高手知道idsmtpserver的使用方法?
2. 保存这个handle / 不保存也可以。。
3. TerminateProcess() // 传入1/2的那个 handle.The CreateProcess function is used to run a new program. The WinExec and LoadModule functions are still available, but they are implemented as calls to CreateProcess. The CreateProcess function creates a new process and its primary thread. The new process executes the specified executable file. BOOL CreateProcess(
LPCTSTR lpApplicationName,
// pointer to name of executable module
LPTSTR lpCommandLine, // pointer to command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // process security attributes
LPSECURITY_ATTRIBUTES lpThreadAttributes, // thread security attributes
BOOL bInheritHandles, // handle inheritance flag
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // pointer to new environment block
LPCTSTR lpCurrentDirectory, // pointer to current directory name
LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO
LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION
);
// 注意最后一个参数....里面
PROCESS_INFORMATION
The PROCESS_INFORMATION structure is filled in by the CreateProcess function with information about a newly created process and its primary thread. typedef struct _PROCESS_INFORMATION { // pi
HANDLE hProcess; // 这个参数就是新执行的handle 到时候你TerminateProcess
HANDLE hThread; // 的时候就用这个 hProcess
DWORD dwProcessId; //
DWORD dwThreadId;
} PROCESS
然后..TerminateProcess
The TerminateProcess function terminates the specified process and all of its threads. BOOL TerminateProcess(
HANDLE hProcess, // handle to the process
UINT uExitCode // exit code for the process
);
还是用 CreateProcess好
將 WaitForSingleObject 去掉就可, 修改後面的代碼,
需要時就用 TerminateProcess 終止程序
uses Forms, Windows;procedure TForm1.Button1Click(Sender: TObject);
var
proc_info: TProcessInformation;
startinfo: TStartupInfo;
ExitCode: longword;
begin
// Initialize the structures
FillChar(proc_info, sizeof(TProcessInformation), 0);
FillChar(startinfo, sizeof(TStartupInfo), 0);
startinfo.cb := sizeof(TStartupInfo); // Attempts to create the process
if CreateProcess('c:\windows\notepad.exe', nil, nil,
nil, false, NORMAL_PRIORITY_CLASS, nil, nil,
startinfo, proc_info) <> False then begin
// The process has been successfully created
// No let's wait till it ends...
WaitForSingleObject(proc_info.hProcess, INFINITE);
// Process has finished. Now we should close it.
GetExitCodeProcess(proc_info.hProcess, ExitCode); // Optional
CloseHandle(proc_info.hThread);
CloseHandle(proc_info.hProcess);
Application.MessageBox(
PChar(Format('Notepad finished! (Exit code=%d)', [ExitCode])),
'Info', MB_ICONINFORMATION);
end else begin
// Failure creating the process
Application.MessageBox('Couldn''t execute the '
+ 'application', 'Error', MB_ICONEXCLAMATION);
end;//if
end;
begin
FillChar(proc_info, sizeof(TProcessInformation), 0);
FillChar(startinfo, sizeof(TStartupInfo), 0);
startinfo.cb := sizeof(TStartupInfo);
if CreateProcess(nil, 'c:\windows\notepad.exe', nil,
nil, false, CREATE_DEFAULT_ERROR_MODE
+ NORMAL_PRIORITY_CLASS, nil, nil, startinfo,
proc_info) then begin
btnExecute.Enabled := False;
btnCancel.Enabled := True;
Timer1.Enabled := True;
end else begin
CloseHandle(proc_info.hProcess);
Application.MessageBox('Couldn''t execute the '
+ 'application', 'Error', MB_ICONEXCLAMATION);
end;
end;
procedure TForm1.btnCancelClick(Sender: TObject);
begin
Timer1.Enabled := False;
if Application.MessageBox('You should try to finish'
+ ' the application normally.'#13#13'¿Terminate it '
+ 'anyway?', 'Warning', MB_YESNO + MB_DEFBUTTON2 +
MB_ICONQUESTION + MB_TASKMODAL) = ID_YES then begin
TerminateProcess(proc_info.hProcess, 0);
CloseHandle(proc_info.hProcess);
btnCancel.Enabled := False;
btnExecute.Enabled := True;
end else begin
Timer1.Enabled := True;
end;
end;
SENDMESSAGE(DOSHANDLE,WM_KEYDOWN,CTRL+C,0)
从而中止应用程序的执行.比较容易实现.
StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
StartUpInfo.wShowWindow := SW_HIDE;使用这个参数!!!
// create hidden process
if CreateProcess(nil, PChar(FileName), nil, nil,False, IDLE_PRIORITY_CLASS,
nil, nil, StartUpInfo,ProcessInfo) then
begin
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
end;