When a warehouse is opened and food is stored: What are the two problems that Delphi has encountered when executing commands on the command line?

Source: Internet
Author: User
When a warehouse is opened and food is stored: What are the two problems that Delphi has encountered when executing commands on the command line? Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiAPI/html/delphi_20061120203959190.html
1: How to wait until the command is executed and then execute the following statement
2: how to capture the Errors generated by the command and prompt them?

2nd Question values: 150 points

1 waitforsingleobject
2 var A: tstringlist;
begin
A: = tstringlist. create;
try
a.loadfromfile('123.txt '); // This file does not exist
exist T
On E: exception do
showmessage ('error: '+ E. message);
. free
end;

It is feasible to use waitforsingleobject ..

Try

Except
On E: exception do
Showmessage (E. Message)
End;

This method is feasible.

There is another wayProgramTo display the applicationevents control.

Write in its exception eventCode

Procedure tfrmo1.applicationevents1exception (Sender: tobject;
E: exception );

Sender is your object, and exception is an exception.

The first problem can be solved with sleep (1000 ~~~

For more information, see <DelphiSource codeAnalysis>

1. Use waitforsingleobject
Procedure tform1.button1click (Sender: tobject );
VaR
Mutexhandle: thandle;
Begin
Waitforsingleobject (mutexhandle, infinite );
// Do your code
Closehandle (mutexhandle );
End;


2. var
List: tstringlist;
Begin
List: = tstringlist. Create;
Try
// Do something Error
Except
On E: exception do
Showmessage (E. Message );
A. Free
End;
End;

Jf...

Debug for 2nd Problems
If there is something wrong, debug

Score snatching!

Jf ....

Pass.

Jf

1 use waitforsingleobject

2 try
Except
On E: exception

Why don't you see the stars here ~~~
Try

Except

He will not know> 〉?

If the main command refers to the doscommand line, you must use
CreateProcess recommended command process
Then, use the wait function (such as waitforsingleobject) to wait for the handle of the process.

Grain receiving

Grain receiving

Grain receiving

Score

1st questions:
Function winexecandwait32 (
Filename: string; visibility: integer): integer;
VaR
Zappname: array [0 .. 512] of char;
Zcurdir: array [0 .. 255] of char;
Workdir: string;
Startupinfo: tstartupinfo;
Processinfo: tprocessinformation;
Exitcode: DWORD;
Begin
Strpcopy (zappname, filename );
Getdir (0, workdir );
Strpcopy (zcurdir, workdir );
Fillchar (startupinfo, sizeof (startupinfo), #0 );
Startupinfo. CB: = sizeof (startupinfo );
Startupinfo. dwflags: = startf_useshowwindow;
Startupinfo. wshowwindow: = visibility;
If not CreateProcess (nil, zappname,
{Pointer to command line string}
Nil, {pointer to process security attributes}
Nil, {pointer to thread security attributes}
False, {handle inheritance flag}
Create_new_console or {creation flags}
Normal_priority_class,
Nil, {pointer to new environment block}
Nil, {pointer to current directory name}
Startupinfo, {pointer to startupinfo}
Processinfo) then result: =-1
{Pointer to process_inf}
Else begin
Waitforsingleobject (processinfo. hprocess, infinite );
Getexitcodeprocess (processinfo. hprocess, exitcode );
Result: = exitcode;
End;
End;

2nd

Procedure captureconsoleoutput (dosapp: string; amemo: tmemo );
Const
Readbuffer = 1048576; // 1 MB Buffer
VaR
Security: tsecurityattributes;
Readpipe, writepipe: thandle;
Start: tstartupinfo;
Processinfo: tprocessinformation;
Buffer: pchar;
Totalbytesread,
Bytesread: DWORD;
Apprunning, n,
Bytesleftthismessage,
Totalbytesavail: integer;
Begin
With security do
Begin
Nlength: = sizeof (tsecurityattributes );
Binherithandle: = true;
Lpsecuritydescriptor: = nil;
End;

If createpipe (readpipe, writepipe, @ security, 0) then
Begin
// Redirect in-and output through startupinfo Structure

Buffer: = allocmem (readbuffer + 1 );
Fillchar (START, sizeof (start), #0 );
Start. CB: = sizeof (start );
Start. hstdoutput: = writepipe;
Start. hstdinput: = readpipe;
Start. dwflags: = startf_usestdhandles + startf_useshowwindow;
Start. wshowwindow: = sw_hide;

// Create a console child process with redirected Input and Output

If CreateProcess (nil, pchar (dosapp ),
@ Security, @ security,
True, create_no_window or normal_priority_class,
Nil, nil,
Start, processinfo) then
Begin
N: = 0;
Totalbytesread: = 0;
Repeat
// Increase counter to prevent an endless loop if the process is dead
INC (n, 1 );

// Wait for end of child process
Apprunning: = waitforsingleobject (processinfo. hprocess, 100 );
Application. processmessages;

// It is important to read from time to time the output information
// So that the pipe is not blocked by an overflow. New information
// Can be written from the console app to the pipe only if there is
// Enough buffer space.

If not peeknamedpipe (readpipe, @ buffer [totalbytesread],
Readbuffer, @ bytesread,
@ Totalbytesavail, @ bytesleftthismessage) Then break
Else if bytesread> 0 then
Readfile (readpipe, buffer [totalbytesread], bytesread, bytesread, nil );
Totalbytesread: = totalbytesread + bytesread;
Until (apprunning <> wait_timeout) or (n> 150 );

Buffer [totalbytesread]: = #0;
Oemtochar (buffer, buffer );
Amemo. Text: = amemo. Text + strpas (buffer );
End;
Freemem (buffer );
Closehandle (processinfo. hprocess );
Closehandle (processinfo. hthread );
Closehandle (readpipe );
Closehandle (writepipe );
End;
End;


let me answer the second question. The above answers all exceptions from your own program. In reality, when an exception occurs when you call an external program, the general program will output the error to the standard error (stderr, you just need to read his stderr or stdout (standard output.
Read method:
1. use a named pipe as shown in the preceding figure
2. there is a control, and the name does not remember ....
3. add the command line parameters to save the standard output and error output to the file ....
mcommand: = "test.exe> a.txt 2> B .txt";
winexec (mcommand);
after execution, the standard output is written to a.txt, and B .txt is written to the output by mistake.
it may not help you, but it just provides an idea.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.