I wonder if you have used such a program. They do not have the decompression function, but call the DOS program PKZIP to decompress the zip package. However, when the program is running, there is no dos Console window and all the information that should have been displayed under DOS appears in the text box of the installer. This design is both beautiful and can prevent a few users who are eye-catching from closing your DOS window in advance.
Now let's discuss how to use the anonymous pipeline technology to implement this function.
Pipeline technology has been around for a long time. I believe many people are most familiar with the pipeline technology in the DOS command. When we type a file, we can input it if we want it to be paged.
C:> type autoexec. Bat | more
Here "|" is the pipeline operator. It uses the type output information as the Reading end and the more input end as the pipeline established by the writing end.
In Windows, a large number of pipelines are also anonymous pipelines, which are created using the createpipe API function.
Bool createpipe (
Phandle hreadpipe, // pointer to the read-side handle
Phandle hwritepipe, // pointer to the write handle
Lpsecurity_attributes lppipeattributes, // pointer to the Security Attribute Structure
DWORD nsize // MPs queue capacity
);
In the preceding parameters, note that hreadpipe, hwritepipe, is the pointer to the handle, rather than the handle (I used it wrong for the first time ). Nsize is generally set to 0 so that the system determines the pipeline capacity. Now let's look at the security attribute structure, security_attributes.
Typedef struct _ security_attributes {// SA
DWORD nlength;
Lpvoid lpsecuritydescriptor;
Bool binherithandle;
} Security_attributes;
Nlength is the size of the struct, which is naturally obtained using sizeof. Lpsecuritydescriptor is a security descriptor (a C-style string ). Binherithandle indicates whether the security description object can be inherited by the newly created process. Don't worry about their specific meaning. You will naturally know it when using it.
Okay. Now let's create a pipeline.
Handle hreadpipe, hwritepipe;
Security_attributes SA;
SA. nlength = sizeof (security_attributes );
SA. lpsecuritydescriptor = NULL; file: // use the default security descriptor of the system.
SA. binherithandle = true; file: // it must be true. Otherwise, the handle cannot be inherited.
Creeatepipe (& hreadpipe, & hwritepipe, & SA, 0 );
Our pipeline has been built. Of course, this is not the ultimate goal. Our goal is to redirect the output of a program on DOS to the Edit Control of a Windows program. So we still need to start a DOS program first, and there is no window in the DOS Console (otherwise, we will not reveal it ). We use CreateProcess to create a DOS program process.
Bool CreateProcess (
Lptstr lpapplicationname, // C-style string: Application name
Lptstr lpcommandline, // C-style string: executed command
Lpsecurity_attributes lpprocessattributes, // process Security Attribute
Lpsecurity_attributes lpthreadattributes, // thread Security Attribute
Bool binherithandles, // whether to inherit the HANDLE flag
DWORD dwcreationflags, // create a flag
Lpvoid lpenvironment, // C-style string: Environment Settings
Maid directory, // C-style string: Execution directory
Lpstartupinfo, // start information
Lpprocess_information lpprocessinformation // Process Information
);
Don't go first. The parameter is a little more, but most of them do not need to fill in or leave a null parameter. Lpapplication can be used at any time. Lpcommandline but the commands you want to execute must be carefully written. Let's see how to set lpprocessattributes and lpthreadattributes. Ah? This is the one you just mentioned. Yes, but it is simpler than just now. Since we only create a process and whether it can be inherited or not, the two values are all null. Binherithandles must also be set to true, because since we want a new process to output information to the process that calls it, we must let the new process inherit the handle of the calling process. We have no requirement on the newly created process, so dwcreationflags is null. According to your own requirements, lpenvironment and lpcurrentdirectory are just a bit simple. Generally, they are also null. The next lpstartupinfo is the key. Let's take a closer look.
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;
If there are so many parameters, one write will surely be exhausted. That's right, Ms has long thought it would be exhausting. Therefore, it provides the life-saving API function getstartupinfo.
Void getstartupinfo (
Lpstartupinfo
);
This function is used to obtain the startupinfo of the current process. The newly created process is basically the same as the startupinfo of the current process. just borrow it. Then you can make a small modification.
Here are a few changes: CB, dwflags, hstdoutput, hstderror, and wshowwindow. Let's talk about CB. It refers to the size of startupinfo or the sizeof method. Besides, wshowwindow sets the real state of the window when the new process is created. This property is of course for sw_hide. Aren't we going to hide the newly created DoS process. Haha, see hstdoutput and hstderror, standard output and error output handle. The key point is that as long as we set these two handles to hwrite, our processes will be written into the newly established anonymous pipeline once there is a standard output, we can use the hreadpipe handle of the pipeline to read the content and write it into the Edit Control. It's really easy to say. After these key parameters are completed, do not forget dwflags. This parameter is used to specify the value of this heap parameter in startupinfo. Now that we use hstdoutput, hstderror, and wshowwindow, dwflags will give startf_useshowwindow | startf_usestdhandles.
Return to the last parameter lpprocessinformation (tired!) of CreateProcess !). You don't need to fill in this parameter. It is the information returned by CreateProcess. You just need to give him the address of the process_information structure case.
This is a big success. one end of the pipeline is connected to the standard output end of the new process, and one end can be read by itself using the API function readfile. Wait, no. Our pipeline is still faulty. After hwrite is given to hstdoutput and hstderror, a pipeline write end will be opened in the new process when the new process starts, when createpipe is used in the current process to create an MPS queue, hwrite is also available in the current process. Well, there is a malformed pipe with two writing ends and one reading end. There must be a problem with such pipelines. Since the current process does not use the write end, we must disable the write end of the current process. In this way, our pipeline is truly successful. Let's take a look at the source program written by VC ++:
/*
* Use the MPs queue technology to change the Dir /? To a cedit control of the MFC application.
* VC ++ 6.0 + WINXP
*
* Detrox, 2003.
*/
Void cpipedlg: onbutton1 ()
{
Security_attributes SA;
Handle hread, hwrite;
SA. nlength = sizeof (security_attributes );
SA. lpsecuritydescriptor = NULL;
SA. binherithandle = true;
If (! Createpipe (& hread, & hwrite, & SA, 0 )){
MessageBox ("error on createpipe ()");
Return;
}
Startupinfo Si;
Process_information PI;
Si. cb = sizeof (startupinfo );
Getstartupinfo (& Si );
Si. hstderror = hwrite;
Si. hstdoutput = hwrite;
Si. wshowwindow = sw_hide;
Si. dwflags = startf_useshowwindow | startf_usestdhandles;
If (! CreateProcess (null, "C:/Windows/system32/cmd.exe/c dir /? "
, Null, null, true, null, & Si, & PI )){
MessageBox ("error on CreateProcess ()");
Return;
}
Closehandle (hwrite );
Char buffer [4096] = {0 };
DWORD bytesread;
While (true ){
If (readfile (hread, buffer, 4095, & bytesread, null) = NULL)
Break;
M_edit1 + = buffer;
Updatedata (false );
Sleep (200 );
}
}