我想实现这个功能:在我的程序里用户按下一个按钮后就用记事本打开c:\abc.txt。。而用户按下另一个按钮后就把这个记事本程序关闭。请问这个功能应该如何实现呢??

解决方案 »

  1.   

    ShellExecute
    Performs an operation on a specified file. HINSTANCE ShellExecute(
        HWND hwnd, 
        LPCTSTR lpOperation,
        LPCTSTR lpFile, 
        LPCTSTR lpParameters, 
        LPCTSTR lpDirectory,
        INT nShowCmd
    );使用上面的函数打开一个程序(记事本),参数是c:\abc.txt,
    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
    );
    使用这个函数关闭一个程序
      

  2.   

    ShellExecute(this->m_hWnd,"open","notepad.exe",
        "c:\\abc.txt","",SW_SHOW );
      

  3.   

    ShellExecute好像不太好控制你运行的程序
    最好用CreateProcess创建进程,然后获得这个进程的instance,只要获得了进程的聚丙,爱怎么做都可以了(TerminateProcess)
      

  4.   

    打开:
    HINSTANCE hInstance=ShellExecute(this->m_hWnd,"open","notepad.exe",
        "c:\\abc.txt","",SW_SHOW );
    关闭
    TerminateProcess(hInstance,0);
      

  5.   

    给你个进程的例子:先加一个全局变量 HANDLE m_hPro;
    再初始化为 m_hPro = NULL;
    下面两段分别是创建一个记事本进程,和关闭这个进程~
    void CCtrlOtherPrcssDlg::OnButtono() //打开
    {
    // TODO: Add your control notification handler code here
    PROCESS_INFORMATION pi;
    STARTUPINFO si; memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);
    si.wShowWindow = SW_SHOW;
    si.dwFlags = STARTF_USESHOWWINDOW; BOOL fRet = CreateProcess(NULL,
    "c:\\windows\\notepad.exe",
    NULL,
    NULL,
    FALSE,
    NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
    NULL,
    NULL,
    &si,
    &pi); if(!fRet) //failed
    {
    LPVOID lpMsgBuf;
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
    (LPTSTR)&lpMsgBuf,
    0,
    NULL);
    AfxMessageBox((LPCTSTR)lpMsgBuf);
    LocalFree(lpMsgBuf);
    }
    else
    {
    AfxMessageBox("CreateProcess OK");
    m_hPro = pi.hProcess;
    }
    }void CCtrlOtherPrcssDlg::OnButtonc() //关闭
    {
    // TODO: Add your control notification handler code here
    if(m_hPro){ //Close
    if(!TerminateProcess(m_hPro, 0)) //Error
    {
    LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
    (LPTSTR)&lpMsgBuf,
    0,
    NULL);
    AfxMessageBox((LPCTSTR)lpMsgBuf);
    LocalFree(lpMsgBuf);
    }
    else
    {
    AfxMessageBox("TerminateProcess OK");
    }
    m_hPro = NULL;
    }
    else
    AfxMessageBox("m_hPro is NULL");
    }
      

  6.   

    使用TerminateProcess()是不是太野蛮了?不知道向主线程发送WM_QUIT消息是否好用,没使用过
      

  7.   

    用VC调用别的可执行文件:
    方法一:CFileFind ff;
    if(ff.FindFile("sql.exe"))
    {
        ShellExecute(NULL,"open","sql.exe",NULL,NULL,SW_SHOW);
    }
    else
    {
        AfxMessageBox("文件没找到!");
    }
    方法二CFileFind ff;
    if(ff.FindFile("sql.exe"))
    {
        WinExec("sql.exe",SW_SHOW);
    }
    else
    {
        AfxMessageBox("文件没找到!");
    }
     终止:
    TerminateProcess()
      

  8.   

    用VC调用别的可执行文件:
    方法一:CFileFind ff;
    if(ff.FindFile("sql.exe"))
    {
        ShellExecute(NULL,"open","sql.exe",NULL,NULL,SW_SHOW);
    }
    else
    {
        AfxMessageBox("文件没找到!");
    }
    方法二CFileFind ff;
    if(ff.FindFile("sql.exe"))
    {
        WinExec("sql.exe",SW_SHOW);
    }
    else
    {
        AfxMessageBox("文件没找到!");
    }
     终止:
    TerminateProcess()