| 1 | #Region "Microsoft.VisualBasic::0e83d38969f603db39f2d9324e5dde95, Microsoft.VisualBasic.Core\ApplicationServices\Tools\Win32\WindowsServices.vb" | 
| 2 | |
| 3 | ' Author: | 
| 4 | ' | 
| 5 | '       asuka (amethyst.asuka@gcmodeller.org) | 
| 6 | '       xie (genetics@smrucc.org) | 
| 7 | '       xieguigang (xie.guigang@live.com) | 
| 8 | ' | 
| 9 | ' Copyright (c) 2018 GPL3 Licensed | 
| 10 | ' | 
| 11 | ' | 
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) | 
| 13 | ' | 
| 14 | ' | 
| 15 | ' This program is free software: you can redistribute it and/or modify | 
| 16 | ' it under the terms of the GNU General Public License as published by | 
| 17 | ' the Free Software Foundation, either version 3 of the License, or | 
| 18 | ' (at your option) any later version. | 
| 19 | ' | 
| 20 | ' This program is distributed in the hope that it will be useful, | 
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 23 | ' GNU General Public License for more details. | 
| 24 | ' | 
| 25 | ' You should have received a copy of the GNU General Public License | 
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. | 
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ | 
| 31 | |
| 32 | ' Summaries: | 
| 33 | |
| 34 | '     Module WindowsServices | 
| 35 | ' | 
| 36 | '         Properties: Initialized, ServicesLogs | 
| 37 | ' | 
| 38 | '         Function: (+3 Overloads) Initialize, LogsInstaller | 
| 39 | ' | 
| 40 | '         Sub: RegisterURLProtocol | 
| 41 | ' | 
| 42 | ' | 
| 43 | ' /********************************************************************************/ | 
| 44 | |
| 45 | #End Region | 
| 46 | |
| 47 | Imports Microsoft.Win32 | 
| 48 | Imports Logs = Microsoft.VisualBasic.ApplicationServices.Debugging.Logging.EventLog | 
| 49 | |
| 50 | Namespace Win32 | 
| 51 | |
| 52 | ''' <summary> | 
| 53 | ''' Windows event logger services & installer services. | 
| 54 | ''' (这个模块主要的功能是日志服务,包括在安装阶段对日志记录的创建以及自定义url协议的创建等, | 
| 55 | ''' 请注意,这个模块之中的大部分的功能都需要你的应用程序是在管理员权限之下运行的) | 
| 56 | ''' </summary> | 
| 57 | Public Module WindowsServices | 
| 58 | |
| 59 | ''' <summary> | 
| 60 | ''' Windows system logging services interface, you can viewing the application log events from Event Viewer: | 
| 61 | ''' Explorer >> Manage >> Event Viewer >> Applications and Services Logs >> <Your_Product> | 
| 62 | ''' </summary> | 
| 63 | ''' <returns></returns> | 
| 64 | Public ReadOnly Property ServicesLogs As Logs | 
| 65 | |
| 66 | ''' <summary> | 
| 67 | ''' Does component <see cref="ServicesLogs"/> have been initialized? | 
| 68 | ''' </summary> | 
| 69 | ''' <returns></returns> | 
| 70 | Public ReadOnly Property Initialized As Boolean | 
| 71 | Get | 
| 72 | Return Not ServicesLogs Is Nothing AndAlso ServicesLogs.Initialized | 
| 73 | End Get | 
| 74 | End Property | 
| 75 | |
| 76 | ''' <summary> | 
| 77 | ''' You should execute the log category entry creates job under the administrators privileges! | 
| 78 | ''' </summary> | 
| 79 | ''' <returns></returns> | 
| 80 | Public Function Initialize() As Boolean | 
| 81 | Return Initialize(GetType(Logs).FullName, App.AssemblyName) | 
| 82 | End Function | 
| 83 | |
| 84 | ''' <summary> | 
| 85 | ''' You should execute the log category entry creates job under the administrators privileges! | 
| 86 | ''' </summary> | 
| 87 | ''' <param name="ServicesName"></param> | 
| 88 | ''' <returns></returns> | 
| 89 | Public Function Initialize(ServicesName As String) As Boolean | 
| 90 | Return Initialize(ServicesName, App.AssemblyName) | 
| 91 | End Function | 
| 92 | |
| 93 | ''' <summary> | 
| 94 | ''' You should execute the log category entry creates job under the administrators privileges! | 
| 95 | ''' </summary> | 
| 96 | ''' <param name="ServicesName"></param> | 
| 97 | ''' <param name="Product">This value is usually the property value of <see cref="App.AssemblyName"/></param> | 
| 98 | ''' <returns></returns> | 
| 99 | Public Function Initialize(ServicesName As String, Product As String) As Boolean | 
| 100 | WindowsServices._ServicesLogs = New Logs(ServicesName, Product) | 
| 101 | If Not WindowsServices.ServicesLogs.Initialized Then | 
| 102 | Call $"Exception during register node: {WindowsServices.ServicesLogs.ToString}.".__DEBUG_ECHO | 
| 103 | Call $"You should execute the log category entry creates job under the administrators privileges!".__DEBUG_ECHO | 
| 104 | End If | 
| 105 | |
| 106 | Return WindowsServices.ServicesLogs.Initialized | 
| 107 | End Function | 
| 108 | |
| 109 | ''' <summary> | 
| 110 | ''' You should execute the log category entry creates job under the administrators privileges! | 
| 111 | ''' </summary> | 
| 112 | ''' <param name="ServicesName"></param> | 
| 113 | ''' <param name="Products">This value is usually the property value of <see cref="App.AssemblyName"/></param> | 
| 114 | ''' <returns></returns> | 
| 115 | Public Function LogsInstaller(ServicesName As String, ParamArray Products As String()) As Boolean | 
| 116 | Dim b As Boolean = True | 
| 117 | |
| 118 | For Each Product As String In Products | 
| 119 | If Not WindowsServices.Initialize(ServicesName, Product) Then | 
| 120 | b = False | 
| 121 | End If | 
| 122 | Next | 
| 123 | |
| 124 | If Not b Then | 
| 125 | Call "Parts of the log entry was installed failed....".__DEBUG_ECHO | 
| 126 | End If | 
| 127 | |
| 128 | Return b | 
| 129 | End Function | 
| 130 | |
| 131 | ''' <summary> | 
| 132 | ''' (**** Please notice, that the application has To have admin privileges To be able To write the needed stuff into registry. ****) | 
| 133 | ''' | 
| 134 | ''' Everyone knows HTTP-URLs. Windows Shell also enables to define own ``URL protocols``. | 
| 135 | ''' Some programs (like Visual Studio Help ``ms-help://`` ... or Steam ``steam://`` ...) take advantage of this feature. | 
| 136 | ''' By creating some registry entries one is able to set up a self-made URL protocol. | 
| 137 | ''' This allows to access your applications by URL (originating from every software). | 
| 138 | ''' | 
| 139 | ''' Please notice, that **the application has To have admin privileges To be able To write the needed stuff into registry**. | 
| 140 | ''' You can test your application very easy by opening Windows Explorer And typing ``yoururlprotocol://testdata`` | 
| 141 | ''' into the path/address field. | 
| 142 | ''' | 
| 143 | ''' Registers an user defined URL protocol for the usage with the Windows Shell, the Internet Explorer and Office. | 
| 144 | ''' | 
| 145 | ''' Example for an URL of an user defined URL protocol: | 
| 146 | ''' | 
| 147 | ''' ``` | 
| 148 | ''' rainbird://RemoteControl/OpenFridge/GetBeer | 
| 149 | ''' ``` | 
| 150 | ''' </summary> | 
| 151 | ''' <param name="protocolName"> | 
| 152 | ''' Name of the protocol (e.g. "rainbird" for "rainbird://...") | 
| 153 | ''' </param> | 
| 154 | ''' <param name="applicationPath"> | 
| 155 | ''' Complete file system path to the EXE file, which processes the URL being called (the complete URL is handed over as a Command Line Parameter). | 
| 156 | ''' </param> | 
| 157 | ''' <param name="description"> | 
| 158 | ''' Description (e.g. "URL:Rainbird Custom URL") | 
| 159 | ''' </param> | 
| 160 | Public Sub RegisterURLProtocol(protocolName As String, applicationPath As String, description As String) | 
| 161 | ' Create new key for desired URL protocol | 
| 162 | Dim protocol As RegistryKey = Registry.ClassesRoot.CreateSubKey(protocolName) | 
| 163 | |
| 164 | ' Assign protocol | 
| 165 | Call protocol.SetValue(Nothing, description) | 
| 166 | Call protocol.SetValue("URL Protocol", String.Empty) | 
| 167 | |
| 168 | ' Register Shell values | 
| 169 | Call Registry.ClassesRoot.CreateSubKey(protocolName & "\Shell") | 
| 170 | Call Registry.ClassesRoot.CreateSubKey(protocolName & "\Shell\open") | 
| 171 | protocol = Registry.ClassesRoot.CreateSubKey(protocolName & "\Shell\open\command") | 
| 172 | |
| 173 | ' Specify application handling the URL protocol | 
| 174 | Call protocol.SetValue(Nothing, """" & applicationPath, +""" %1") | 
| 175 | End Sub | 
| 176 | End Module | 
| 177 | End Namespace |