Understanding ShellExecute function and it’s application to open a list of URLs present in a file using C++ code
Given a URL as a string open it using a C++ code in Microsoft Windows OS.
Input : https://www.geeksforgeeks.org/ Output : The site opened in your browser.
GeeksforGeeks site opened in Google Chrome in a new tab.
ShellExecute is the code equivalent of a user double clicking a file icon. It causes Windows to work out what application the document file is associated with, launch the program and have it load the document file.
By using ShellExecute, you don’t need to know the name or location of the program that’s registered to a particular file type. Windows takes care of that for you. For example, you can ShellExecute a .PDF file and, so long as Reader, Acrobat or some other PDF-reading app is installed, Windows will launch it and load the PDF for you.
Parameters of ShellExecute and their meaning:
HINSTANCE ShellExecute (
_In_opt_ HWND hwnd,
_In_opt_ LPCTSTR lpOperation,
_In_ LPCTSTR lpFile,
_In_opt_ LPCTSTR lpParameters,
_In_opt_ LPCTSTR lpDirectory,
_In_ INT nShowCmd
A handle to the parent window used for displaying a UI or error messages. This value can be NULL if the operation is not associated with a window.
A pointer to a null-terminated string, referred to in this case as a verb, that specifies the action to be performed. The set of available verbs depends on the particular file or folder. Generally, the actions available from an object’s shortcut menu are available verbs. In our case ‘open’ is used which opens the item specified by the lpFile parameter. The item can be a file or folder.
A pointer to a null-terminated string that specifies the file or object on which to execute the specified verb. To specify a Shell namespace object, pass the fully qualified parse name. Note that not all verbs are supported on all objects. For example, not all document types support the “print” verb. If a relative path is used for the lpDirectory parameter do not use a relative path for lpFile. In our case it’s an array of characters ‘url’ on which the operation is executed.
If lpFile specifies an executable file, this parameter is a pointer to a null-terminated string that specifies the parameters to be passed to the application. The format of this string is determined by the verb that is to be invoked. If lpFile specifies a document file, lpParameters should be NULL.
A pointer to a null-terminated string that specifies the default (working) directory for the action. If this value is NULL, the current working directory is used. If a relative path is provided at lpFile, do not use a relative path for lpDirectory.
The flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file, the flag is simply passed to the associated application. It is up to the application to decide how to handle it. These values are defined in Winuser.h. In our case SW_SHOWNORMAL is used which activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, however.
Input : A list of URLs stored in a text file. Output : All the URLs opened in different tabs.
List of all urls present in the urls.txt file opened in different tabs.
Note: Run the above codes in some C++ editor like Codeblocks and the text file and the code should be present in the same folder.
This article is contributed by Aditya Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.