Open a local exe file using a web page

When I first received the task of "using a web page to open a local exe file", I was really confused. Later, I thought about it. Clicking the seller's Want on Taobao can open the local Want, isn't this the webpage that is used to open the local exe file? ?

Once you understand this real and legitimate need, start investigating. After online search and query, it is mainly summarized into two implementation methods, method one: use JS to open the local exe file. General browsers, due to security issues, will disable this feature, which leads to some browsers not supporting this method. Method 2: Use the browser external protocol (URL Procotol) to open the local exe file. Implemented this way, any browser is compatible. In actual development, of course, the second method is preferred.

First, use the registry file to write the external protocol into the registry

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\PCTV]
@="PCTVProtocol"
"URL Protocol"="\"C:\\Program Files (x86)\\PCTV Dual Mode Soft Terminal_64bit\\PCTV.exe\""

[HKEY_CLASSES_ROOT\PCTV\DefaultIcon]
@="\"C:\\Program Files (x86)\\PCTV Dual Mode Soft Terminal_64bit\\PCTV.exe,1\""

[HKEY_CLASSES_ROOT\PCTV\shell]

[HKEY_CLASSES_ROOT\PCTV\shell\open]

[HKEY_CLASSES_ROOT\PCTV\shell\open\command]
@="\"C:\\Program Files (x86)\\PCTV Dual Mode Soft Terminal_64bit\\PCTV.exe\" \"%1\""

 Save the above code into the reg file and double-click the file to execute it. Enter "pctv://" or "pctv://param1,param2" in the browser, and the exe file in the corresponding path can be opened after execution.

2. Write the external protocol to the registry when installing the exe file

In actual deployment, customers will not be allowed to manually click the registry file to write the installation path into the registry after installing the program. The most acceptable way is to write the installation path into the registry when installing the exe file. When using Inno Setup to package the exe file, add the following code to the script:

[Registry]  
Root:HKCR;Subkey:"PCTV";ValueType:string;ValueName:"URL Protocol";ValueData:"{app}\{#MyAppExeName}";Flags:createvalueifdoesntexist uninsdeletekey
Root:HKCR;Subkey:"PCTV\DefaultIcon";ValueType:string;ValueData:"{app}\{#MyAppExeName}";Flags:createvalueifdoesntexist uninsdeletekey   
Root:HKCR;Subkey:"PCTV\shell";Flags:createvalueifdoesntexist uninsdeletekey
Root:HKCR;Subkey:"PCTV\shell\open";Flags:createvalueifdoesntexist uninsdeletekey
Root:HKCR;Subkey:"PCTV\shell\open\command";ValueType:string;ValueData:"{app}\{#MyAppExeName} ""%1""";Flags:createvalueifdoesntexist uninsdeletekey

  In this way, enter "pctv://" or "pctv://param1,param2" in the browser, and the exe file in the corresponding path can be opened after execution.

3. Problems encountered

When using an external protocol to open a local exe file, by viewing the log records, we can see that the path is incorrect. By viewing the code, use Environment.CurrentDirectory to obtain the path of the executable file in the program, but when opening the exe file through the browser, Environment.CurrentDirectory obtains the path of the browser exe file, so an error will be reported in the program. The solution is to change Environment.CurrentDirectory to Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory).

The above two ways of writing registration are allowed to take parameters in the external protocol.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325792907&siteId=291194637