How to Use ShellExecute in VC

Source: Internet
Author: User
Tags nslookup virtual environment eventvwr microsoft outlook

There are three API functions that can run the executable files winexec, ShellExecute, and CreateProcess. Because of its complexity, CreateProcess is rarely used.

Winexec mainly runs the EXE file. For example, winexec('notepad.exe readme.txt ', sw_show );
ShellExecute can run not only EXE files, but also associated files.
First, you must reference the shellapi. Pas unit: Uses shellapi;

1. Standard usage
The meanings of the parameters and prototype of the ShellExecute Function are as follows:
Function ShellExecute (hwnd: hwnd; operation, filename, parameters, directory: pchar; showcmd: integer): hinst; stdcall;
● Hwnd: Specifies the parent window handle. When an error occurs during a function call, it is used as the parent window of the Windows message window. For example, you can set it to the application Main Window handle, that is, application. Handle, or desktop window handle (obtained using the getdesktopwindow function ).
● Operation: Specifies the operation to be performed. The "open" operation indicates that the program specified by the filename parameter is executed, or the file or folder specified by the filename parameter is opened. The "print" operation indicates that the file specified by the filename parameter is printed; the "Browse e" operation indicates browsing the folder specified by the filename parameter. If the parameter is set to nil, the default operation "open" is executed ".
● Filename: used to specify the name of the file to be opened, the name of the program file to be executed, or the name of the folder to be browsed.
● Parameters: If the filename parameter is an executable program, this parameter specifies the command line parameter; otherwise, this parameter should be nil or pchar (0 ).
● Directory: used to specify the default directory.
● Showcmd: If the filename parameter is an executable program, this parameter specifies the initial display mode of the program window. Otherwise, this parameter should be set to 0.
If the ShellExecute Function is successfully called, the returned value is the instance handle of the executed program. If the returned value is smaller than 32, an error occurs.
The above is only the standard usage of the ShellExecute Function. The following describes its special usage.

