WinExec(path + '\file.exe', sw_show);

解决方案 »

  1.   

    你调用api函数loadmodule不行吗?
      

  2.   

    The LoadModule function loads and executes a Windows-based application or creates a new instance of an existing Windows-based application. This function is provided for compatibility with earlier versions of Windows. Win32-based applications should use the CreateProcess function. DWORD LoadModule(    LPCSTR lpModuleName, // address of filename to load 
        LPVOID lpParameterBlock  // address of parameter block for new module  
       );
     ParameterslpModuleNamePoints to a null-terminated string that contains the filename of the application to run. If the lpModuleName parameter does not contain a directory path, Windows searches for the executable file in this order: 1. The directory from which the application loaded. 
    2. The current directory. 
    3. Windows 95: The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.Windows NT: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is SYSTEM32.4. Windows NT: The 16-bit Windows system directory. There is no Win32 function that obtains the path of this directory, but it is searched. The name of this directory is SYSTEM.
    5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. 
    6. The directories that are listed in the PATH environment variable.  lpParameterBlockPoints to an application-defined LOADPARMS32 structure that defines the new application's parameter block. 
    The LOADPARMS32 structure has the following form: typedef struct tagLOADPARMS32 {  
        LPSTR lpEnvAddress;  // address of environment strings 
        LPSTR lpCmdLine;     // address of command line 
        LPSTR lpCmdShow;     // how to show new program 
        DWORD dwReserved;    // must be zero 
    } LOADPARMS32; 
     Member Description
    lpEnvAddress Points to an array of null-terminated strings that supply the environment strings for the new process. The array has a value of NULL as its last entry. A value of NULL for this parameter causes the new process to start with the same environment as the calling process.
    lpCmdLine Points to a Pascal-style string that contains a correctly formed command line. The first byte of the string contains the number of bytes in the string. The remainder of the string contains the command line arguments, excluding the name of the child process. If there are no command line arguments, this parameter must point to a zero length string; it cannot be NULL.
    lpCmdShow Points to a structure containing two WORD values. The first value must always be set to two. The second value specifies how the application window is to be shown and is used to supply the wShowWindow member of the STARTUPINFO structure to the CreateProcess function. See the description of the nCmdShow parameter of the ShowWindow function for a list of acceptable values.
    dwReserved This parameter is reserved; it must be zero.
     Set all unused members to NULL, except for lpCmdLine, which must point to a null-terminated string if it is not used.  Return ValuesIf the function succeeds, the return value is greater than 31.
    If the function fails, the return value is an error value, which may be one of the following: Value Meaning
    0 The system is out of memory or resources.
    ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
    ERROR_FILE_NOT_FOUND The specified file was not found.
    ERROR_PATH_NOT_FOUND The specified path was not found.
     ResWin32-based applications should use the CreateProcess function. In the Win32 API, the implementation of the LoadModule function calls CreateProcess. The following section describes how each parameter for CreateProcess is formed: CreateProcess parameter Value
    lpszImageName LoadModule lpModuleName parameter.
    lpszCommandLine LoadModule lpParameterBlock->lpCmdLine.
    lpsaProcess NULL.
    lpsaThread NULL.
    fInheritHandles FALSE.
    fdwCreate 0.
    lpvEnvironment LoadModule lpParameterBlock->lpEnvAddress.
    lpszCurDir NULL.
    lpsiStartInfo The structure is initialized to zero. The cb member is set to the size of the structure, and the wShowWindow member is set to the value of the second word of the LoadModule lpParameterBlock->lpCmdShow parameter.
    lppiProcInfo.hProcess The handle is immediately closed.
    lppiProcInfo.hThread The handle is immediately closed.你先看一下帮助再说三少 :o)
      

  3.   

    我是这样的:
    procedure TForm1.Button1Click(Sender: TObject);
    type
      tagLOADPARMS32=Record
       lpEnvAddress: Pchar;
       lpCmdLine   : Pchar;
       lpCmdShow   : ^DWord;
       dwReserved  : DWord;
      end;
    Var
      P1 :PChar;
      P  :^tagLOADPARMS32;
      i1    : integer;
      S     : String[80];
    begin
       P1:='D:\DDD\project1.exe';
       S:='D:\DDD\project1.exe'+#0;
       with p^ do
        Begin
         lpEnvAddress:=nil;
         lpCmdLine:=@S[0];
         i1:=SW_Show;
         lpCmdShow:=@i1;
         dwReserved:=0;
        End;
        i1:=Loadmodule(P1,P);
       if i1<=31 then Edit1.Text:=intTostr(i1);
    end;结果报告:
    0 The system is out of memory or resources.大少!!!
      

  4.   

    我觉得你因该用CreateProcess,因为Win32-based applications should use the CreateProcess function. 三少 :o)
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      StartUpInfo: TStartUpInfo;        
      ProcessInfo: TProcessInformation; 
      CurDir: string;                   
    begin
      FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
      with StartupInfo do
      begin
        cb := SizeOf(TStartupInfo);
        dwFlags := STARTF_USESHOWWINDOW;
        wShowWindow := SW_SHOWNORMAL;
      end;
      CurDir := ExtractFilePath(ParamStr(0))+'YourProject.exe';
      CreateProcess(PChar(CurDir), nil, nil, nil, False,
          NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);
    end;这样可以实现,LoadModule是用来兼容windows版本比较低的程序,所以我建议你用CreateProcess可以激活另外一个进程的.
    不知道这个解答你满意吗?三少 :o)
      

  6.   

    不好意思,刚才去开会了。你的方法是激活另外一个进程。有办法让这个'YourProject.exe'退出后,TForm1才有效吗,相当于'YourProject.exe'是我的一个Form。先给40,最后再给60。谢谢!!
      

  7.   

    windownt下可能只能使用ShellExecuateEX
      

  8.   

    那里能得到Shell32.lib??一样给分!!
      

  9.   

    To hlbl11:
      你那上面的意思是想你调用的程序象你的窗口.showmodal吗 ?三少 :o)
      

  10.   

    我觉得你的想法挺好的,我觉得两个没有语言门户的程序可以通过动态链接库实现一些入口和出口供两个程序调用,也可以进行相应的处理.你上面的要求我还没有研究过,不过我想你发送一个消息让你所调用的程序Stay on Top = true ,不知可否?三少 :o)
      

  11.   

    you can try to wait for the child process exit using the handle returned in processinfo
      

  12.   

    I do not have delphi. I think you can do the same thing in delphi.if(CreateProcess(NULL, "calc.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
    // disable something
    WaitForSingleObject(pi.hThread, INFINITE);
    // enable something
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
    }
      

  13.   

    三少:两个程序除非都是你自己编的,不能调用其他公司的产品,例如上面人兄提到的“Cal.exe”.对吗?fengye:感谢你的参与,你的方法原理上行得通。加10分
      

  14.   

    有办法让这个'YourProject.exe'退出后,TForm1才有效吗,相当于'YourProject.exe'是我的一个Form。先给40,最后再给60。
    ??????????????
    要达到这个效果,请看下面的例程:
    //执行一个外部的程序模块,并且等待他的结束。
    //cmd用于指明调用的模块文件名,workdir指明模块运行时的工作目录,visiable用于指明模块
    //的显示方式,若成功函数返回运行的程序的返回值,否则返回0;
    //Ex:
    //   WinExecExW('c:\windows\notepad.exe','',sw_show);
    //程序将运行记事本,并且等待记事本程序的结束,此时你的程序不能进行任何操作。
    Function WinExecExW(cmd,workdir:pchar;visiable:integer):DWORD;
    var
     StartupInfo:TStartupInfo;
     ProcessInfo:TProcessInformation;
    begin
     FillChar(StartupInfo,SizeOf(StartupInfo),#0);
     StartupInfo.cb:=SizeOf(StartupInfo);
     StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;
     StartupInfo.wShowWindow:=visiable;
     if not CreateProcess(nil,cmd,nil,nil,false,Create_new_console or Normal_priority_class,nil,nil,StartupInfo,ProcessInfo) then
       result:=0
     else
     begin
       waitforsingleobject(processinfo.hProcess,INFINITE);
       GetExitCodeProcess(ProcessInfo.hProcess,Result);
     end;
    end;
      

  15.   

    如果你是想别人的程序运行在你得程序窗口里面.实施
    var
     hWnd: THandle;
    ...
    hWnd := FindWindow();
    if hWnd <> 0 then Windows.Setparent(hWnd);