Go Delphi Execution cmd Command

Source: Internet
Author: User

Today I see someone asking the code to execute the cmd command, just summarize the usage and make a memo.

In Delphi, executing a command or running a program has 2 functions, one is winexec, and the other is ShellExecute. These two people should have seen, among them, WinExec is relatively simple, you can directly run an external program, ShellExecute is more advanced, in addition to running the external EXE, you can also execute special commands.

Here are some examples: Let's take a look at running an EXE program, take Notepad as an example:

WinExec (PChar (' Notepad. exe '), sw_normal);//Normal mode Open

WinExec (PChar (' Notepad. exe '), sw_hide);//Hidden mode open

WinExec (PChar (' notepad.exe '), sw_showmaximized);//Maximize open, the software must support maximum run, otherwise the parameter is invalid, run in normal mode

WinExec (PChar (' notepad.exe '), sw_showminimized);//Minimized mode open

ShellExecute (Handle,nil, ' notepad.exe ', nil,nil,sw_normal);//This is opened in a shellexecute manner, note the last parameter, as above

Use ShellExecute note need uses SHELLAPI, the above Notepad, because is in the system directory, so you can directly write Notepad.exe file name, without having to write the path, if it is other non-environment variables registered EXE, you need to take an absolute path or relative path

OK, the above is the simplest, let us say that with the parameters of the operation, you know, some EXE program can be attached to the operation of the parameters, here to cmd for example explanation

For example, I want to run the ping command to do ping 192.168.1.1

WinExec (PChar (' cmd.exe/c ping 192.168.1.1 '), SW_SHOWNORMAL);

ShellExecute (Handle,nil, ' cmd.exe ', Pchar ('/C ping 192.168.1.1 '), nil,sw_normal);

Note that the cmd command needs to be run with the/C representation with parameters, followed by the specific command, so that the ping command can be executed.

The above command can be successfully run, but there is a disadvantage, that is, CMD in the completion of the execution will automatically close the window, if sometimes the command executes too fast, we can not see the results, then, there is any way to let the cmd after execution does not close the window? The answer is yes, we need an extra command to pause

WinExec (PChar (' cmd.exe/c ping 192.168.1.1 & Pause '), SW_SHOWNORMAL);

We pay attention to the red part, we use a & symbol and Pause,pause is the pause command in the batch,& symbol means that you can execute multiple commands, the above example shows that after the ping command to execute the pause command, so that the window will not be closed. ShellExecute also supports this approach.

Below, let's look at some other methods of ShellExecute:

Open Web page: This is too easy, everyone should know

ShellExecute (handle, ' open ', PChar (' http://www.baidu.com '), nil, nil, SW_SHOWNORMAL);//Open with default browser baidu.com

ShellExecute (handle, ' open ', ' Firefox.exe ', PChar (' http://www.baidu.com '), nil, SW_SHOWNORMAL);//Use Firefox browser to open baidu.com

Of course, ShellExecute also supports associated commands that have been registered in the system. Like sending an e-mail

ShellExecute (handle, ' open ', PChar (' mailto:[email protected] '), Nil,nil, SW_SHOWNORMAL);

Execute this sentence to open the default mail client to [email protected] email, if we want to bring the subject and content is also very good:

ShellExecute (handle, ' open ', PChar (' Mailto:[email protected]?subject= This is the message subject &[email protected]&body= message body '), Nil,nil, SW_SHOWNORMAL);

The above can automatically fill in the subject, content and CC, note that the Chinese need to encode, otherwise it may appear garbled.

Go Delphi Execution cmd Command

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.