ShellExecute usage of various

Source: Internet
Author: User



Q: How do I open an app ?
ShellExecute (This->m_hwnd, "open", "calc.exe", "", "", Sw_show); 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.*******.com"," "," ", sw_show);

Q:How to activate the relevant program, send email?
ShellExecute (This->m_hwnd, "open", "mailto:[email protected]", "", "", sw_show);

Q:How to print a document with a system printer?
ShellExecute (This->m_hwnd, "print", "C:\\abc.txt", "", "", sw_hide);

Q:How to find the specified file using the System lookup feature?
ShellExecute (M_hwnd, "find", "D:\\nish", null,null,sw_show);

Q:How to 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);

multiple uses of ShellExecuteFriday, January 11, 2008 10:43

The function of ShellExecute is to run an external program (either open a registered file, open a directory, print a file, etc.) and have some control over the external program.

There are several API functions that can implement these functions, but in most cases shellexecute is more used, and it is not too complex. Here is an example of how to use it.

Start a new application
ShellExecute (Handle, ' Open ', PChar (' C:\test\app.exe '), nil, nil, sw_show);

Open Notepad and open a file (the system recognizes the path to the Notepad application, so we don't have to use absolute paths)
ShellExecute (Handle, ' Open ', PChar (' Notepad '), PChar (' C:\test\readme.txt '), nil, sw_show);

Print a document
ShellExecute (Handle, ' print ', PChar (' C:\test\test.doc '), nil, nil, sw_show);

Note: You may see word temporarily open, but it will automatically close.

Open an HTML page
ShellExecute (Handle, "open", PChar (Http://hi.baidu.com/keepzeal
ShellExecute (Handle, ' open ', PChar (' C:\test\readme.txt '), nil, nil, sw_show),  

Open a directory with Windows Explorer  
ShellExecute (Handle, ' Explore ', PChar (' C:\Windows) ' , nil, nil, sw_show);  

run a DOS command and return immediately to the  
ShellExecute (Handle, ' Open ', PChar (' Command.com '), PChar ('/C copy file1.txt File2.txt '), Nil, sw_show);  

run a DOS command and keep the DOS window present  
ShellExecute (Handle, ' Open ', PChar (' Command.com '), PChar ('/k dir '), Nil, sw_show);  < BR style= "line-height:23px" >

1. Function function:

You can give it any file name, it can be recognized and open it.

2. Function Prototypes:

HInstance ShellExecute (
HWND hwnd,
LPCTSTR Lpoperation,
LPCTSTR Lpfile,
LPCTSTR Lpparameters,
LPCTSTR Lpdirectory,
INT nShowCmd
);
3. 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.

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.

4. How to use:

For example:

ShellExecute (NULL, "open", "iloveu.bmp", null,null,sw_shownormal);

Open a bitmap file called Iloveu.bmp with the default bitmap editor, which may be Microsoft Paint, Adobe Photoshop, or Corel photopaint.

This function can open any file, even the desktop and URL shortcuts (. ink or. url). ShellExecute parses all the contents of the system registry HKEY_CLASSES_ROOT, determines which one executes the program, launches a new instance, or uses DDE to connect the file name to an open instance. Then, ShellExecute returns 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. When ShellExecute encounters the "http:" in front of the file name, you can tell that the file you want to open is a Web file that starts with Internet Explorer or Netscape Navigator or any other browser you use to open the file.

ShellExecute can also identify other protocols, such as FTP, 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 summary, the ShellExecute function simply opens the disk file and the Internet file. If you change the second parameter "open" to "print" or "EXPLORE", ShellExecute will be able to print the file and open the folder. ShellExecute also has an extension function ShellExecuteEx, with a special structure in the parameters, a stronger function, or any other browser you use to open the file.

Use of ShellExecute::

To open a Web page:
ShellExecute (NULL, "open", "http://www.onsky.com.cn", null,null,sw_shownormal);

To open the Help file:

Char Szpath[max_path];
memset (szpath, 0, sizeof (szpath));
GetModuleFileName (NULL, szpath, MAX_PATH);
Char *ppos = STRRCHR (szpath, ' \ \ ');
Szpath[ppos-&szpath[0]] = ' + ';

CString strFileName;
Strfilename.format ("%s\\nsmanager.chm", szpath);
ShellExecute (NULL, "open", Strfilename,null,null, SW_SHOWNORMAL);

Open the application:

Char Szpath[max_path];
memset (szpath, 0, sizeof (szpath));
GetModuleFileName (NULL, szpath, MAX_PATH);
Char *ppos = STRRCHR (szpath, ' \ \ ');
Szpath[ppos-&szpath[0]] = ' + ';

CString strFileName;
Strfilename.format ("%s\\update.exe", szpath);
ShellExecute (NULL, "open", Strfilename,null, NULL, SW_SHOWNORMAL);

Application with reference:

Char Szpath[max_path];
memset (szpath, 0, sizeof (szpath));
GetModuleFileName (NULL, szpath, MAX_PATH);
Char *ppos = STRRCHR (szpath, ' \ \ ');
Szpath[ppos-&szpath[0]] = ' + ';

CString strFileName;
Strfilename.format ("%s\\update.exe", szpath);
ShellExecute (NULL, "open",NULL,strFileName, "parameters", SW_SHOWNORMAL);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ShellExecute usage of various

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.