Understanding ShellExecute function and it’s application to open a list of URLs present in a file using C++ code

07 Jul, 2017
Given a URL as a string open it using a C++ code in Microsoft Windows OS.Example:
Input : https://www.geeksforgeeks.org/
Output : The site opened in your browser.

// C++ program to open a URL in browser.// This program is written on for Microsoft// Windows OS#include <bits/stdc++.h>#include <windows.h>using namespace std;  int main(){    char url[100] = "http:// www.geeksforgeeks.org/";    ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);    return 0;}
Output:
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);
Parameters:
Type: HWND
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.
Type: LPCTSTR
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.
Type: LPCTSTRA 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.
Type: LPCTSTRIf 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.
Type: LPCTSTRA 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.
Type: INTThe 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.
Return valueType: HINSTANCEIf 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.
Example:
Input : A list of URLs stored in a text file.
Output : All the URLs opened in different tabs.

// C++ program to list URLs in a text file and// open them.// This program is written only for Microsoft// Windows OS#include <bits/stdc++.h>#include <windows.h>using namespace std;  int main(){    long long int s, i, x, c = 0;    char temp[1000];      // Name of the text file which contains all the    // URLs separated by end line.    ifstream all_lines("urls.txt");    string line; // To get each line of the file as a string.      while (getline(all_lines, line, 'n'))    {        x = 0; // For getting each character of a string.        s = line.size(); // Measuring size of each string.          // Here we limit number of tabs to 10, we can        // change the value of c according to your        // requirement.        for (i = 0; i <= s && c < 10; i++) {            if (line[i] != 't' && line[i] != '\0')            {                temp[x] = line[i];                x++;            }            else            {                temp[x] = '\0'; ShellExecute(NULL, "open", temp, NULL,                                                  NULL, SW_SHOWNORMAL);                  // Keeping a count of number of tabs                // getting opened.                c++;            }        }    }    system("pause");    return 0;}
Output:
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.
Reference: https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
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 contribute@geeksforgeeks.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.
CPP-Library
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Polymorphism in C++
Queue in C++ Standard Template Library (STL)
List in C++ Standard Template Library (STL)
Command line arguments in C/C++
Exception Handling in C++
Operators in C / C++
Sorting a vector in C++
Destructors in C++
Power Function in C/C++
Pure Virtual Functions and Abstract Classes in C++