如何实现按下一个按钮时直接运行masm.exe *.asm?(100分)

  • 主题发起人 主题发起人 sjposw
  • 开始时间 开始时间
S

sjposw

Unregistered / Unconfirmed
GUEST, unregistred user!
如何实现按下一个按钮时直接运行masm.exe *.asm?并且把masm运行后输出的编译信息输入到一个文本编辑器显示.
 
winexec('masm.exe *.asm &gt;&gt; out.text);<br>然后打开out.text读到编辑器中。
 
我用各种方法都试过了(包括winexec),不过都没用<br>如果不加&gt;temp.txt还可以生成*.obj,<br>加了之后masm根本无法运行<br>我的语句如下:<br>winexec(pchar(ExtractFilePath(Application.Exename)+'masm.exe'+' '+FFileName+';'),sw_shownormal);<br>或<br>shellexecute(handle,'open',pchar(ExtractFilePath(Application.Exename)+'masm.exe+' '+FFileName+';'),nil,nil,sw_shownormal);<br>以上两句都能运行无误,只是我想把编译信息输出到memo时,应该怎么重定向?<br>
 
编写自己的“IDE”<br>——如何在图形界面中实时捕获控制台程序的标准输出<br>&nbsp;<br><br>IDE是集成开发环境(Integrated Development Environment)的简称。印象里有很多出色的IDE,比如JBuilder和Kylix,比如Visual Studio。不知大家是否留意过,大多数IDE本身只提供代码编辑、工程管理等人机交互功能,我们在IDE中编译代码、调试程序时,IDE需要调用命令行的编译器、调试器完成相应的操作。例如,使用Visual Studio编译C++程序时,我们会在IDE下方的Output窗口中看到编译和连接的全过程,虽然我们看不到弹出的DOS窗口,但实际上是IDE先后启动了Microsoft C++编译器cl.exe和连接器link.exe这两个命令行程序,而cl.exe和link.exe的输出又实时反映到了IDE的Output窗口中。还有,我们可以在Visual Studio中配置自己需要的工具程序(比如特殊的编译器),然后让Visual Studio在适当的时候运行这些工具,并将工具程序的输出实时显示到Output窗口中。下图是我在Visual Studio 6.0的Output窗口中运行J2SDK的javac.exe编译java源程序并显示程序中语法错误的情形:<br><br><br>也就是说,大多数IDE工具都可以在集成环境中调用特定的命令行程序(WIN32里更确切的说法是控制台程序),然后实时捕获它们的输出(这多半是输出到标准的stdout和stderr流里的东西),并将捕获到的信息显示在图形界面的窗口中。<br><br>这显然是一种具备潜在价值的功能。利用这一技术,我们至少可以<br><br>1. &nbsp; &nbsp; &nbsp; &nbsp; 编写出自己的IDE,如果我们有足够的耐心的话;<br><br>2. &nbsp; &nbsp; &nbsp; &nbsp; 在我们自己的应用程序里嵌入全文检索功能(调用Borland C++里的grep.exe工具),或者压缩和解压缩功能(调用控制台方式的压缩解压程序,比如arj.exe、pkzip.exe等);<br><br>3. &nbsp; &nbsp; &nbsp; &nbsp; 连接其他人编写的,或者我们自己很久以前编写的控制台程序——我经常因为难以调用一个功能强大但又没有源码的控制台程序而苦恼万分。<br><br>这样好的功能是如何实现的呢?<br><br>首先,如果我们想做的是用一个控制台程序调用另一个控制台程序,那就再简单不过了。我们只消把父进程的stdout重定向到某个匿名管道的WRITE端,然后启动子进程,这时,子进程的stdout因为继承的关系也连在了管道的WRITE端,子进程的所有标准输出都写入了管道,父进程则在管道的另一端随时“侦听”——这一技术叫做输入输出的重定向。<br><br>可现在的问题是,GUI方式的Windows程序根本没有控制台,没有stdin、stdout之类的东西,子进程又是别人写好的东西无法更改,这重定向该从何谈起呢?<br><br>还有另外一招:我们可以直接在调用子进程时用命令行中的管道指令“&gt;”将子进程的标准输出重定向到一个文件,子进程运行完毕后再去读取文件内容。这种方法当然可行,但它的问题是,我们很难实时监控子进程的输出,如果子进程不是随时刷新stdout的话,那我们只能等一整块数据实际写入文件之后才能看到运行结果;况且,访问磁盘文件的开销也远比内存中的管道操作来得大。<br><br>我这里给出的方案其实很简单:既然控制台程序可以调用另一个控制台程序并完成输入输出的重定向,那我们完全可以编写一个中介程序,这个中介程序调用我们需要调用的工具程序并随时获取该程序的输出信息,然后直接将信息用约定的进程间通讯方式(比如匿名管道)传回GUI程序,就象下图中这样:<br><br>&nbsp;<br><br>pipe<br>&nbsp;<br>pipe<br>&nbsp;<br>后台线程<br>&nbsp;<br>stdout<br>&nbsp;<br>GUI程序<br>&nbsp;<br>中介程序<br>&nbsp;<br>工具程序<br>&nbsp;<br>&nbsp;<br><br>&nbsp;<br><br>&nbsp;<br><br>&nbsp;<br><br>&nbsp;<br><br>&nbsp;<br><br>&nbsp;<br><br>&nbsp;<br><br>&nbsp;<br><br><br><br>图中,工具程序和中介程序都是以隐藏的方式运行的。工具程序原本输出到stdout的信息被重定向到中介程序开辟的管道中,中介程序再利用GUI程序创建的管道将信息即时传递到GUI程序的一个后台线程里,后台线程负责刷新GUI程序的用户界面(使用后台线程的原因是,只有这样才可以保证信息在GUI界面中随时输出时不影响用户正在进行的其他操作,就象我们在Visual Studio中执行耗时较长的编译功能那样)。<br><br>我写的中介程序名字叫wSpawn,这个名字来自Visual Studio里完成类似功能的中介程序VcSpawn(你可以在Visual Studio的安装目录中找到它)。我的wSpawn非常简单,它利用系统调用_popen()同时完成创建子进程和输入输出重定向两件工作。GUI程序则使用一种特殊的命令行方式调用wSpawn:<br><br>&nbsp; &nbsp; wspawn –h &lt;n&gt; &lt;command&gt; [arg1] [arg2] ...<br><br>其中,-h后跟的是GUI程序提供的管道句柄,由GUI程序自动将其转换为十进制数字,wSpawn运行时将信息写入该句柄中,随后的内容是GUI程序真正要执行的命令行,例如调用C++编译器cl.exe的方式大致如下:<br><br>&nbsp; &nbsp; wspawn –h 1903 cl /Id:/myInclude Test.cpp<br><br>wspawn.cpp的程序清单如下:<br><br>#include &lt;stdlib.h&gt;<br><br>#include &lt;stdio.h&gt;<br><br>#include &lt;string.h&gt;<br><br>#include &lt;string&gt;<br><br>#include &lt;windows.h&gt;<br><br>&nbsp;<br><br>using namespace std;<br><br>&nbsp;<br><br>void exit_friendly(void)<br><br>{<br><br>&nbsp; &nbsp; puts("请不要单独运行wSpawn.");<br><br>&nbsp; &nbsp; exit(0);<br><br>}<br><br>&nbsp;<br><br>int main( int argc, char *argv[] )<br><br>{<br><br>&nbsp; &nbsp; HANDLE &nbsp;hWrite = NULL;<br><br>&nbsp; &nbsp; DWORD &nbsp; dwWrited;<br><br>&nbsp; &nbsp; int &nbsp; &nbsp; i = 0, ret = 0, len = 0;<br><br>&nbsp; &nbsp; char &nbsp; &nbsp;psBuffer[256];<br><br>&nbsp; &nbsp; FILE* &nbsp; child_output;<br><br>&nbsp; &nbsp; string &nbsp;command_line = "";<br><br>&nbsp;<br><br>&nbsp; &nbsp; // 检查命令行,如存在管道句柄,则将其转换为HANDLE类型<br><br>&nbsp; &nbsp; if (argc &lt; 2)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; exit_friendly();<br><br>&nbsp; &nbsp; if (!stricmp(argv[1], "-h"))<br><br>&nbsp; &nbsp; {<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if (argc &lt; 4)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit_friendly();<br><br>&nbsp; &nbsp; &nbsp; &nbsp; hWrite = (HANDLE)atoi(argv[2]);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; i = 3;<br><br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; else<br><br>&nbsp; &nbsp; &nbsp; &nbsp; i = 1;<br><br>&nbsp;<br><br>&nbsp; &nbsp; // 提取要执行的命令<br><br>&nbsp; &nbsp; for (; i &lt; argc; i++)<br><br>&nbsp; &nbsp; {<br><br>&nbsp; &nbsp; &nbsp; &nbsp; command_line += argv;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; command_line += " ";<br><br>&nbsp; &nbsp; }<br><br>&nbsp;<br><br>&nbsp; &nbsp; // 使用_popen创建子进程并重定向其标准输出到文件指针中<br><br>&nbsp; &nbsp; if( (child_output = _popen( command_line.c_str(), "rt" )) == NULL )<br><br>&nbsp; &nbsp; &nbsp; &nbsp; exit( 1 );<br><br>&nbsp;<br><br>&nbsp; &nbsp; while( !feof( child_output ) )<br><br>&nbsp; &nbsp; {<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if( fgets( psBuffer, 255, child_output ) != NULL )<br><br>&nbsp; &nbsp; &nbsp; &nbsp; {<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (hWrite)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 将子进程的标准输出写入管道,提供给自己的父进程<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 格式是先写数据块长度(0表示结束),再写数据块内容<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; len = strlen(psBuffer);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WriteFile(hWrite, &amp;len, sizeof(int), &amp;dwWrited, NULL);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WriteFile(hWrite, psBuffer, len, &amp;dwWrited, NULL);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 如命令行未提供管道句柄,则直接打印输出<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(psBuffer);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; }<br><br>&nbsp; &nbsp; }<br><br>&nbsp;<br><br>&nbsp; &nbsp; // 写“0”表示所有数据都已写完<br><br>&nbsp; &nbsp; len = 0;<br><br>&nbsp; &nbsp; if (hWrite)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; WriteFile(hWrite, &amp;len, sizeof(int), &amp;dwWrited, NULL);<br><br>&nbsp;<br><br>&nbsp; &nbsp; return _pclose( child_output );<br><br>}<br><br>下面,我们就利用wSpawn程序,写一个简单的“IDE”工具。我们选择Visual Studio 6.0作为开发环境(本文给出的代码也在Visual Studio.NET 7.0中做过测试)。首先,创建Visual C++工程myIDE,工程类型为MFC AppWizard(EXE)中的Dialog based类型,即创建了一个主窗口为对话框的GUI程序。工程myIDE的主对话框类是CMyIDEDlg。现在我们要在资源编辑器中为主对话框添加一个足够大的多行编辑框(Edit Box),它的控制ID是IDC_EDIT1,必须为IDC_EDIT1设置以下属性:<br><br>Multiline, Horizontal scroll, Auto HScroll, <br><br>Vertical scroll, Auto VScroll, Want return<br><br>然后用ClassWizard为IDC_EDIT1添加一个对应的成员变量(注意变量的类型要选CEdit型而非字符串CString型)<br><br>CEdit m_edit1;<br><br>使用ClassWizard为“确定”按钮添加消息响应方法OnOK(),编辑该方法:<br><br>void CMyIDEDlg::OnOK() <br><br>{<br><br>&nbsp; &nbsp; AfxBeginThread(myThread, this);<br><br>&nbsp; &nbsp; InvalidateRect(NULL);<br><br>&nbsp; &nbsp; UpdateWindow();<br><br>}<br><br>也就是说,我们在“确定”按钮按下时,启动了后台线程myThread(),那么,myThread()到底做了些什么呢?我们先在CMyIDEDlg类的头文件myIDEDlg.h中加上一个成员函数声明:<br><br>protected:<br><br>&nbsp; &nbsp; static UINT myThread(LPVOID pParam);<br><br>然后,在CMyIDEDlg类的实现文件myIDEDlg.cpp里添加myThread()的实现代码:<br><br>UINT CMyIDEDlg::myThread(LPVOID pParam)<br><br>{<br><br>&nbsp; &nbsp; PROCESS_INFORMATION pi;<br><br>&nbsp; &nbsp; STARTUPINFO siStartInfo;<br><br>&nbsp; &nbsp; SECURITY_ATTRIBUTES saAttr;<br><br>&nbsp; &nbsp; CString Output, tmp;<br><br>&nbsp; &nbsp; char command_line[200];<br><br>&nbsp; &nbsp; DWORD dwRead;<br><br>&nbsp; &nbsp; char* buf; int len;<br><br>&nbsp; &nbsp; HANDLE hRead, hWrite;<br><br>&nbsp;<br><br>&nbsp; &nbsp; CMyIDEDlg* pDlg = (CMyIDEDlg*)pParam;<br><br>&nbsp;<br><br>&nbsp; &nbsp; // 创建与wSpawn.exe通讯的可继承的匿名管道<br><br>&nbsp; &nbsp; saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);<br><br>&nbsp; &nbsp; saAttr.bInheritHandle = TRUE;<br><br>&nbsp; &nbsp; saAttr.lpSecurityDescriptor = NULL;<br><br>&nbsp; &nbsp; if (!CreatePipe(&amp;hRead, &amp;hWrite, &amp;saAttr, 0))<br><br>&nbsp; &nbsp; {<br><br>&nbsp; &nbsp; &nbsp; &nbsp; AfxMessageBox("创建管道失败");<br><br>&nbsp; &nbsp; &nbsp; &nbsp; return 0;<br><br>&nbsp; &nbsp; }<br><br>&nbsp;<br><br>&nbsp; &nbsp; // 准备wSpawn的命令行,在命令行给出写管道句柄和要wSpawn执行的命令<br><br>&nbsp; &nbsp; memset(&amp;pi, 0, sizeof(pi));<br><br>&nbsp; &nbsp; sprintf(command_line, "wspawn -h %d cl /?", (unsigned int)hWrite);<br><br>&nbsp; &nbsp; // 子进程以隐藏方式运行<br><br>&nbsp; &nbsp; ZeroMemory( &amp;siStartInfo, sizeof(STARTUPINFO) );<br><br>&nbsp; &nbsp; siStartInfo.cb = sizeof(STARTUPINFO);<br><br>&nbsp; &nbsp; siStartInfo.wShowWindow = SW_HIDE;<br><br>&nbsp; &nbsp; siStartInfo.dwFlags = STARTF_USESHOWWINDOW;<br><br>&nbsp; &nbsp; // 创建wSpawn子进程<br><br>&nbsp; &nbsp; if (!CreateProcess( NULL, command_line, NULL, NULL, TRUE,<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, NULL, NULL, &amp;siStartInfo, &amp;pi))<br><br>&nbsp; &nbsp; {<br><br>&nbsp; &nbsp; &nbsp; &nbsp; AfxMessageBox("调用wSpawn时失败");<br><br>&nbsp; &nbsp; &nbsp; &nbsp; return 0;<br><br>&nbsp; &nbsp; }<br><br>&nbsp;<br><br>&nbsp; &nbsp; // 读管道,并显示wSpawn从管道中返回的输出信息<br><br>&nbsp; &nbsp; if(!ReadFile( hRead, &amp;len, sizeof(int), &amp;dwRead, NULL) || dwRead == 0)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; return 0;<br><br>&nbsp; &nbsp; while(len)<br><br>&nbsp; &nbsp; {<br><br>&nbsp; &nbsp; &nbsp; &nbsp; buf = new char[len + 1];<br><br>&nbsp; &nbsp; &nbsp; &nbsp; memset(buf, 0, len + 1);<br><br>&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if(!ReadFile( hRead, buf, len, &amp;dwRead, NULL) || dwRead == 0)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;<br><br>&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // 将返回信息中的"/n"替换为Edit Box可识别的"/r/n"<br><br>&nbsp; &nbsp; &nbsp; &nbsp; tmp = buf;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; tmp.Replace("/n", "/r/n");<br><br>&nbsp; &nbsp; &nbsp; &nbsp; Output += tmp;<br><br>&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // 将结果显示在Edit Box中,并刷新对话框<br><br>&nbsp; &nbsp; &nbsp; &nbsp; pDlg-&gt;m_edit1.SetWindowText(Output);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; pDlg-&gt;InvalidateRect(NULL);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; pDlg-&gt;UpdateWindow();<br><br>&nbsp;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; delete[] buf;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if(!ReadFile( hRead, &amp;len, sizeof(int), &amp;dwRead, NULL) || dwRead == 0)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;<br><br>&nbsp; &nbsp; }<br><br>&nbsp;<br><br>&nbsp; &nbsp; // 等待wSpawn结束<br><br>&nbsp; &nbsp; WaitForSingleObject(pi.hProcess, 30000);<br><br>&nbsp; &nbsp; // 关闭管道句柄<br><br>&nbsp; &nbsp; CloseHandle(hRead);<br><br>&nbsp; &nbsp; CloseHandle(hWrite);<br><br>&nbsp; &nbsp; return 0;<br><br>}<br><br>很简单,不是吗?后台线程创建一个匿名管道,然后以隐藏方式启动wSpawn.exe并将管道句柄通过命令行传给wSpawn.exe,接下来只要从管道里读取信息就可以了。现在我们可以试着编译运行myIDE.exe了,记住要把myIDE.exe和wSpawn.exe放在同一目录下。还有,我在myThread()函数中写死了传给wSpawn.exe的待执行的命令行是“cl /?”,这模拟了一次典型的编译过程,如果你不打算改变这一行代码的话,那一定要注意在你的计算机上,C++编译器cl.exe必须位于环境变量PATH指明的路径里,否则wSpawn.exe可就找不到cl.exe了。下面是myIDE程序的运行结果:<br><br><br>补充一点,上面给出的wSpawn利用_popen()完成子进程创建和输入输出重定向,这一方法虽然简单,但只能重定向子进程的stdout或stdin,如果还需要重定向子进程的stderr的话(Java编译器javac就利用stderr输出结果信息),那我们就不能这么投机取巧了。根据以上讨论,你一定可以使用传统的_pipe()、_dup()等系统调用,写出功能更完整的新版wSpawn来,我这里就不再罗嗦了。<br>
 
后退
顶部