Turn: ShellExecute function and ShellExecuteEx function

Source: Internet
Author: User
Tags function prototype naming convention one mail microsoft outlook

ShellExecute function

ShellExecute function prototype and the meaning of the parameters such as the following:

function ShellExecute (Hwnd:hwnd; Operation, FileName, Parameters,directory:pchar; Showcmd:integer): HINST; stdcall;
HWnd: Used to specify the parent form handle. When an error occurs in the function call procedure, it acts as the parent form of the Windows message form. For example, the ability to set it as the application main form handle, Application.handle, can also be set as the desktop form handle (obtained with the GetDesktopWindow function).
Operation: Used to specify the action to be made. The "open" operation means that the program specified by the filename parameter is run, or the file or directory specified by the filename parameter is opened, and the "print" operation means that the file specified by the filename parameter is printed; "Explore" The operation represents browsing the directory specified by the filename parameter. When the number of parameters is set to nil, the default action "open" is run.
FileName: Used to specify the name of the file to open, the name of the program file to run, or the directory name to browse.
Parameters: If the filename parameter is a running program, this parameter specifies the command-line parameter, otherwise the parameter should be nil or pchar (0).
Directory: Lets you specify a default folder.
ShowCmd: If the filename parameter is a running program, this parameter specifies how the program form is initially displayed, otherwise this parameter should be set to 0.
If the ShellExecute function call succeeds, the return value is the instance handle of the program being run. If the return value is less than 32, it indicates an error occurred.

How to use the ShellExecute function:

How do I open a webpage?

Assuming that the filename parameter is set to the "http:" protocol format, the function opens the default browser and links to the specified URL address. If multiple browsers are installed in the user's machine, the function determines which browser to start based on the settings of the HTTP protocol handler (Protocols Handler) in the Windows 9x/nt registry. such as: ShellExecute (handle,l "open", L "http://www.neu.edu.cn", NULL, NULL, SW_SHOWNORMAL); The ShellExecute () function searches under Hkey_classes_root\http\shell\open\command. Default browser in the Register table hkey_classes_root\.htm key under Default settings.

How to activate the relevant program, send email?

Assuming the filename parameter is set to the "mailto:" protocol format, the function launches the default mail client, such as Microsoft Outlook (also including Microsoft Outlook Express) or Netscape Messanger. If more than one mail client is installed in the user's machine, the function determines which mail client to start based on the settings of the Mailto protocol handler in the Windows 9x/nt registry.  mailto: User account @ Mail server address? subject= message subject &body= message body; such as: ShellExecute (handle, L "open", L "mailto:[email protected]? Subject=hello&body=this is a test ", Null,null, SW_SHOWNORMAL); open a new mail form and voluntarily fill in the recipient address, message subject, and message body. If the message body contains multiple lines of text, you must increment the line break escape character%0a between each line of text. The ShellExecute () function searches under Hkey_classes_root\mailto\shell\open\command.

How to open a program?

ShellExecute (null,l "open", L "notepad.exe", L "C:\\mylog.log", NULL, Sw_show);

How do I print a document using a system printer?

ShellExecute (null,l "print", L "C:\\abc.txt", NULL, NULL, SW_HIDE);

How do I use the system lookup function to find the specified file?

ShellExecute (null,l "Find", L "D:\\nish", NULL, NULL, SW_SHOW);

How do I start a program until it finishes?

