用ShellExecute打开一个文本文件,
系统用notepad打开该文本文件,
怎样用程序得知该notepad是否关闭了?

解决方案 »

  1.   

    是不是可以开一个线程并用WaitForSingleObject函数?
      

  2.   

    http://expert.csdn.net/Expert/topic/1008/1008739.xml?temp=.1015283
      

  3.   

    HINSTANCE ShellExecute(
        HWND hwnd, 
        LPCTSTR lpVerb,
        LPCTSTR lpFile, 
        LPCTSTR lpParameters, 
        LPCTSTR lpDirectory,
        INT nShowCmd
    );
    函数的返回值不是真正的HINSTANCE,从而不能再程序中使用它进行窗口的关联!
    你最好再打开了notepad后,
    用函数CWnd::FindWindow(....)去捕捉你打开的窗口,利用该函数返回的CWnd*
    然后::IsWindow(pWnd->m_hWnd);判断窗口是否还存在
      

  4.   

    利用 ShellExecuteEx 可以得到新进程的句柄,然后利用 WaitForSingleObject 可以BOOL ShellExecuteEx(
        LPSHELLEXECUTEINFO lpExecInfo
    );
      

  5.   

    >>不是直接打开另一个程序,而是要知道由文件关联打开的程序是否关闭
    看看这样行不行.
    得先得到关联到某种文件的程序的位置,然后列举当前所有进程,检查该进程的路径和可执行文件名是否和先前得到的一致,如果是就证明该程序没有关闭,不然就是关闭了.
      

  6.   

    同意In355Hz(好象一条狗) ( )
      

  7.   

    >> kingcom_xu(杀不了人的刀,水园屠狗ing!) 如果有多个程序实例怎么办?
    例如已经打开多个word文档
      

  8.   

    CreateProcess也可以得到打开进程的句柄。进程关闭则句炳成激发状态,可以使所有的Wait...API函数返回。
      

  9.   

    http://expert.csdn.net/Expert/topic/1008/1008739.xml?temp=.1015283
      

  10.   

    回复人: In355Hz(好象一条狗) ( ) 信誉:110  2003-03-09 13:44:00  得分:0 
     
     
      利用 ShellExecuteEx 可以得到新进程的句柄,然后利用 WaitForSingleObject 可以BOOL ShellExecuteEx(
        LPSHELLEXECUTEINFO lpExecInfo
    );  ------------------------------------------------
    老大:
    请仔细的看一下msdn好不好!
    //=====================================
    ShellExecute
    Performs an operation on a specified file. HINSTANCE ShellExecute(
        HWND hwnd, 
        LPCTSTR lpVerb,
        LPCTSTR lpFile, 
        LPCTSTR lpParameters, 
        LPCTSTR lpDirectory,
        INT nShowCmd
    );Return Values
    Returns a value greater than 32 if successful, or an error value that is less than or equal to 32 otherwise. The following table lists the error values. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Microsoft® Windows® applications. It is not a true HINSTANCE, however. The only thing that can be done with the returned HINSTANCE is to cast it to an integer and compare it with the value 32 or one of the error codes below.0  The operating system is out of memory or resources. 
    ERROR_FILE_NOT_FOUND  The specified file was not found. 
    ERROR_PATH_NOT_FOUND  The specified path was not found. 
    ERROR_BAD_FORMAT  The .exe file is invalid (non-Win32® .exe or error in .exe image). 
    SE_ERR_ACCESSDENIED  The operating system denied access to the specified file.  
    SE_ERR_ASSOCINCOMPLETE  The file name association is incomplete or invalid. 
    SE_ERR_DDEBUSY  The DDE transaction could not be completed because other DDE transactions were being processed. 
    SE_ERR_DDEFAIL  The DDE transaction failed. 
    SE_ERR_DDETIMEOUT  The DDE transaction could not be completed because the request timed out. 
    SE_ERR_DLLNOTFOUND  The specified dynamic-link library was not found.  
    SE_ERR_FNF  The specified file was not found.  
    SE_ERR_NOASSOC  There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable. 
    SE_ERR_OOM  There was not enough memory to complete the operation. 
    SE_ERR_PNF  The specified path was not found. 
    SE_ERR_SHARE  A sharing violation occurred. 
      

  11.   

    睁大眼睛仔细看一看:
    The return value is cast as an HINSTANCE for backward compatibility with 16-bit Microsoft® Windows® applications. It is not a true HINSTANCE, however. The only thing that can be done with the returned HINSTANCE is to cast it to an integer and compare it with the value 32 or one of the error codes below.
      

  12.   

    ShellExecuteEx
    Performs an action on a file. BOOL ShellExecuteEx(
        LPSHELLEXECUTEINFO lpExecInfo
    );If the function succeeds, it sets the hInstApp member of the SHELLEXECUTEINFO structure to a value greater than 32. If the function fails, hInstApp is set to the SE_ERR_XXX error value that best indicates the cause of the failure. Although hInstApp is declared as an HINSTANCE for compatibility with 16-bit Microsoft Windows applications, it is not a true HINSTANCE. It can only be cast to an integer and compared to either 32 or the SE_ERR_XXX error codes.
      

  13.   

    shellexecute 不能得到真正的句柄,你要用 createprocess, 然后waitforsingleobject:  STARTUPINFO si;
      PROCESS_INFORMATION pi;  ZeroMemory(&si,sizeof(si));
      si.cb=sizeof(si);
      ZeroMemory(&pi,sizeof(pi));  // Start the child process. 
      CreateProcess(NULL, // No module name (use command line). 
          "notepad",              // 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.  WaitForSingleObject(pi.hProcess, INFINITE);  // Close process and thread handles. 
      CloseHandle( pi.hProcess );
      CloseHandle( pi.hThread );记得多给分:))
      

  14.   

    ZouMorn(默文) 的代码比较管用哦
      

  15.   

    SHELLEXECUTEINFO ShExecInfo = {0};
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = NULL;
    ShExecInfo.lpFile = "c:\\winnt\\notepad.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...");
    }
      

  16.   

    想要正常终止启动的程序,除了findwindow还有什么办法吗谁给解决方案,我另开贴送分
      

  17.   

    UINT uExitCode;
    ::TerminateProcess(pi.hProcess,uExitCode);//我找到一个方法,不知道这样是否会引起那个被运行起来的程序的问题啊。//也就是这个是不是强制推出。
      

  18.   

    用CreateProcess打开!
    你用ShellExecute这种轻量级api本身就是为了方便的!