2. Special usage
If you set the filename parameter to the "http:" protocol format, this function opens the default browser and links to the specified URL address. If multiple browsers are installed on your machine, the function determines which browser to start based on the settings of the HTTP handler (Protocols handler) in Windows 9x/NT Registry.
Format 1: http: // website domain name.
Example: ShellExecute (handle, 'open', http: //; www.neu.edu.cn ', nil, nil, sw_shownormal );
Format 2: http: // website domain name/Webpage file name.
Example: ShellExecute (handle, 'open', http: //; www.neu.edu.cn/default.htm', nil,nil, sw_shownormal );
If you set the filename parameter to the "mailto:" protocol format, this function starts the default mail client program, such as Microsoft Outlook (including Microsoft Outlook Express) or Netscape Messanger. If multiple email client programs are installed on your machine, this function determines which email client program to start based on the settings of the mailto protocol handler in the Windows 9x/NT Registry.
Format 1: mailto:
For example, ShellExecute (handle, 'open', 'mailto: ', nil, nil, sw_shownormal); opens the new mail window.
Format 2: mailto: User Account @ email server address
Example: ShellExecute (handle, 'open', 'mailto: who@mail.neu.edu.cn ', nil, nil, sw_shownormal); open the new mail window and automatically fill in the recipient address. If multiple recipient addresses are specified, the recipient addresses must be separated by semicolons or commas (,), for example, ShellExecute (this-> m_hwnd, "open ",
"Mailto: nishinapp@yahoo.com", "", "", sw_show); this can activate Outlook Express.
Format 3: mailto: User Account @ email server address? Subject = Email Subject & Body = Email body
Example: ShellExecute (handle, 'open', 'mailto: who@mail.neu.edu.cn? Subject = Hello & Body = This is a test ', nil, nil, sw_shownormal); opens the new mail window and automatically fills in the recipient address, mail subject, and mail body. If the mail body contains multiple lines of text, you must add the line feed escape character % 0a between each line of text.
Example (Delphi ):
Call C: project1.exe in an application;
ShellExecute (handle, 'open', 'c: project1.exe ', 'string content', nil, sw_shownormal );
In project1.exe, you can call:
Procedure tform1.formcreate (Sender: tobject );
VaR I: integer;
Begin
For I: = 1 to paramcount do
If paramstr (I) <> ''then showmessage (paramstr (I ));
End;

The last parameter specifies the visibility command for the window.
Use any of the following Constants
Sw_hide hides the window, and the activity status gives a window
Sw_minimize minimizes the window, and the active status gives a window
Sw_restore displays a window with the original size and position, and enables it to enter the active status.
Sw_show displays a window with the current size and position, and enables it to enter the active status.
Sw_showmaximized: Maximize the window and activate it.
Sw_showminimized minimizes the window and activates it.
Sw_showminnoactive minimizes a window without changing the activity window.
Sw_showna displays a window with the current size and position without changing the activity window.
Sw_shownoactivate displays a window with the latest size and position without changing the activity window.
Sw_shownormal is the same as sw_restore.

Go to ShellExecute
Translator: Xu Jingzhou (Original: nishant S)

Q: How to open an application? ShellExecute (this-> m_hwnd, "open", "calc.exe", "", "", sw_show );
Or ShellExecute (this-> m_hwnd, "open", "notepad.exe ",
"C: \ mylog. log", "", sw_show );
As you can see, I have not passed the complete path of the program.
Q: How do I open a file related to the same system program? ShellExecute (this-> m_hwnd, "open ",
"C: \ abc.txt", "", "", sw_show );
Q: How to open a webpage? ShellExecute (this-> m_hwnd, "open ",
"Http://www.google.com", "", "", sw_show );
Q: How to activate related programs and send emails? ShellExecute (this-> m_hwnd, "open ",
"Mailto: nishinapp@yahoo.com", "", "", sw_show );
Q: How do I print documents with a system printer? ShellExecute (this-> m_hwnd, "print ",
"C: \ abc.txt", "", "", sw_hide );
Q: How can I use the system search function to find a specified file? ShellExecute (m_hwnd, "find", "d: \ Nish ",
Null, null, sw_show );
Q: How do I start a program until it finishes running? Shellexecuteinfo shexecinfo = {0 };
Shexecinfo. cbsize = sizeof (shellexecuteinfo );
Shexecinfo. fmask = see_mask_nocloseprocess;
Shexecinfo. hwnd = NULL;
Shexecinfo. lpverb = NULL;
Shexecinfo. lpfile = "C: \ myprogram.exe ";
Shexecinfo. lpparameters = "";
Shexecinfo. lpdirectory = NULL;
Shexecinfo. nshow = sw_show;
Shexecinfo. hinstapp = NULL;
Shellexecuteex (& shexecinfo );
Waitforsingleobject (shexecinfo. hprocess, infinite );
Or: process_information processinfo;
Startupinfo; // This is an [in] Parameter
Zeromemory (& startupinfo, sizeof (startupinfo ));
Startupinfo. cb = sizeof startupinfo; // only compulsory field
If (CreateProcess ("C: \ winnt \ notepad.exe", null,
Null, null, false, 0, null,
Null, & startupinfo, & processinfo ))
{
Waitforsingleobject (processinfo. hprocess, infinite );
Closehandle (processinfo. hthread );
Closehandle (processinfo. hprocess );
}
Else
{
MessageBox ("the process cocould not be started ...");
}

Q: How do I display attributes of a file or folder? Shellexecuteinfo shexecinfo = {0 };
Shexecinfo. cbsize = sizeof (shellexecuteinfo );
Shexecinfo. fmask = see_mask_invokeidlist;
Shexecinfo. hwnd = NULL;
Shexecinfo. lpverb = "properties ";
Shexecinfo. lpfile = "C: \"; // can be a file as well
Shexecinfo. lpparameters = "";
Shexecinfo. lpdirectory = NULL;
Shexecinfo. nshow = sw_show;
Shexecinfo. hinstapp = NULL;
Shellexecuteex (& shexecinfo );

Appendix: some windows system commands:

Winver --------- check the Windows version
Wmimgmt. msc ---- open windows management architecture (Wmi)
Wupdmgr -------- Windows Update Program
Wscript -------- Windows Script Host settings
Write ---------- WordPad
Winmsd --------- system information
Wiaacmgr ------- scanner and camera wizard
Winchat -------- XP built-in LAN chat

Mem.exe -------- Display memory usage
Msconfig.exe --- System Configuration Utility
Mplayer2 ------- simple Widnows Media Player
Mspaint -------- graphic Board
Mstsc ---------- Remote Desktop Connection
Mplayer2 ------- Media Player
Magnify -------- magnifier Utility
MMC ------------ open the Console
Mobsync -------- synchronization command

Dxdiag --------- check DirectX Information
Drwtsn32 ------ system doctor
Devmgmt. msc --- Device Manager
Dfrg. msc ------- disk fragment Program
Diskmgmt. msc --- disk Management Utility
Dcomcnfg ------- open the system component service
Ddeshare ------- enable DDE sharing settings
Dvdplay -------- DVD player

Net stop messenger ----- stop the Messenger Service
Net start messenger ---- start the Messenger Service
Notepad -------- open notepad
NSLookup ------- network management tool wizard
Ntbackup ------- system backup and Restoration
NARRATOR ------- "narrator" on the screen"
Ntmsmgr. msc ---- mobile Storage Manager
Ntmsoprq. msc --- mobile storage administrator * request
Netstat-An ---- (TC) command Check interface

Syncapp -------- create a briefcase
Sysedit -------- System Configuration Editor
Sigverif ------- file signature verification program
Sndrec32 ------- Recorder
Shrpubw -------- create a shared folder
Secpol. msc ----- Local Security Policy
Syskey --------- system encryption. Once encrypted, it cannot be unlocked. This protects Windows XP dual passwords.
Services. msc --- local service settings
Sndvol32 ------- Volume Control Program
Sfc.exe -------- System File Checker
SFC/scannow --- Windows File Protection

Tsshutdn ------- 60 seconds countdown shutdown command
Tourstart ------ XP introduction (roaming XP program displayed after installation)
Taskmgr -------- Task Manager

Eventvwr ------- Event Viewer
Eudcedit ------- Word Creation Program
Explorer ------- open the Resource Manager

Packager ------- object package
Perfmon. msc ---- computer performance monitoring program
Progman -------- Program Manager

Regedit.exe ---- Registry
Rsop. msc ------- group policy result set
Regedt32 ------- Registry Editor
Rononce-P ---- shutdown in 15 seconds
Regsvr32/u *. dll ---- stop DLL file running
Regsvr32/u zipfldr. dll ------ cancel zip support

Cmd.exe -------- cmd Command Prompt
Chkdsk.exe ----- chkdsk disk check
Certmgr. msc ---- Certificate Management Utility
Calc ----------- start Calculator
Charmap -------- start character ing table
Cliconfg ------- SQL Server Client Network Utility
Clipbrd -------- clipboard Viewer
Conf ----------- Start netmeeting
Compmgmt. msc --- Computer Management
Cleanmgr ------- organize good things
Ciadv. msc ------ Indexing Service Program

Osk ------------ enable the Screen Keyboard
Odbcad32 ------- ODBC data source Manager
Oobe/msobe/A ---- check whether XP is activated
Lusrmgr. msc ---- local users and groups
Logoff --------- logout command

Iexpress ------- Trojan bundle tool, which comes with the System

NSLookup ------- IP address Detector

Fsmgmt. msc ----- Shared Folder Manager

Utilman -------- auxiliary tool Manager

Gpedit. msc ----- Group Policy
Command list in XP.

$ Systemroot $ documents and settingsusername under the Directory

Appwiz. Cpl ------------ add and delete a program

Control userpasswords2 -------- user account settings

Cleanmgr ------- garbage collection

CMD -------------- a command prompt can be used as an attachment for Windows. Ping, convert, and other functions that cannot be used in the graphic environment must be used.

CMD ------ view the Java Virtual Machine version in JView.

Command.com ------ the call is the built-in NTVDM of the system, a DOS virtual machine. It is a virtual environment similar to Virtual PC and has little connection with the system itself. When we run the DOS program at the command prompt, it is actually automatically transferred to the NTVDM virtual machine. It has nothing to do with cmd itself.

Calc ----------- start Calculator

Chkdsk.exe ----- chkdsk disk check

Compmgmt. msc --- Computer Management

Conf ----------- Start netmeeting

Control userpasswords2 ----- user account permission settings

Devmgmt. msc --- Device Manager

Diskmgmt. msc --- disk Management Utility

Dfrg. msc ------- disk fragment Program

Drwtsn32 ------ system doctor

Dvdplay -------- start Media Player

Dxdiag ----------- DirectX diagnostic tool

Gpedit. msc ------- Group Policy Editor

Gpupdate/Target: computer/Force refresh Group Policy

Eventvwr.exe ----- Event Viewer

Explorer ------- open the Resource Manager

Logoff --------- logout command

Lusrmgr. msc ---- local users and groups

Msinfo32 --------- system information

Msconfig --------- System Configuration Utility

Net start (servicename) ---- start the service

Net stop (servicename) ----- stop the service

Notepad -------- open notepad

Nusrmgr. Cpl ------- same as control userpasswords, open the User Account Control Panel

NSLookup ------- IP address Detector

Oobe/msobe/A ---- check whether XP is activated

Perfmon. msc ---- computer performance monitoring program

Progman -------- Program Manager

Regedit ---------- Registry Editor

Regedt32 ------- Registry Editor

Regsvr32/u *. dll ---- stop DLL file running

Route print ------ view route table

Rononce-P ---- shutdown in 15 seconds

Rsop. msc ------- group policy result set

Rundll32.exe rundll32.exe % SystemRoot % system32shimgvw. dll, imageview_fullscreen ---- start a blank Windows Image and fax Viewer

Secpol. msc -------- Local Security Policy

Services. msc --- local service settings

SFC/scannow ----- start the System File Checker

Sndrec32 ------- Recorder

Taskmgr ----- Task Manager (applicable to 2000/XP/2003)

Tsshutdn ------- 60 seconds countdown shutdown command

Winchat -------- XP built-in LAN chat

Winmsd --------- system information

Winver ----- display the about Windows window

Windows XP is controlled by the shutdown.exe program and is located in the windows \ system32 folder. If you want the same effect for Windows, copy shutdown.exe to the system directory.

For example, to shut down your computer at, you can select start> Run and enter "at shutdown-s, at, the "system shutdown" dialog box appears. By default, there is a 30-second countdown and you are prompted to save your work. If you want to shut down the server by a time-based method, you can enter “shutdown.exe-s-t 3600 ", which indicates that the server will automatically shut down after 60 minutes, and" 3600 "indicates 60 minutes.

If you want to cancel Automatic shutdown, enter "shutdown-a" during running ". In addition, enter "shutdown-I" to open the Set automatic shutdown dialog box and set the automatic shutdown.

Each shutdown.exe parameter has a specific purpose, and each execution will have different effects. For example, "-s" indicates that the local computer is disabled, and "-a" indicates that the shutdown operation is canceled, more parameters are listed below, which can be used as needed in shutdown.exe.

-F: forcibly close the application

-M \ computer name: controls the remote computer

-I: displays the graphic user interface, but must be the first option of shutdown.

-L: log out of the current user.

-R: Shut down and restart

-T time: Set shutdown countdown

-C "message content": Enter the message content in the shutdown dialog box (cannot exceed 127 characters)

Sometimes, we need to shut down the computer at regular intervals. Next we will introduce a simple method to implement Timed Shutdown in Windows XP.

Specify that the system will automatically shut down after 22 minutes: click "Start> Run", and enter the command "shutdown-s-t 1320" in "open" (Note: Do not enter the quotation marks, there is a space between parameters, 1320 in seconds), click "OK"

Button.

Specify that the system is automatically disabled at a certain time (such as): Enter the command in "open"
"At shutdown-s.

Cancel Timed Shutdown: Enter the command "shutdown-a" in "open.

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.