Shellexecuteinfo Shexecinfo = {0};
shexecinfo.cbsize = sizeof (SHELLEXECUTEINFO);
Shexecinfo.fmask = see_mask_nocloseprocess;
Shexecinfo.hwnd = NULL;
Shexecinfo.lpverb = NULL;
Shexecinfo.lpfile =l "C:\\myprogram.exe";
Shexecinfo.lpparameters = NULL;
Shexecinfo.lpdirectory = NULL;
Shexecinfo.nshow = Sw_show;
Shexecinfo.hinstapp = NULL;
ShellExecuteEx (&shexecinfo);
WaitForSingleObject (Shexecinfo.hprocess,infinite);
Or:
Process_information ProcessInfo;
Startupinfo Startupinfo; This is a [in] parameter
ZeroMemory (&startupinfo, sizeof (STARTUPINFO));
STARTUPINFO.CB = sizeof Startupinfo; Only compulsory field
if (CreateProcess (L "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 (null,l "The process could not be started", null,null);

How do I display the properties of a file or directory?

Shellexecuteinfo Shexecinfo ={0};
shexecinfo.cbsize = sizeof (SHELLEXECUTEINFO);
Shexecinfo.fmask = see_mask_invokeidlist;
Shexecinfo.hwnd = NULL;
Shexecinfo.lpverb = L "Properties";
Shexecinfo.lpfile = L "c:\\"; Can is a file as well
Shexecinfo.lpparameters = NULL;
Shexecinfo.lpdirectory = NULL;
Shexecinfo.nshow = Sw_show;
Shexecinfo.hinstapp = NULL;
ShellExecuteEx (&shexecinfo);

ShellExecuteEx function

This section goes from click to open link

ShellExecute () has a major hurdle that is difficult to use: it cannot return or let you know the handle of the new process. In other words, you cannot export the program and wait for it to terminate before continuing to run. In other words ShellExecute () was damaged by its 16-bit lineage, which only uncovered a subset of the feature subsets of the new and more powerful functions CreateProcess ()-winexec (). However, a new function was introduced in the version number after 4.0: ShellExecuteEx (). It has a typical prototype of a shell function, supports multiple flags, and all of the above features, extending ShellExecute () by providing support for process synchronization and PIDLs.

The ShellExecuteEx () function clearly replaces the ShellExecute (). It declares in shellapi.h: BOOL ShellExecuteEx (Lpshellexecuteinfo lpexecinfo);

Shellexecuteinfo definitions such as the following:

typedef struct _SHELLEXECUTEINFO

{

DWORD cbsize;

ULONG fmask;

HWND hwnd;

LPCTSTR Lpverb;

LPCTSTR Lpfile;

LPCTSTR lpparameters;

LPCTSTR lpdirectory;

int nshow;

HINSTANCE Hinstapp;

Optional Members

LPVOID lpIDList;

LPCSTR Lpclass;

HKEY Hkeyclass;

DWORD Dwhotkey;

HANDLE Hicon;

HANDLE hprocess;

} shellexecuteinfo, far *lpshellexecuteinfo;

Before using this structure, we strongly recommend that you fill it with 0 and set the cbsize to the actual length of the structure, such as the following:

Shellexecuteinfo sei;

ZeroMemory (&sei, sizeof (SHELLEXECUTEINFO));

sei.cbsize = sizeof (SHELLEXECUTEINFO);

As the gaze in the statement says, the members of the structure are divided into two groups. In fact, the first group makes the function of ShellExecuteEx () equivalent to ShellExecute (). The option member group makes the function more powerful, which is the origin of the ' Ex ' suffix. The HWND, Lpverb, Lpfile, Lpparameters, Lpdirectory, and nshow members are equivalent to the parameters of ShellExecute (), which we have seen. The Hinstapp member is an output buffer, which is filled in by the return value of ShellExecute (). Nshow members always represent the style of building a form, even if Lpfile is an application, it only shows how the app should be displayed. Regardless of whether Lpfile is an application or a document file, Nshow must always be assigned a value of Sw_ constant, which you know, assuming that setting to 0 will get the hidden form.

The simplest way to call ShellExecuteEx () is as follows:

Shellexecuteinfo sei;

ZeroMemory (&sei, sizeof (SHELLEXECUTEINFO));

sei.cbsize = sizeof (SHELLEXECUTEINFO);

Sei.lpfile = __text ("explorer.exe");

Sei.nshow = Sw_show;

Sei.lpverb = __text ("open");

ShellExecuteEx (&sei);

One of the members of the corresponding parameter in ShellExecute () is fmask. It can be a combination of one or more of the following values :

Description of the symbol

See_mask_classkey should use Hkeyclass members

See_mask_classname should use Lpclass members

See_mask_connectnetdrv Lpfile will be interpreted as a UNC (Universal naming Convention) format file name

See_mask_doenvsubst Whatever the environment variables in the lpdirectory and Lpfile members will be expanded, for example,%WINDIR% open the Windows directory

See_mask_flag_ddewait assumes that the function initiates a DDE session and waits for it to terminate before returning.

SEE_MASK_FLAG_NO_UI do not display a message box in error cases

See_mask_hotkey should use Dwhotkey members

See_mask_icon should use HICON members

See_mask_idlist forcing function to replace lpfile with lpidlist content

The see_mask_invokeidlist causes the function to use the pidl specified in lpIDList. Assuming this member is NULL, create a lpfile pidl and use this pidl. This flag is overloaded with see_mask_idlist
See_mask_nocloseprocess set hprocess members with process handle. lpIDList members can include a pidl that is used to replace Lpfile. Hprocess returns the new process for the exported hprocess type handle

Additional Features

Optional fields are available for some additional features that exceed ShellExecute (). The 1th and most important point is the ability to use PIDLs to execute applications and open directories. Here's the code to open the ' Printers ' directory:

Lpitemidlist Pidl;

SHGetSpecialFolderLocation (NULL, Csidl_printers, &pidl);

Shellexecuteinfo sei;

ZeroMemory (&sei, sizeof (SHELLEXECUTEINFO));

sei.cbsize = sizeof (SHELLEXECUTEINFO);

Sei.nshow = Sw_show;

Sei.lpidlist = Pidl;

Sei.fmask = see_mask_invokeidlist;

Sei.lpverb = __text ("open");

ShellExecuteEx (&sei);

Assuming the SEE_MASK_DOENVSUBST flag is specified, you can use any environment variable in both Lpfile and lpdirectory. For example, to open a Windows folder, it can be represented as%windir%.

Finally, we have the ability to synchronize the applications exported by ShellExecuteEx (). After you set the see_mask_nocloseprocess bit to the Fmask member, the handle of the new process is returned by the hprocess member, so this line of code: WaitForSingleObject (Sei.hprocess, INFINITE); will cause the calling application to jam and wait for an application to terminate.

Show file Properties dialog box

The SEE_MASK_INVOKEIDLIST flag is an important sign, as this is ShellExecuteEx () there is also a bright spot better than ShellExecute (): It agrees that the function awakens the dynamic verb as if it were a static verb. As explained earlier, dynamic verbs are added by the shell-extended context menu at execution time. It works by assuming that ShellExecuteEx () cannot find the verb in the list of static verbs, it tries to find the context menu for a given file. This search leads to IContextMenu interface pointers. The dynamic verb is then awakened by the method exposed by the interface.

As a result of this operation, we are able to display the file's Properties dialog box very easily-with the right-click File and then select Properties to display the same dialog box. Here is a simple sample function:

void Showfileproperties (LPCTSTR szpathname)

{

Shellexecuteinfo sei;

ZeroMemory (&sei, sizeof (SHELLEXECUTEINFO));

sei.cbsize = sizeof (SHELLEXECUTEINFO);

Sei.lpfile = Szpathname;

Sei.nshow = Sw_show;

Sei.fmask = see_mask_invokeidlist;

Sei.lpverb = __text ("Properties");

ShellExecuteEx (&sei);

}





Go: ShellExecute function and ShellExecuteEx function

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.