2 // Microsoft.Win32/Win32RegistryApi.cs: wrapper for win32 registry API
5 // Erik LeBel (eriklebel@yahoo.ca)
6 // Jackson Harper (jackson@ximian.com)
8 // Copyright (C) Erik LeBel 2004
9 // (C) 2004 Novell, Inc (http://www.novell.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System
.Runtime
.InteropServices
;
39 namespace Microsoft
.Win32
42 /// Function stubs, constants and helper functions for
43 /// the Win32 registry manipulation utilities.
45 internal class Win32RegistryApi
: IRegistryApi
47 // bit masks for registry key open access permissions
48 public int OpenRegKeyRead
{
49 get { return 0x00020019; }
52 public int OpenRegKeyWrite
{
53 get { return 0x00020006; }
56 // type values for registry value data
57 public int RegStringType
{
61 public int RegEnvironmentString
{
65 public int RegBinaryType
{
69 public int RegDwordType
{
73 public int RegStringArrayType
{
78 /// Create a registry key.
80 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegCreateKey")]
81 private static extern int RegCreateKey_Internal (IntPtr keyBase
,
82 string keyName
, out IntPtr keyHandle
);
84 public int RegCreateKey (IntPtr keybase
, string keyname
, out IntPtr handle
)
86 return RegCreateKey_Internal (keybase
, keyname
, out handle
);
90 /// Close a registry key.
92 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegCloseKey")]
93 private static extern int RegCloseKey_Internal (IntPtr keyHandle
);
95 public int RegCloseKey (IntPtr handle
)
97 return RegCloseKey_Internal (handle
);
101 /// Flush a registry key's current state to disk.
103 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegFlushKey")]
104 private static extern int RegFlushKey_Internal (IntPtr keyHandle
);
106 public int RegFlushKey (IntPtr handle
)
108 return RegFlushKey_Internal (handle
);
112 /// Open a registry key.
113 /// 'unknown' must be zero.
115 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegOpenKeyEx")]
116 private static extern int RegOpenKeyEx_Internal (IntPtr keyBase
,
117 string keyName
, IntPtr reserved
, int access
,
118 out IntPtr keyHandle
);
120 public int RegOpenKeyEx (IntPtr keybase
, string keyname
, IntPtr reserved
,
121 int access
, out IntPtr handle
)
123 return RegOpenKeyEx_Internal (keybase
, keyname
, reserved
, access
, out handle
);
127 /// Delete a registry key.
129 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegDeleteKey")]
130 private static extern int RegDeleteKey_Internal (IntPtr keyHandle
,
133 public int RegDeleteKey (IntPtr handle
, string valuename
)
135 return RegDeleteKey_Internal (handle
, valuename
);
139 /// Delete a registry value.
141 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegDeleteValue")]
142 private static extern int RegDeleteValue_Internal (IntPtr keyHandle
,
145 public int RegDeleteValue (IntPtr handle
, string valuename
)
147 return RegDeleteValue_Internal (handle
, valuename
);
151 /// Fetch registry key subkeys itteratively.
153 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegEnumKey")]
154 private static extern int RegEnumKey_Internal (IntPtr keyBase
, int index
,
155 [Out
] byte[] nameBuffer
, int bufferLength
);
157 public int RegEnumKey (IntPtr keybase
, int index
,
158 [Out
] byte [] namebuffer
, int buffer_length
)
160 return RegEnumKey_Internal (keybase
, index
, namebuffer
, buffer_length
);
164 /// Fetch registry key value names itteratively.
166 /// Arguments 'reserved', 'data', 'dataLength'
167 /// should be set to IntPtr.Zero.
169 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegEnumValue")]
170 private static extern int RegEnumValue_Internal (IntPtr keyBase
,
171 int index
, StringBuilder nameBuffer
,
172 ref int nameLength
, IntPtr reserved
,
173 ref int type
, IntPtr data
, IntPtr dataLength
);
175 public int RegEnumValue (IntPtr keybase
, int index
, StringBuilder namebuffer
,
176 ref int namelength
, IntPtr reserved
, ref int type
, IntPtr data
,
179 return RegEnumValue_Internal (keybase
, index
, namebuffer
, ref namelength
,
180 reserved
, ref type
, data
, datalength
);
184 /// Set a registry value with string builder data.
186 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegSetValueEx")]
187 private static extern int RegSetValueEx_Internal (IntPtr keyBase
,
188 string valueName
, IntPtr reserved
, int type
,
189 StringBuilder data
, int rawDataLength
);
191 public int RegSetValueEx (IntPtr keybase
, string valuename
, IntPtr reserved
,
192 int type
, StringBuilder data
, int datalength
)
194 return RegSetValueEx_Internal (keybase
, valuename
, reserved
,
195 type
, data
, datalength
);
199 /// Set a registry value with string data.
201 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegSetValueEx")]
202 private static extern int RegSetValueEx_Internal (IntPtr keyBase
,
203 string valueName
, IntPtr reserved
, int type
,
204 string data
, int rawDataLength
);
206 public int RegSetValueEx (IntPtr keybase
, string valuename
, IntPtr reserved
,
207 int type
, string data
, int datalength
)
209 return RegSetValueEx_Internal (keybase
, valuename
, reserved
,
210 type
, data
, datalength
);
214 /// Set a registry value with binary data (a byte array).
216 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegSetValueEx")]
217 private static extern int RegSetValueEx_Internal (IntPtr keyBase
,
218 string valueName
, IntPtr reserved
, int type
,
219 byte[] rawData
, int rawDataLength
);
221 public int RegSetValueEx (IntPtr keybase
, string valuename
, IntPtr reserved
,
222 int type
, byte [] data
, int datalength
)
224 return RegSetValueEx_Internal (keybase
, valuename
, reserved
,
225 type
, data
, datalength
);
229 /// Set a registry value to a DWORD value.
231 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegSetValueEx")]
232 private static extern int RegSetValueEx_Internal (IntPtr keyBase
,
233 string valueName
, IntPtr reserved
, int type
,
234 ref int data
, int rawDataLength
);
236 public int RegSetValueEx (IntPtr keybase
, string valuename
, IntPtr reserved
,
237 int type
, ref int data
, int datalength
)
239 return RegSetValueEx_Internal (keybase
, valuename
, reserved
, type
,
240 ref data
, datalength
);
244 /// Get a registry value's info. No data.
246 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegQueryValueEx")]
247 private static extern int RegQueryValueEx_Internal (IntPtr keyBase
,
248 string valueName
, IntPtr reserved
, ref int type
,
249 IntPtr zero
, ref int dataSize
);
251 public int RegQueryValueEx (IntPtr keybase
, string valuename
, IntPtr reserved
,
252 ref int type
, IntPtr zero
, ref int datasize
)
254 return RegQueryValueEx_Internal (keybase
, valuename
, reserved
,
255 ref type
, zero
, ref datasize
);
259 /// Get a registry value. Binary data.
261 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegQueryValueEx")]
262 private static extern int RegQueryValueEx_Internal (IntPtr keyBase
,
263 string valueName
, IntPtr reserved
, ref int type
,
264 [Out
] byte[] data
, ref int dataSize
);
266 public int RegQueryValueEx (IntPtr keybase
, string valuename
, IntPtr reserved
,
267 ref int type
, [Out
] byte [] data
, ref int datasize
)
269 return RegQueryValueEx_Internal (keybase
, valuename
, reserved
,
270 ref type
, data
, ref datasize
);
274 /// Get a registry value. DWORD data.
276 [DllImport ("advapi32.dll", CharSet
=CharSet
.Unicode
, EntryPoint
="RegQueryValueEx")]
277 private static extern int RegQueryValueEx_Internal (IntPtr keyBase
,
278 string valueName
, IntPtr reserved
, ref int type
,
279 ref int data
, ref int dataSize
);
281 public int RegQueryValueEx (IntPtr keybase
, string valuename
, IntPtr reserved
,
282 ref int type
, ref int data
, ref int datasize
)
284 return RegQueryValueEx_Internal (keybase
, valuename
, reserved
,
285 ref type
, ref data
, ref datasize
);