Application process implements call to other applications

xiaoxiao2021-03-06  36

Among the procedures we have designed, it can be used to use its application to finish a certain function. For example, when we compress and decompress many files in order to facilitate data transmission, one method is our own design of such compression / decompression programs, and then in the form of dynamic link library (DLL) or function library Application is called. But more and high-efficiency work is to use the existing Side of Existing Software, such as Arj.exe, and call it in the form of a process, and close it when it is appropriate. The next step will be described as an example, and the latter method is implemented.

In the case where you need to call Arj.exe to compress the shrink / decompression, create a member function, you don't want to createbat (), which is generated into a batch process. Arj.exe is called by this batch process and gives a specific compression / decompression parameter. After that, use the MS-DOS DIR command to generate a temporary file to be used as a sign of compression / decompression shrinkage.

Void CMycompress :: Createbat (CString Batpath, Cstring Arjpath,

CString Batname, CString ArjfileName,

CSTRING TEMPPATH, CSTRING EXITFLAG, BOOL OUT

{

LPTSTR LPBUFFER;

Uint usize;

Handle hheap;

USIZE = (GetCurrentDirectory (0, NULL)) * Sizeof (tchar);

HHEAP = getProcessheap ();

LPBuffer = (lpstr) Heapalloc (hheap, heap_zero_memory, usize);

GetCurrentDirectory (USIZE, LPBUFFER);

// I know the current directory information in order to change the directory as needed.

IF (lpbuffer! = batpath) // Diferent Dir

SetCurrentDirectory (BATPATH);

Cstdiofile f;

CfileException E;

IF (! f.open (Batname, CFile :: Modecreate | CFILE :: Modewrite, & E))

/ / Create a batch file with BatName

{

AfxMessageBox ("You can't create files" batname);

Return;

}

Char Density [6];

Sprintf (Density, "% D", mtotalbytes;

---- // MtotalBytes is a variable set by other functions, used to record the maximum available space for the disk for copying or copying the file.

CSTRING DENSITY = DENSITY

CString string;

If (out) // Description is a batch file that generates compression work

String = "arj a -v" denity;

ELSE // Description is a batch file that generates the decompression work

String = "arj e -v" density;

String = "..//"4arjpath "//"4arjfilename "; if (out)

String = string "..//" TEMPPATH "//*.* -y -jm / n";

Else

String = string "..//" TEMPPATH "// -y -jm / n";

f.writeString (String);

String = "DIR>" EXITFLAG "/ N";

f.writeString (String);

f.close ();

SetCurrentDirectory (lpbuffer); // Reply to the original directory

}

---- After the function is executed, a batch file will be generated, the content is roughly: ---- ARJ A -V1440 Compressed file path name file name is compressed path name file name -y -jm ---- Dir> Temporary file name ---- or: ---- ARJ E -V1440 The path name of the decompressed file file name decomposers the path name file name - Y-JM - - DIR> Temporary file name

In a class that needs to call Arj.exe for compression / decompression, create a member function, it may be called Runbat (), which is a batch file that creates and executes the process to run the batch file generated above, and undo at the appropriate time process.

Void CMYCompress :: runbat (cstring

BATPATH, CSTRING FILENAME, CSTRING EXITFLAG

{

Cstring lpapplicationname = batPath "//" filename;

// The full path name of the application program is executed

Startupinfo startupinfo; // Creating the information required for the process

GetStartupInfo (& Startupinfo);

STARTUPINFO.LPRESERVED = NULL;

STARTUPINFO.LPDESKTOP = NULL;

Startupinfo.lptitle = NULL;

STARTUPINFO.DWX = 0;

STARTUPINFO.DWY = 0;

Startupinfo.dwxsize = 200;

STARTUPINFO.DWYSIZE = 300;

STARTUPINFO.DWXCOUNTCHARS = 500;

STARTUPINFO.DWYCOUNTCHARS = 500;

STARTUPINFO.DWFLAGS = startf_useshowwindow;

Startupinfo.wshowwindow = sw_hide;

// The process will be implemented in the background with a hidden approach

Startupinfo.cbreserved2 = 0;

STARTUPINFO.LPRESERVED2 = NULL;

STARTUPINFO.HSTDINPUT = stdin;

STARTUPINFO.HSTDOUTPUT = STDOUT;

Startupinfo.hstderr = stderr; lptstr lpbuffer;

Uint usize;

Handle hheap;

USIZE = (GetCurrentDirectory (0, NULL)) * Sizeof (tchar);

HHEAP = getProcessheap ();

LPBuffer = (lpstr) Heapalloc (hheap, heap_zero_memory, usize);

GetCurrentDirectory (USIZE, LPBUFFER);

// Receive the current directory information, so that you need to change your directimate

IF (lpbuffer! = batpath) // Diferent Dir

SetCurrentDirectory (BATPATH);

// Creating a process

IF (CreateProcess (LPApplicationName, Null, NULL,

NULL, FALSE, CREATE_DEFAULT_ERROR_MODE,

NULL, NULL, & Startupinfo, & Pro_INFO))

{

MSG Message;

Deletefile (EXITFLAG);

Settimer (1,100, null); // Setting timer

Search = True;

WHILE (SEARCH) {

IF (: PeekMessage (& Message, NULL, 0, 0, PM_REMOVE)) {

:: TranslateMessage (& Message);

:: DispatchMessage (& Message);

}

}

// Processing work before the end of the process

DWORDEXITCODE;

IF (! getExitcodeProcess (Pro_info.hprocess, & exitcode))

AfxMessageBox ("GetExitcodeProcess IS Failed!");

if (! TerminateProcess (Pro_info.hprocess, (uint) EXITCODE))

// Final process

AFXMESSAGEBOX ("TerminateProcess IS Failed!");

IF (! CLOSEHANDLE (Pro_info.hprocess))

/ / Release the handle of the ended process

AfxMessageBox ("Closehandle Is Failed!");

KillTimer (1); // Removal

}

Else AfxMessageBox ("Process is not create!";

SetCurrentDirectory (lpbuffer); // Reupfaction to the original directory

}

Press the Ctrl and W key or directly click the ClassWizard button on the toolbar to open the ClassWizard dialog box. In the Class Name list box, select the class that you want to call Arj.exe for compression / decompression / decompression. After selecting the class name of the class in the Object IDS list box, select the WM_TIMER message in the message (Messages) list box and Double-click it, then ClassWizard adds an ONTIMER () function in this class. The function will check the compression / decompression zone in a set time interval whether the compression / decompression zoom has been completed, ie the time to check if the document is displayed, and then modified "Search", so that the Runbat () function end is notified. Void CMYCompress :: ONTIMER (Uint Nidevent)

{

// Todo: add your message handler code here and / or call default

CFILE FILE;

CfileException Error;

IF (file.open (exitflag, cfile :: ModeRead, & error) {

Search = false;

File.Close ();

}

}

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

New Post(0)