A powerful API function ShellExecute forwarded from the VC Knowledge Base

Source: Internet
Author: User
Tags gopher knowledge base

The question is raised:

I use MFC to write an app that asks for a logo, company name, address, and URL link to the company in the About dialog box. After the user clicks on the URL link, the browser is launched to access the Web page, and the hot links in this dialog box add a lot to the application. The way I figured it out was to get the name of the default browser from the system registry by looking up the. htm or. html file's associated program, as if there was a lot of work to do. In addition, if the browser is already open, a new instance is always started to replace the one that is already present. Want to find an easy way, but I checked the relevant information, no harvest, please expert advice.

Workaround:

If there is such an API function that much better: Openthishereurlformenowplease (), the problem is not solved!! I tell you, yes, but it's not the function you think it is. So far, there is no mention of a useful function in the documentation that can open any file on the desktop or open the Internet URL. The only reference I can find is at Microsoft? In Access Knowledgebase, this function has been mentioned very vaguely.

This incredible function is: ShellExecute, instead of winexec. You can give it any file name, it can be recognized and open it. For example:

View Source print? 1. 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.

For detailed usage of ShellExecute refer to MSDN for yourself, and it is important that you know that this function can open any file, even 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.

What's even more amazing is that ShellExecute not only can open files on the machine, but also open the web address of the Internet. For example:

View Source print? 1. ShellExecute(NULL, "open" , "http://www.microsoft.com" 2.               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. 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 parameters that have a special structure, more powerful, and its usage see the documentation "> 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 "> 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.

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, more powerful, and its usage is described in the documentation.

Now that you know the secret of ShellExecute, it's easy to add a link to your about dialog box with it. Isn't it. :)

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.