Click here to Skip to main content
15,868,082 members
Articles / Desktop Programming / MFC

Custom URL Protocol for Invoking Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (18 votes)
6 Mar 2012CPOL3 min read 123.5K   14.8K   50   12
Registering a custom URL protocol for invoking an application
Image 1

Introduction

While working, I came across a problem which required to invoke a Windows application (present in a local machine) from my web application. After some Google search, I came to know that by using custom URL protocol, we can invoke a Windows application. I found that there is a lot of information present related to custom URL protocol. I also found some sample applications on the internet. But all the sample applications seem to be made in C# or some other language. But as per the project requirement, I needed to write code in C++\Win32 for registering custom URL protocol handler. I have written a C++\Win32 class which registers the custom URL protocol handler.

This article provides information for registering a client side Custom URL protocol handler for invoking windows application from web based applications. I have attached the "CustomURLProtocolFiles.zip" file which contains a cpp and header file. This files can be added in the application for creating a custom URL protocol. I have also attached a demo application along with its source which uses this Custom URL protocol files.

Custom URL protocol can also be used for passing content between applications.

Example: Application A wants to pass some data after its processing to application B. For achieving this, application A can register a protocol handler and can call application B with processed data as parameter. In this way, one application can pass data to another application.

Using the Code

Zip file "CustomURLProtocolFiles.zip" contains a cpp file and a header file which should be included in your project. This file contains a class named "class CustomURLProtocol".This class contains setter and getter function for protocol name, company name and application path which needs to be launched.

C++
std::wstring getProtocolName() {return wszProtocolName;}
std::wstring getCompanyName() {return wszCompanyName;}
std::wstring getAppPath() {return wszAppPath;}

void setProtocolName(std::wstring pwProtocolName){wszProtocolName = pwProtocolName;}
void setCompanyName(std::wstring pwCompanyName){wszCompanyName = pwCompanyName;}
void setAppPath(std::wstring pwAppPath){wszAppPath = pwAppPath;}

Calling code has to create an object of the CustomURLProtocol class. With the help of setter function, calling application has to set required parameters like protocol name, company name and application path. Once this parameter is set, calling application can call CreateCustomProtocol function. This function will create a custom URL protocol for the application.

In my demo, the application calls initializeParameter function which calls the setter function of CustomURLProtocol class. It calls DeleteCustomProtocol function which will delete the custom protocol which is already present. Finally, it calls CreateCustomProtocol function which will create a custom protocol for launching the application.

C++
void CCustomURLProtocolDlg::OnBnClickedOk()
{
  DWORD errorCode = 0;

  // Initialize Custom Protocol Class Parameter
  initializeParameter();

  // Delete Custom Protocol if already present
  m_CustomURLProtocol.DeleteCustomProtocol();

  // Create new Custom protocol
	if(m_CustomURLProtocol.CreateCustomProtocol() != ERROR_SUCCESS)
		MessageBox(m_CustomURLProtocol.getErrorMsg().c_str());
	else
		MessageBox(L"Successfully Created Custom Protocol.");
}

CreateCustomProtocol function registers the custom protocol by entering the below keys in the registry.

CustomURLProtocol class also handles error conditions. The above code snippets show that if some error happens, then the application can get error description by calling m_CustomURLProtocol.getErrorMsg().c_str()

C++
[HKEY_CLASSES_ROOT]
  [< ProtocolName >]
      (Default) = "URL:< ProtocolName > Protocol Handler"
      URL Protocol = ""
    [DefaultIcon]
       (Default) = "< Application Path >"
    [shell]
       [open]
         [command]
           (Default) = "< Application Path> "%1""

For deleting custom protocol, we just need to call DeleteCustomProtocol function as called in the demo function below. First, we need to initialize the parameter of CustomURLProtocol class and then call DeleteCustomProtocol function.

C++
void CCustomURLProtocolDlg::OnBnClickedBtnDelete()
{
    DWORD errorCode = 0;
    initializeParameter();
	if(m_CustomURLProtocol.DeleteCustomProtocol() != ERROR_SUCCESS)
		MessageBox(m_CustomURLProtocol.getErrorMsg().c_str());
	else
		MessageBox(L"Successfully Deleted Custom Protocol.");
}

Registry key creation and deletion needs admin privileges in some Windows OS. This will need your app to run in admin privileges. My demo application contains a manifest file which is used to launch demo application with admin privileges. If there is any error, then demo application provides description of error along with error code.

In the demo app, I have also provided a way to test the registered custom URL protocol handler. When user clicks Test button, demo application generates an HTML file which launches the registered application. This test app can be used as a sample for invoking the application.

The provided class for creating a custom protocol can be updated according to the need of the project. I will update the code as I come to know about issues in it.

History

  • 17th February, 2012: Initial version
  • 27th February, 2012: Added Error Handling code in CustomURLProtocol class
  • 06th March, 2012: Solved issue related to chrome browser in test application source code

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncan i convert exe dmg to html with your app Pin
yasien trabih 20216-Dec-22 0:02
yasien trabih 20216-Dec-22 0:02 
QuestionDidn't work for me either no errors Pin
Alistair Rigney 202121-Oct-21 18:56
Alistair Rigney 202121-Oct-21 18:56 
Questionplease source code not open Pin
Member 1060694310-Dec-19 19:15
Member 1060694310-Dec-19 19:15 
QuestionDo not run on windows 10 Pin
Member 1013238726-Jul-19 5:54
Member 1013238726-Jul-19 5:54 
QuestionSource code doesn't exist Pin
SAlvador.ta26-Jul-17 7:21
SAlvador.ta26-Jul-17 7:21 
QuestionI don't see notepad Pin
Sam Hobbs13-Apr-16 9:38
Sam Hobbs13-Apr-16 9:38 
Thank you, this looks good.

I ran the demo as an administrator and created a protocol for notepad and tested it and clicked the button in the browser but still I don't see notepad. I am not sure what is supposed to happen.

Okay, probably the reason it does not work for me is that Microsoft has changed the way it works, beginning with Windows 8 probably. Now the user has the power to select what is used so what is shown in this article is now unsupported by Windows.

modified 21-May-16 12:55pm.

Question"Index was outside the bounds of the array" Pin
Member 1239976018-Mar-16 10:33
Member 1239976018-Mar-16 10:33 
AnswerRe: "Index was outside the bounds of the array" Pin
Chandrakantt14-Jun-16 2:34
Chandrakantt14-Jun-16 2:34 
Questionvb compatability Pin
chabad36522-Jun-14 12:44
chabad36522-Jun-14 12:44 
AnswerRe: vb compatability Pin
Mr.ExA-MaN19-Apr-15 14:42
Mr.ExA-MaN19-Apr-15 14:42 
Questionnot working Pin
Alexandre GRANVAUD1-Mar-12 20:59
Alexandre GRANVAUD1-Mar-12 20:59 
AnswerRe: not working Pin
Chandrakantt1-Mar-12 22:19
Chandrakantt1-Mar-12 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.