1 | #Region "Microsoft.VisualBasic::edc000362f4d5dfd9be253745cf13508, Microsoft.VisualBasic.Core\ApplicationServices\VBDev\Ngen\Registry.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 RegistryUtils |
35 | ' |
36 | ' Function: RegisterCOMDll, RegisterExtensions, WriteToRegistry |
37 | ' |
38 | ' |
39 | ' /********************************************************************************/ |
40 | |
41 | #End Region |
42 | |
43 | Imports System.Reflection |
44 | Imports System.Runtime.InteropServices |
45 | Imports Microsoft.VisualBasic.CommandLine.Reflection |
46 | Imports Microsoft.VisualBasic.Scripting.MetaData |
47 | Imports Microsoft.Win32 |
48 | |
49 | Namespace ApplicationServices |
50 | |
51 | Public Module RegistryUtils |
52 | |
53 | ''' <summary> |
54 | ''' Register a .NET dll file as a COM component.(将某一个.NET语言所编写的DLL文件注册为COM组件) |
55 | ''' </summary> |
56 | ''' <param name="COM_Dll"></param> |
57 | ''' <returns></returns> |
58 | ''' <remarks></remarks> |
59 | <ExportAPI("RegAsm")> |
60 | Public Function RegisterCOMDll(<Parameter("COM.Dll", "The file path of the DLL file which will be registered as a COM component.")> COM_Dll As String) As Boolean |
61 | Dim Asm As Assembly = Assembly.LoadFile(COM_Dll) |
62 | Dim regAsm As New RegistrationServices |
63 | Dim bResult As Boolean = regAsm.RegisterAssembly(Asm, AssemblyRegistrationFlags.SetCodeBase) |
64 | Return bResult |
65 | End Function |
66 | |
67 | ''' <summary> |
68 | ''' ### Registering Extensions of the .NET Framework |
69 | ''' |
70 | ''' > https://msdn.microsoft.com/en-us/library/ee722096.aspx |
71 | ''' </summary> |
72 | ''' <param name="PATH$"></param> |
73 | Public Function RegisterExtensions(PATH$, company$) As String |
74 | Try |
75 | Call WriteToRegistry(RegistryHive.LocalMachine, $"SOFTWARE\Microsoft\.NETFramework\v4.0.30319\AssemblyFoldersEx\{company}\", "", PATH) |
76 | Catch ex As Exception |
77 | Return ex.ToString |
78 | End Try |
79 | |
80 | Return "success!" |
81 | End Function |
82 | |
83 | Public Function WriteToRegistry(ParentKeyHive As RegistryHive, SubKeyName$, ValueName$, Value As Object) As Boolean |
84 | |
85 | ' DEMO USAGE |
86 | ' Dim bAns As Boolean |
87 | ' bAns = WriteToRegistry(RegistryHive.LocalMachine, "SOFTWARE\MyCompany\MyProgram\", "ProgramHasRunBefore", "Y") |
88 | ' Debug.WriteLine("Registry Write Successful: " & bAns) |
89 | |
90 | Dim objSubKey As RegistryKey |
91 | Dim sException$ = Nothing |
92 | Dim objParentKey As RegistryKey |
93 | Dim bAns As Boolean |
94 | |
95 | Select Case ParentKeyHive |
96 | Case RegistryHive.ClassesRoot |
97 | objParentKey = Registry.ClassesRoot |
98 | Case RegistryHive.CurrentConfig |
99 | objParentKey = Registry.CurrentConfig |
100 | Case RegistryHive.CurrentUser |
101 | objParentKey = Registry.CurrentUser |
102 | Case RegistryHive.DynData |
103 | #Disable Warning |
104 | objParentKey = Registry.DynData |
105 | #Enable Warning |
106 | Case RegistryHive.LocalMachine |
107 | objParentKey = Registry.LocalMachine |
108 | Case RegistryHive.PerformanceData |
109 | objParentKey = Registry.PerformanceData |
110 | Case RegistryHive.Users |
111 | objParentKey = Registry.Users |
112 | Case Else |
113 | Throw New Exception(ParentKeyHive.ToString) |
114 | End Select |
115 | |
116 | 'Open |
117 | objSubKey = objParentKey.OpenSubKey(SubKeyName, True) |
118 | |
119 | 'create if doesn't exist |
120 | If objSubKey Is Nothing Then |
121 | objSubKey = objParentKey.CreateSubKey(SubKeyName) |
122 | End If |
123 | |
124 | objSubKey.SetValue(ValueName, Value) |
125 | bAns = True |
126 | |
127 | Return bAns |
128 | End Function |
129 | End Module |
130 | End Namespace |