请问如何在程序中执行一个exe文件?我查到了一个函数
int _execl( const char *cmdname, const char *arg0, ... const char *argn, NULL )我是这样调用的:
_execl("good","good",NULL);
其中good是我放在同目录下的一个exe文件,功能是打印出一行文本。结果执行后什么都没有
请问有什么问题??或者还有什么别的方法??
非常感谢。

解决方案 »

  1.   

    UINT WinExec(
      LPCSTR lpCmdLine,  // command line
      UINT uCmdShow      // window style
    );
    或者
    HINSTANCE ShellExecute(
        HWND hwnd, 
        LPCTSTR lpVerb,
        LPCTSTR lpFile, 
        LPCTSTR lpParameters, 
        LPCTSTR lpDirectory,
        INT nShowCmd
    );
    或者
    BOOL CreateProcess(
      LPCTSTR lpApplicationName,                 // name of executable module
      LPTSTR lpCommandLine,                      // command line string
      LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
      LPSECURITY_ATTRIBUTES lpThreadAttributes,  // SD
      BOOL bInheritHandles,                      // handle inheritance option
      DWORD dwCreationFlags,                     // creation flags
      LPVOID lpEnvironment,                      // new environment block
      LPCTSTR lpCurrentDirectory,                // current directory name
      LPSTARTUPINFO lpStartupInfo,               // startup information
      LPPROCESS_INFORMATION lpProcessInformation // process information
    );自己选一个用吧
      

  2.   

    UINT WinExec(
      LPCSTR lpCmdLine,  // 命令行参数,就是可执行文件的路径如:"C:\\test.exe"
      UINT uCmdShow      // 窗口风格,可以为 SW_SHOW,SW_HIDE,SW_MAXIUM .....
    );
    上面这个函数是从16位的Windows沿用下来的,这个函数比较老,不过参数少很容易使用HINSTANCE ShellExecute(
        HWND hwnd, 
        LPCTSTR lpVerb,
        LPCTSTR lpFile, 
        LPCTSTR lpParameters, 
        LPCTSTR lpDirectory,
        INT nShowCmd
    );
    这个函数是Windows的外壳函数,其内部是调用了CreateProcess()函数,参数比较多,具体的就参考一下MSDN 吧!呵呵……BOOL CreateProcess(
      LPCTSTR lpApplicationName,                 // name of executable module
      LPTSTR lpCommandLine,                      // command line string
      LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
      LPSECURITY_ATTRIBUTES lpThreadAttributes,  // SD
      BOOL bInheritHandles,                      // handle inheritance option
      DWORD dwCreationFlags,                     // creation flags
      LPVOID lpEnvironment,                      // new environment block
      LPCTSTR lpCurrentDirectory,                // current directory name
      LPSTARTUPINFO lpStartupInfo,               // startup information
      LPPROCESS_INFORMATION lpProcessInformation // process information
    );
    这个函数是是这三个函数中功能最强大的一个函数,需要设置的信息比较多,可以完成的对子进程的控制也是最多的,值得楼主好好研究一下!
    上面的就是俺说的,希望对楼主有所帮助!
      

  3.   

    UINT WinExec(
      LPCSTR lpCmdLine,  // 命令行参数,就是可执行文件的路径如:"C:\\test.exe"
      UINT uCmdShow      // 窗口风格,可以为 SW_SHOW,SW_HIDE,SW_MAXIUM .....
    );
    上面这个函数是从16位的Windows沿用下来的,这个函数比较老,不过参数少很容易使用HINSTANCE ShellExecute(
        HWND hwnd, 
        LPCTSTR lpVerb,
        LPCTSTR lpFile, 
        LPCTSTR lpParameters, 
        LPCTSTR lpDirectory,
        INT nShowCmd
    );
    这个函数是Windows的外壳函数,其内部是调用了CreateProcess()函数,参数比较多,具体的就参考一下MSDN 吧!呵呵……BOOL CreateProcess(
      LPCTSTR lpApplicationName,                 // name of executable module
      LPTSTR lpCommandLine,                      // command line string
      LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
      LPSECURITY_ATTRIBUTES lpThreadAttributes,  // SD
      BOOL bInheritHandles,                      // handle inheritance option
      DWORD dwCreationFlags,                     // creation flags
      LPVOID lpEnvironment,                      // new environment block
      LPCTSTR lpCurrentDirectory,                // current directory name
      LPSTARTUPINFO lpStartupInfo,               // startup information
      LPPROCESS_INFORMATION lpProcessInformation // process information
    );
    这个函数是是这三个函数中功能最强大的一个函数,需要设置的信息比较多,可以完成的对子进程的控制也是最多的,值得楼主好好研究一下!
    上面的就是俺说的,希望对楼主有所帮助!
      

  4.   

    我看你像是控制台的程序,那就用winexec吧!