⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_17157.htm

📁 一些关于vc的问答
💻 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>&nbsp;&nbsp;&nbsp;&nbsp;STARTUPINFO si;<BR>&nbsp;&nbsp;&nbsp;&nbsp;PROCESS_INFORMATION pi;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;ZeroMemory( &si, sizeof(si) );<BR>&nbsp;&nbsp;&nbsp;&nbsp;si.cb = sizeof(si);<BR>&nbsp;&nbsp;&nbsp;&nbsp;ZeroMemory( &pi, sizeof(pi) );<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;// Start the child process. <BR>&nbsp;&nbsp;&nbsp;&nbsp;if( !CreateProcess( NULL, // No module name (use command line). <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"MyChildProcess", // Command line. <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Process handle not inheritable. <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Thread handle not inheritable. <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FALSE,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Set handle inheritance to FALSE. <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// No creation flags. <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Use parent's environment block. <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Use parent's starting directory. <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&si,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Pointer to STARTUPINFO structure.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&pi )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Pointer to PROCESS_INFORMATION structure.<BR>&nbsp;&nbsp;&nbsp;&nbsp;) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ErrorExit( "CreateProcess failed." );<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;// Wait until child process exits.<BR>&nbsp;&nbsp;&nbsp;&nbsp;WaitForSingleObject( pi.hProcess, INFINITE );<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;// Close process and thread handles. <BR>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle( pi.hProcess );<BR>&nbsp;&nbsp;&nbsp;&nbsp;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";&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>ShExecInfo.lpParameters = "";&nbsp;&nbsp;&nbsp;&nbsp;<BR>ShExecInfo.lpDirectory = NULL;<BR>ShExecInfo.nShow = SW_SHOW;<BR>ShExecInfo.hInstApp = NULL;&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;NULL,NULL,FALSE,0,NULL,<BR>&nbsp;&nbsp;&nbsp;&nbsp;NULL,&StartupInfo,&ProcessInfo))<BR>{ <BR>&nbsp;&nbsp;&nbsp;&nbsp;WaitForSingleObject(ProcessInfo.hProcess,INFINITE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(ProcessInfo.hThread);<BR>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(ProcessInfo.hProcess);<BR>}&nbsp;&nbsp;<BR>else<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;MessageBox("The process could not be started...");<BR>}<BR><BR>再如要打开一个CHM帮助文件的话:<BR>// ---------------------------------------------------------<BR>//&nbsp;&nbsp;&nbsp;&nbsp;名称: OnHelp<BR>//&nbsp;&nbsp;&nbsp;&nbsp;功能: 打开联机帮助文件<BR>//&nbsp;&nbsp;&nbsp;&nbsp;变量: 无<BR>//&nbsp;&nbsp;&nbsp;&nbsp;返回: 无<BR>//&nbsp;&nbsp;&nbsp;&nbsp;编写: 徐景周,2000.6.3<BR>// ---------------------------------------------------------<BR>void CMainFrame::OnHelp() <BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;CString strPath;<BR>&nbsp;&nbsp;&nbsp;&nbsp;GetModuleFileName(NULL,strPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);<BR>&nbsp;&nbsp;&nbsp;&nbsp;strPath.ReleaseBuffer();<BR>&nbsp;&nbsp;&nbsp;&nbsp;int nPos;<BR>&nbsp;&nbsp;&nbsp;&nbsp;nPos=strPath.ReverseFind('\\');<BR>&nbsp;&nbsp;&nbsp;&nbsp;strPath=strPath.Left(nPos);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;HINSTANCE result =ShellExecute(NULL, _T("open"), strPath+"\\"+"LovePet.chm", NULL,NULL, SW_SHOW);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;if((UINT)result &lt;= HINSTANCE_ERROR)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;LPCSTR lpCmdLine,&nbsp;&nbsp;// address of command line<BR>&nbsp;&nbsp;UINT uCmdShow&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 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>&nbsp;&nbsp;LPCTSTR lpApplicationName,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // pointer to name of executable module<BR>&nbsp;&nbsp;LPTSTR lpCommandLine,&nbsp;&nbsp;// pointer to command line string<BR>&nbsp;&nbsp;LPSECURITY_ATTRIBUTES lpProcessAttributes,&nbsp;&nbsp;// process security attributes<BR>&nbsp;&nbsp;LPSECURITY_ATTRIBUTES lpThreadAttributes,&nbsp;&nbsp; // thread security attributes<BR>&nbsp;&nbsp;BOOL bInheritHandles,&nbsp;&nbsp;// handle inheritance flag<BR>&nbsp;&nbsp;DWORD dwCreationFlags, // creation flags<BR>&nbsp;&nbsp;LPVOID lpEnvironment,&nbsp;&nbsp;// pointer to new environment block<BR>&nbsp;&nbsp;LPCTSTR lpCurrentDirectory,&nbsp;&nbsp; // pointer to current directory name<BR>&nbsp;&nbsp;LPSTARTUPINFO lpStartupInfo,&nbsp;&nbsp;// pointer to STARTUPINFO<BR>&nbsp;&nbsp;LPPROCESS_INFORMATION lpProcessInformation&nbsp;&nbsp;// 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 + -