WINDOWS pipeline technology

xiaoxiao2021-03-05  21

WINDOWS pipeline technology

I don't know if you have used this program, and they don't decompress the function, but call the DOS program PKZIP to complete the decompression of the ZIP package. However, in the program running, there is no window of the DOS console and everything should appear in the information displayed under the DOS. This design is beautiful and prevents minority, fast users, and close your DOS window in advance. Now let's discuss how this feature is implemented with anonymous pipeline technology. The pipeline technology has been long, and it is believed that many people are most familiar with pipe technology in the DOS command. When we want a file, if you want him to page the reality, you can enter C: /> Type Autoexec.bat | More "|" is the pipe operator. He is used by TYPE output as a read end, which is the pipeline established by the written end of the input. More pipelines used in Windows are also anonymous pipes, which is created via the API function CreatePipe. Bool createpipe (Phandle Hreadpipe, Pointing Pointing Phandle HWRITEPIPE, / / ​​Pointer to Write Handle LPSecurity_Attributes LPPIPEATTRIBUTES, / / ​​Pointing to the Pointer DWORD NSize // of the security attribute structure); in several parameters Pay attention to HREADPIPE, hwritepipe is a pointer to the handle, not a handle (I am wrong when I first used it). Nsize is generally specified as 0 to allow the system to determine the capacity of the pipe. Now see the security attribute structure, security_attributes. TypedEf struct _security_attributes {// sa dord nlength; lpvoid lpsecurityDescriptor; bool binherithandle;} security_attributes; NLENGTH is the size of the structure, nature is used in Sizeof. LPSecurityDescriptor is a security descriptor (a string of a C-style). BinheritHandle He pointed out if the security described object can be inherited by the newly created process. Don't worry about their specific meaning, you will naturally know when you use it. OK, now let's create a pipeline HANDLE hReadPipe, hWritePipe; SECURITY_ATTRIBUTES sa; sa.nLength = sizeof (SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; // use the system default security descriptor sa.bInheritHandle = TRUE; // must For TRUE, otherwise the handle cannot be inherited. CreeaTepipe (& Hreadpipe, & HwritePipe, & Sa, 0); OK, our pipeline is built. Of course, this is not the ultimate goal, our purpose is to redirect what the program on DOS is redirected to an Edit control of a Windows program. So we also need to start a DOS program first, but you can't show the window of the DOS console (otherwise not stuffing). We create a DOS program with CreateProcesss.

BOOL CreateProcess (LPCTSTR lpApplicationName, // C-style string: Name LPTSTR lpCommandLine application, // C-style string: command execution LPSECURITY_ATTRIBUTES lpProcessAttributes, // process security attributes LPSECURITY_ATTRIBUTES lpThreadAttributes, // thread security attributes BOOL bInheritHandles // if the inheritance flag DWORD dwCreationFlags handle, // create a sign LPVOID lpEnvironment, // C-Style string: environment settings LPCTSTR lpCurrentDirectory, // C-Style string: perform directory lPSTARTUPINFO lpStartupInfo, // start information LPPROCESS_INFORMATION lpProcessInformation // Process information); Don't go, the parameters are more, but most of you need to fill yourself if you fill a null. LPApplication is just a bit. LPCommandline, but the command you want to execute must be written seriously. Come, we look at LPProcessAttributes and LPTHREADATTRIBUTES how to set it. Hey? Isn't this just that? For A, but it is simpler than just. Because we just create a process, he can be inherited, so these two values ​​are NULL. BinheritHandles is also a must set to True, because we must let the new process can output information to the process of calling his process, you must let the new process inherit the handle of the process. We don't have anything else to create, so DWCREATIONFLAGS is NULL. LpenVironment and LPCurrentDirectory are finished according to your own request, usually null. The next LPStartupinfo is the key, we have to look at it seriously. typedef struct _STARTUPINFO {// si DWORD cb; LPTSTR lpReserved; LPTSTR lpDesktop; LPTSTR lpTitle; DWORD dwX; DWORD dwY; DWORD dwXSize; DWORD dwYSize; DWORD dwXCountChars; DWORD dwYCountChars; DWORD dwFillAttribute; DWORD dwFlags; WORD wShowWindow; WORD cbReserved2; LPBYTE LPRESERVED2; HANDLE HSTDINPUT; Handle Hstdoutput; Handle Hstderror;} Startupinfo, * LPStartupInfo; So many parameters, a one is sure to die. Yes, MS has long thought of being exhausted. So providing the API function GetStartupinfo, a rescued life. Void getStartupinfo (LPStartupInfo lpstartupinfo); this function is used to get the current process Startupinfo. Our new process base root is almost the startupinfo of the current process, just borrow it. Then make a small modification. There are such a few places we have to change: CB, DWFLAGS, HSTDOUTPUT, HSTDERROR, WSHOWINDOW. Let's talk about CB, he refers to the size of Startupinfo, or the old manifests SIZEOF. Besides WshowWindow, he has developed a realistic state of the window when the new process is created, this property is of course given SW_HIDE, isn't we hide the newly built DOS process?

Haha, see HSTDOUTPUT and HSTDERROR, standard output and error output handles. The key place is here, as long as we set these two handles to hwrite, once our process has standard output, we will write to our anonymous pipeline that we have just established, we use the pipe's HREADPIPE handle to read the content Is it going to achieve our goal? Oh, it's really easy to say it. These few key parameters have been completed, don't forget DWFLAGS. He is used to develop this pile of parameters in StartupInfo. Since we use HSTDOUTPUT, HSTDERROR and WSHOWINDOW That DWFLAGS is given to Startf_useshowWindow | Startf_usestDHandles. Ok, now go back to the last parameter of CreateProcess LPProcessInformation (Tired!). Oh, this parameter does not need to be filled with himself. He is the information returned by CreateProcess, as long as it gives him an address of a process_information structure. Maximic is high, and the pipeline is connected to the standard output of the new process, and one end can be read with the API function readfile. Wait, wrong, our pipeline has problems. We gave HWRITE to HSTDOUTPUT and HSTDERROR, then open a pipe writing in the new process when the new process starts, and we use createPipe in the current process to create a pipe, then there is this in the current process. The write end of the pipe is hwrite. Ok, there is a deformity pipe with two writes and a read-out end here. Such pipes must have problems. Since the current process does not use the write, we must turn off the writing of the current process. In this way, our pipeline is really successful. Take a look at the source program written by VC : In a CEDIT control of the DIR /® help through pipe technology.

转载请注明原文地址:https://www.9cbs.com/read-32427.html

New Post(0)