A brief analysis of ShellExecute API function usage

Source: Internet
Author: User

In vc/c++ programming, we often encounter open files, Web pages, executable application scenarios, ShellExecute API functions can do this. Now let's see how powerful it is!

ShellExecute function Prototypes:

hinstance ShellExecute (
hwnd hwnd,
LPCTSTR Lpoperation,
LPCTSTR Lpfile,
LPCTSTR Lpparameters,
LPCTSTR Lpdirectory,
INT nShowCmd
);
        ShellExecute Function Parameter description:

hwnd
Used to specify the parent window handle. When an error occurs in the function call procedure, it acts as the parent window of the Windows message window.
Lpoperation:
Used to specify the action to be made.
The "open" operation means that the program specified by the Lpfile parameter is executed, or the file or folder specified by the Lpfile parameter is opened;
The "print" operation means that the file specified by the Lpfile parameter is printed;
The "Explore" operation means browsing the folder specified by the Lpfile parameter.
When the parameter is set to NULL, the default action "open" is executed.
Lpfile:
Used to specify the file name to open, the name of the program file to execute, or the name of the folder to browse. (when you set this parameter with a relative path, you should place the called file in the same directory as the source program, otherwise it will not be an error, but it will not open the file .
Lpparameters:
If the Lpfile parameter is an executable program, this parameter specifies a command-line argument, otherwise this parameter should be null.
Lpdirectory:
Used to specify the default directory.
nShowCmd:
If the Lpfile parameter is an executable program, this parameter specifies how the program window is initially displayed, otherwise this parameter should be set to 0.
constants commonly used for this parameter:
Sw_hide hidden window, active state to make a window
sw_minimize minimized window, active state to make a window
Sw_restore displays a window with its original size and position, while making it active
Sw_show displays a window with the current size and position, and makes it active
Sw_showmaximized to maximize the window and activate it
sw_showminimized Minimize the window and activate it
Sw_showminnoactive minimizes a window without changing the active window
Sw_showna displays a window with the current size and position without changing the active window
Sw_shownoactivate displays a window with the nearest size and position without changing the active window
Sw_shownormal is the same as Sw_restore
If the ShellExecute function call succeeds, the return value is the instance handle of the executing program. If the return value is less than 32, it indicates an error occurred.

How to use the ShellExecute function:
For example:
ShellExecute (NULL, "open", "iloveu.bmp", Null,null,sw_shownormal );     
Use the default bitmap editor to open a calliloveu.bmpbitmap file, this default bitmap editor may beMicrosoft Paint, Adobe Photoshop,orCorel PhotoPaint.
       This function can open any file, even the desktop andURLShortcuts (. Inkor. URL). ShellExecuteparsing the system registration formHKEY_CLASSES_ROOTin all of the content, decide to start that one execution program, and start a new instance or useDDEconnect the file name to an open instance. Then,ShellExecutereturns an instance handle of the app that opened the file.
ShellExecute (NULL, "open", "http://www.microsoft.com", NULL, NULL, SW_SHOWNORMAL);
       This code allows you to access Microsoft's homepage. WhenShellExecutebefore the file name is encountered"http:", you can tell if the file you want to open isWebfile, follow it upInternet ExplorerorNetscape Navigatoror any other browser you use to open the file.
ShellExecuteOther protocols can be identified, likeFTP,GOPHER. Even identify"mailto", if the file name points to"Mailto:[email protected]",It starts the e-mail program and opens a new message to be edited, for example:
ShellExecute ( null, "open", "mailto:[email protected]", NULL, NULL, SW_SHOWNORMAL); Open the New mail window.
      in short,ShellExecuteThe function is so simple to open the disk file andInternetfile. If you add a second parameter"OPEN"instead"PRINT"or"EXPLORE",ShellExecutewill be able to print files and open folders. ShellExecuteThere is also an extension functionShellExecuteEx, there is a special structure in the parameters, more powerful, or any other browser you use to open the file.

Q: How to open a 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 did not pass the full path of the program.
Q: How do I open a document associated with a system program?

ShellExecute (This->m_hwnd, "open", "C:\\abc.txt", "", "", sw_show);
Q: How do I open a webpage?
ShellExecute (This->m_hwnd, "open", "http://www.google.com", "", "", sw_show);
Q: How to activate the relevant program, send email?
ShellExecute (This->m_hwnd, "open", "mailto:[email protected]", "", "", sw_show);
Q: How do i print a document using a system printer?
ShellExecute (This->m_hwnd, "print", "C:\\abc.txt", "", "", sw_hide);
Q: How do I use the System Lookup feature to find the specified file?
ShellExecute (M_hwnd, "find", "D:\\nish", null,null,sw_show);
Q: How do I start a program until it runs to the end?
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 Startupinfo; This is a [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 could not being started ...");
}
Q:How do I display the properties 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 is a file as well
Shexecinfo.lpparameters = "";
Shexecinfo.lpdirectory = NULL;
Shexecinfo.nshow = Sw_show;
Shexecinfo.hinstapp = NULL;
ShellExecuteEx (&shexecinfo);


A brief analysis of ShellExecute API function usage

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.