📄 subject_17157.htm
字号:
<p>
序号:17157 发表者:清水年华 发表日期:2002-10-09 08:54:13
<br>主题:如何在一个程序中,调用另一个编好的可执行程序呀!
<br>内容:希望大家帮我解决一下这个问题,急用,3xs,:)
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:阿鬼 回复日期:2002-10-09 09:05:00
<br>内容:常见WIN32 API有ShellExecute ShellExecuteEx WinExec<BR>最安全的使用方法:<BR>CreateProcess(NULL, "\"C:\\Program Files\\MyApp.exe\" -L -S", ...)<BR>详细参见<BR>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp<BR><BR>void main( VOID )<BR>{<BR> STARTUPINFO si;<BR> PROCESS_INFORMATION pi;<BR><BR> ZeroMemory( &si, sizeof(si) );<BR> si.cb = sizeof(si);<BR> ZeroMemory( &pi, sizeof(pi) );<BR><BR> // Start the child process. <BR> if( !CreateProcess( NULL, // No module name (use command line). <BR> "MyChildProcess", // Command line. <BR> NULL, // Process handle not inheritable. <BR> NULL, // Thread handle not inheritable. <BR> FALSE, // Set handle inheritance to FALSE. <BR> 0, // No creation flags. <BR> NULL, // Use parent's environment block. <BR> NULL, // Use parent's starting directory. <BR> &si, // Pointer to STARTUPINFO structure.<BR> &pi ) // Pointer to PROCESS_INFORMATION structure.<BR> ) <BR> {<BR> ErrorExit( "CreateProcess failed." );<BR> }<BR><BR> // Wait until child process exits.<BR> WaitForSingleObject( pi.hProcess, INFINITE );<BR><BR> // Close process and thread handles. <BR> CloseHandle( pi.hProcess );<BR> CloseHandle( pi.hThread );<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:徐景周 回复日期:2002-10-09 09:51:01
<br>内容:可试试下面方法:<BR>如何启动一个程序,直到它运行结束? SHELLEXECUTEINFO ShExecInfo = {0};<BR>ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);<BR>ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;<BR>ShExecInfo.hwnd = NULL;<BR>ShExecInfo.lpVerb = NULL;<BR>ShExecInfo.lpFile = "c:\\MyProgram.exe"; <BR>ShExecInfo.lpParameters = ""; <BR>ShExecInfo.lpDirectory = NULL;<BR>ShExecInfo.nShow = SW_SHOW;<BR>ShExecInfo.hInstApp = NULL; <BR>ShellExecuteEx(&ShExecInfo);<BR>WaitForSingleObject(ShExecInfo.hProcess,INFINITE);<BR>或: PROCESS_INFORMATION ProcessInfo; <BR>STARTUPINFO StartupInfo; //This is an [in] parameter<BR>ZeroMemory(&StartupInfo, sizeof(StartupInfo));<BR>StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field<BR>if(CreateProcess("c:\\winnt\\notepad.exe", NULL, <BR> NULL,NULL,FALSE,0,NULL,<BR> NULL,&StartupInfo,&ProcessInfo))<BR>{ <BR> WaitForSingleObject(ProcessInfo.hProcess,INFINITE);<BR> CloseHandle(ProcessInfo.hThread);<BR> CloseHandle(ProcessInfo.hProcess);<BR>} <BR>else<BR>{<BR> MessageBox("The process could not be started...");<BR>}<BR><BR>再如要打开一个CHM帮助文件的话:<BR>// ---------------------------------------------------------<BR>// 名称: OnHelp<BR>// 功能: 打开联机帮助文件<BR>// 变量: 无<BR>// 返回: 无<BR>// 编写: 徐景周,2000.6.3<BR>// ---------------------------------------------------------<BR>void CMainFrame::OnHelp() <BR>{<BR> CString strPath;<BR> GetModuleFileName(NULL,strPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);<BR> strPath.ReleaseBuffer();<BR> int nPos;<BR> nPos=strPath.ReverseFind('\\');<BR> strPath=strPath.Left(nPos);<BR><BR> HINSTANCE result =ShellExecute(NULL, _T("open"), strPath+"\\"+"LovePet.chm", NULL,NULL, SW_SHOW);<BR> <BR> if((UINT)result <= HINSTANCE_ERROR)<BR> AfxMessageBox("\n 抱歉,联机帮助文件LovePet.chm不存在或不在程序存放路径下!");<BR>}<BR><BR>可参看下面文章及源码:<BR>http://www.vckbase.com/document/viewdoc.asp?id=416<BR><BR>http://www.vckbase.com/vckbase/vckbase12/vc/nonctrls/misc_21/1221002.htm
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:黄飚 回复日期:2002-10-09 11:55:26
<br>内容:1最简单的可用WinExec<BR>The WinExec function runs the specified application. <BR><BR>This function is provided for compatibility with 16-bit Windows. Win32-based applications should use the CreateProcess function. <BR><BR>UINT WinExec(<BR> LPCSTR lpCmdLine, // address of command line<BR> UINT uCmdShow // window style for new application<BR>);<BR> <BR>2复杂一点可以用<BR>CreateProcess<BR>The CreateProcess function creates a new process and its primary thread. The new process executes the specified executable file. <BR><BR>BOOL CreateProcess(<BR> LPCTSTR lpApplicationName,<BR> // pointer to name of executable module<BR> LPTSTR lpCommandLine, // pointer to command line string<BR> LPSECURITY_ATTRIBUTES lpProcessAttributes, // process security attributes<BR> LPSECURITY_ATTRIBUTES lpThreadAttributes, // thread security attributes<BR> BOOL bInheritHandles, // handle inheritance flag<BR> DWORD dwCreationFlags, // creation flags<BR> LPVOID lpEnvironment, // pointer to new environment block<BR> LPCTSTR lpCurrentDirectory, // pointer to current directory name<BR> LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO<BR> LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION<BR>);<BR> <BR>3当然可以用<BR>ShellExecute ShellExecuteEx <BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:catnaps 回复日期:2002-12-07 22:36:39
<br>内容:可是还有问题,<BR><BR>如果被调用的程序不是命令行程序,怎么传递参数?<BR><BR>比如有一个图象显示程序,现在另一个可执行程序想调用它显示图象。想调用的时候就传一个文件名过来,让前者立即显示。<BR><BR>这个怎么办?求助
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -