我在程序中使用ShellExecute函数调用别的程序,现在要求必须调用的程序执行完毕后操作才能继续,请问各位怎样判断ShellExecute函数是否执行完毕?

解决方案 »

  1.   

    改用CreateProcess吧
    void main( VOID )
    {
        STARTUPINFO si;
        PROCESS_INFORMATION pi;    ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );    // Start the child process. 
        if( !CreateProcess( NULL, // No module name (use command line). 
            "MyChildProcess", // Command line. 
            NULL,             // Process handle not inheritable. 
            NULL,             // Thread handle not inheritable. 
            FALSE,            // Set handle inheritance to FALSE. 
            0,                // No creation flags. 
            NULL,             // Use parent's environment block. 
            NULL,             // Use parent's starting directory. 
            &si,              // Pointer to STARTUPINFO structure.
            &pi )             // Pointer to PROCESS_INFORMATION structure.
        ) 
        {
            ErrorExit( "CreateProcess failed." );
        }    // Wait until child process exits.
        WaitForSingleObject( pi.hProcess, INFINITE );    // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
    }
      

  2.   

    楼上的方法就可以了
    也可以用ShellExecuteEx,
    用WaitForSingleObject等待参数中的hProcess,看一下msdn
      

  3.   

    如何启动一个程序,直到它运行结束? SHELLEXECUTEINFO ShExecInfo = {0};
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = NULL;
    ShExecInfo.lpFile = "c:\\MyProgram.exe";
    ShExecInfo.lpParameters = "";
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOW;
    ShExecInfo.hInstApp = NULL;
    ShellExecuteEx(&ShExecInfo);
    WaitForSingleObject(ShExecInfo.hProcess,INFINITE);或: PROCESS_INFORMATION ProcessInfo; 
    STARTUPINFO StartupInfo; //This is an [in] parameter
    ZeroMemory(&StartupInfo, sizeof(StartupInfo));
    StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field
    if(CreateProcess("c:\\winnt\\notepad.exe", NULL, 
        NULL,NULL,FALSE,0,NULL,
        NULL,&StartupInfo,&ProcessInfo))

        WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
        CloseHandle(ProcessInfo.hThread);
        CloseHandle(ProcessInfo.hProcess);
    }  
    else
    {
        MessageBox("The process could not be started...");
    }
      

  4.   


    谢谢各位的帮助,但使用CreateProcess的话参数怎么写呢?
    ShellExecute(0, NULL, _T("aaaa.exe"), _T("123456"), NULL, SW_SHOWNORMAL)
    其中的aaaa.exe是我调用别人的程序,123456是需要传入的参数,如果改为CreateProcess,应该怎么写呢?
      

  5.   

    我先试一下zhucde(【風間苍月】)(MVP)大侠提供的方法,可以的话马上揭贴。
      

  6.   

    例子
    CreateProcess("c:\\winnt\\aaaa.exe 123456", NULL, 
        NULL,NULL,FALSE,0,NULL,
        NULL,&StartupInfo,&ProcessInfo)
      

  7.   

    WinExec
    原型:
    UINT WinExec(
      LPCSTR lpCmdLine,  // address of command line
      UINT uCmdShow      // window style for new application
    );
    用于十六位操作系统及兼容系统.
    例如:
    WinExec("notepad.exe f:\\调用程序.txt",SW_SHOW);
    WinExec("notepad.exe ",SW_SHOW);
    不同的参数用空格分开,故路径中不能有空格,而大部分程序默认是安装在"...\Program Files\...",如word,这极大的限制了WinExec的应用范围.
    以上可不带路径:
    1,程序所在目录.
    2,当前路径.
    3,系统目录,可以用GetSystemDirectory得到.
    4,Windows 目录. 可以用TheGetWindowsDirectory得到.  
    5,在环境变量中设置的目录.ShellExecute
    原型:
    HINSTANCE ShellExecute(
        HWND hwnd,                   //父窗口句柄
        LPCTSTR lpOperation,         //操作,"open","print","explore"
        LPCTSTR lpFile,              //文件名,前面可加路径
        LPCTSTR lpParameters,        //参数
        LPCTSTR lpDirectory,         //默认文件夹
        INT nShowCmd                 //显示方式
    );打开一个应用程序
    ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW );

    ShellExecute(this->m_hWnd,"open","notepad.exe","c:\MyLog.log","",SW_SHOW );打开一个同系统程序相关连的文档
    ShellExecute(this->m_hWnd,"open","c:\abc.txt","","",SW_SHOW );激活相关程序,发送EMAIL
    ShellExecute(this->m_hWnd,"open","mailto:[email protected]","","", SW_SHOW );用系统打印机打印文档
    ShellExecute(this->m_hWnd,"print","c:\abc.txt","","", SW_HIDE);lpParameters的用法示例:
    一,建立一个可以接受参数的程序call.exe,添加如下代码:
    BOOL CCallApp::InitInstance()
    {
    int n = __argc;
    for(int i = 1 ; i < n ; i++)
    AfxMessageBox(__targv[i]);
           //__targv[0]存储的是程序的文件名
    ...
    }
    二,Alt + F7的进行Project setting, Debug -> program argurments ->"1 2 3 4 5".
    如果有多个参数,用空格分开.
    三,运行.
    四,执行ShellExecute(NULL,NULL,"f:\\call.exe","1 2 3 4 5",NULL,SW_SHOW);BOOL CreateProcess(
      LPCTSTR lpApplicationName,
                             
      LPTSTR lpCommandLine,  
      LPSECURITY_ATTRIBUTES lpProcessAttributes,  
      LPSECURITY_ATTRIBUTES lpThreadAttributes,   
      BOOL bInheritHandles,  
      DWORD dwCreationFlags, 
      LPVOID lpEnvironment,  
      LPCTSTR lpCurrentDirectory,   
      LPSTARTUPINFO lpStartupInfo,  
      LPPROCESS_INFORMATION lpProcessInformation  
    );
    STARTUPINFO   startupInfo;
    memset(&startupInfo,0,sizeof(STARTUPINFO));
    startupInfo.cb = sizeof(STARTUPINFO);示例:
    //程序最启动时最大化
    startupInfo.dwFlags |= STARTF_USESHOWWINDOW;
    startupInfo.wShowWindow = SW_SHOWMAXIMIZED;

    //运行....exe
     PROCESS_INFORMATION ProcessInfo;
      BOOL bCreate = ::CreateProcess
            (
            "f:\\call.exe",// 1 2 3 4",
    NULL,
    NULL,
            NULL,
            FALSE,
            0,
            NULL,
            NULL,
            &startupInfo,
            &ProcessInfo); //等到call.exe执行完毕
    WaitForSingleObject(ProcessInfo.hProcess,1000000);
    MessageBox("调用程序结束!");
      

  8.   

    WinExec
    原型:
    UINT WinExec(
      LPCSTR lpCmdLine,  // address of command line
      UINT uCmdShow      // window style for new application
    );
    用于十六位操作系统及兼容系统.
    例如:
    WinExec("notepad.exe f:\\调用程序.txt",SW_SHOW);
    WinExec("notepad.exe ",SW_SHOW);
    不同的参数用空格分开,故路径中不能有空格,而大部分程序默认是安装在"...\Program Files\...",如word,这极大的限制了WinExec的应用范围.
    以上可不带路径:
    1,程序所在目录.
    2,当前路径.
    3,系统目录,可以用GetSystemDirectory得到.
    4,Windows 目录. 可以用TheGetWindowsDirectory得到.  
    5,在环境变量中设置的目录.ShellExecute
    原型:
    HINSTANCE ShellExecute(
        HWND hwnd,                   //父窗口句柄
        LPCTSTR lpOperation,         //操作,"open","print","explore"
        LPCTSTR lpFile,              //文件名,前面可加路径
        LPCTSTR lpParameters,        //参数
        LPCTSTR lpDirectory,         //默认文件夹
        INT nShowCmd                 //显示方式
    );打开一个应用程序
    ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW );

    ShellExecute(this->m_hWnd,"open","notepad.exe","c:\MyLog.log","",SW_SHOW );打开一个同系统程序相关连的文档
    ShellExecute(this->m_hWnd,"open","c:\abc.txt","","",SW_SHOW );激活相关程序,发送EMAIL
    ShellExecute(this->m_hWnd,"open","mailto:[email protected]","","", SW_SHOW );用系统打印机打印文档
    ShellExecute(this->m_hWnd,"print","c:\abc.txt","","", SW_HIDE);lpParameters的用法示例:
    一,建立一个可以接受参数的程序call.exe,添加如下代码:
    BOOL CCallApp::InitInstance()
    {
    int n = __argc;
    for(int i = 1 ; i < n ; i++)
    AfxMessageBox(__targv[i]);
           //__targv[0]存储的是程序的文件名
    ...
    }
    二,Alt + F7的进行Project setting, Debug -> program argurments ->"1 2 3 4 5".
    如果有多个参数,用空格分开.
    三,运行.
    四,执行ShellExecute(NULL,NULL,"f:\\call.exe","1 2 3 4 5",NULL,SW_SHOW);BOOL CreateProcess(
      LPCTSTR lpApplicationName,
                             
      LPTSTR lpCommandLine,  
      LPSECURITY_ATTRIBUTES lpProcessAttributes,  
      LPSECURITY_ATTRIBUTES lpThreadAttributes,   
      BOOL bInheritHandles,  
      DWORD dwCreationFlags, 
      LPVOID lpEnvironment,  
      LPCTSTR lpCurrentDirectory,   
      LPSTARTUPINFO lpStartupInfo,  
      LPPROCESS_INFORMATION lpProcessInformation  
    );
    STARTUPINFO   startupInfo;
    memset(&startupInfo,0,sizeof(STARTUPINFO));
    startupInfo.cb = sizeof(STARTUPINFO);示例:
    //程序最启动时最大化
    startupInfo.dwFlags |= STARTF_USESHOWWINDOW;
    startupInfo.wShowWindow = SW_SHOWMAXIMIZED;

    //运行....exe
     PROCESS_INFORMATION ProcessInfo;
      BOOL bCreate = ::CreateProcess
            (
            "f:\\call.exe",// 1 2 3 4",
    NULL,
    NULL,
            NULL,
            FALSE,
            0,
            NULL,
            NULL,
            &startupInfo,
            &ProcessInfo); //等到call.exe执行完毕
    WaitForSingleObject(ProcessInfo.hProcess,1000000);
    MessageBox("调用程序结束!");