在delphi中如何运行一个可执行文件,并可在进程中关闭文件.

解决方案 »

  1.   

    关闭程序
    TerminateProcess(Handle, 0);
      

  2.   

    void __fastcall TfmMain::RunExe(const AnsiString ExeName)
    {
      RECT R1; GetWindowRect(Handle,&R1);
      RECT R2=Rect(Screen->Width/2,Screen->Height/2,Screen->Width/2,Screen->Height/2);
      STARTUPINFO           StartupInfo;
      PROCESS_INFORMATION   ProcessInfo;
      ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));
      StartupInfo.cb = sizeof(STARTUPINFO);
      try
      {
        if(CreateProcess(NULL,ExeName.c_str(),NULL,NULL,TRUE,
                         NORMAL_PRIORITY_CLASS, NULL, NULL,
                         &StartupInfo,&ProcessInfo))
        {
          Hide();
          DrawAnimatedRects(Handle,3,&R1,&R2);
          WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
        }
        else
          MessageBox(Handle,AnsiString("运行失败!\n\n"+ExeName).c_str(),"提示信息",MB_OK|MB_ICONERROR);
      }
      __finally
      {
        if(!Visible)
        {
          CloseHandle(ProcessInfo.hProcess);
          CloseHandle(ProcessInfo.hThread);
          DrawAnimatedRects(Handle,3,&R2,&R1);
          Sleep(200);
          Show();
        }
      }
    }
      

  3.   

    {声明}
    var
    MyHandle: THandle;
    MyStartupInfo: TStartupInfo;
    MyProcessInfo: TProcessInformation;
    {执行函数}
    procedure ExecuteApp(MyPath: String);
    begin
    FillChar(MyStartupInfo, SizeOf(MyStartupInfo), 0);
    MyStartupInfo.cb:=SizeOf(MyStartupInfo);
    CreateProcess(PChar(MyPath), nil, nil, nil, False,
    DETACHED_PROCESS, nil, nil, MyStartupInfo,
    MyProcessInfo);
    MyHandle:=MyProcessInfo.hProcess;
    //把执行的程序的句柄赋值给MyHandle,它会在终止程序时用到
    end;
    {结束}
    procedure CloseApp(MyHandle: THandle);
    begin
    TerminateProcess(MyHandle, 0);
    end;
    {用的时候MyPath就是要运行的exe文件带路径名}