subject_18021.htm
来自「一些关于vc的问答」· HTM 代码 · 共 24 行
HTM
24 行
<p>
序号:18021 发表者:bwind 发表日期:2002-10-17 11:24:16
<br>主题:如何把ping封装起来/如何得到window注册的组建就是在开始-运行里可以直接输入的命令?
<br>内容:第一个问题关键是如何在button 的click中执行:是用ShellExec还是其他?<BR> 如何得到ping的结果?这样就可以把他显示在edit等控间中了.<BR><BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:iwill 回复日期:2002-10-17 11:32:39
<br>内容:给你个例子,用管道实现:sFileName可以换成任意dos程序<BR>CString m_sFileName="ping 127.100.0.1 ";<BR><BR> HANDLE hReadPipe,hWritePipe; //read and write pipe handle;<BR> STARTUPINFO si; <BR> PROCESS_INFORMATION pi;<BR> CWaitCursor WaitCursor;<BR> <BR> if(!CreatePipe(&hReadPipe,&hWritePipe,NULL,0)) //create the pipe;<BR> {<BR> AfxMessageBox("Couldn't create pipe!\n");<BR> return;<BR> }<BR> memset(&si,0,sizeof(STARTUPINFO));<BR> si.cb=sizeof(STARTUPINFO);<BR> si.dwFlags=STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;<BR> si.wShowWindow=SW_HIDE; //hide the dos programe;<BR> si.hStdOutput=hWritePipe; //Look carefully,very important,change the standard out device to our pipe;<BR> <BR> if(!CreateProcess(NULL, //create dos process ;<BR> (char *)(LPCTSTR)m_sFileName,<BR> NULL,<BR> NULL,<BR> false,<BR> 0,<BR> NULL,<BR> NULL,<BR> &si,<BR> &pi))<BR> {<BR> CString mes;<BR> mes.Format ("Couldn't create process!\r\nError code:%d",GetLastError());<BR> AfxMessageBox(mes);<BR> return;<BR> }<BR> DWORD cchReadBuffer;<BR> TCHAR ph[5000];<BR> CString result,mes;<BR> for(;;)<BR> {<BR> cchReadBuffer=0;<BR> if(!PeekNamedPipe(hReadPipe, //look the pipe if there is data to availabel to read;<BR> ph,<BR> sizeof(ph),<BR> &cchReadBuffer,<BR> NULL,<BR> NULL)) break;<BR> if(cchReadBuffer)<BR> { <BR> if(!ReadFile(hReadPipe, //read the output from the pipe;<BR> ph,<BR> 4096,<BR> &cchReadBuffer,<BR> NULL)) break;<BR> ph[cchReadBuffer]=0; //carefully,the last char must be '\0';<BR> result+=ph; <BR> }<BR> else<BR> {<BR> if(::WaitForSingleObject(pi.hProcess,0)==WAIT_OBJECT_0) break;<BR> //if the process over,then quit;<BR> }<BR> Sleep(500);<BR> }<BR> mes.Format ("执行结果: \n%s",result);<BR> MessageBox(mes,"执行结果");<BR> //close all the handle that have been use,don't forget!<BR> CloseHandle(hReadPipe);<BR> CloseHandle(pi.hThread);<BR> CloseHandle(pi.hProcess);<BR> CloseHandle(hWritePipe);
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:徐景周 回复日期:2002-10-17 11:52:23
<br>内容:已将WINDOW下实现的PING发到你的邮箱了,查收
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:bwind 回复日期:2002-10-17 13:14:51
<br>内容:谢谢回答,jz_x的信件已经收到<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 + =
减小字号Ctrl + -
显示快捷键?