启动进程——使用CreateProcess需注意的地方

       启动一个进程有三种方法:WinExecShellExecuteCreateProcess。这里只说明CreateProcess的用法。

       CreateProcess的参数可以查MSDN和《Windows API参考手册》,这里不具体说明。下面给出常用的方法:

      

     STARTUPINFO startup;

     ZeroMemory( &startup, sizeof(startup) );

     startup.cb = sizeof(startup);

     ZeroMemory( &m_procInfo, sizeof(m_procInfo) );

     // 初始化结果体是必须的,否则会异常

     BOOL bRetVal = ::CreateProcess( (LPCWSTR)strProcPath,

                                          NULL,

                                          NULL,

                                          NULL,

                                          FALSE,

                                          0,

                                          NULL,

                                          NULL,

                                          &startup,

                                          &m_procInfo );

     if ( !bRetVal )

     {

         AfxMessageBox( _T( "启动失败!" ) );

     }

       第一个参数是应用程序的全路径,第二个参数是命令行参数。如果第一个参数为空,就必须在第二个参数中指定应用程序的路径。

       如果启动的进程与当前进程不再同一个目录下,则第八个参数必须指定,第八个参数为启动的进程所在的路径。否则不能成功启动进程。如下所示:

     BOOL bRetVal = ::CreateProcess( (LPCWSTR)strProcPath,

                                          NULL,

                                          NULL,

                                          NULL,

                                          FALSE,

                                          0,

                                          NULL,

                                          (LPCWSTR)strProcFolder,

                                          &startup,

                                          &m_procInfo );

     if ( !bRetVal )

     {

         AfxMessageBox( _T( "启动失败!" ) );

     }

你可能感兴趣的:(windows,api,null)