Skip to content

Commit aae2782

Browse files
author
Pallavi Taneja
committed
Consolidate interop calls for Microsoft.Win32.Registry library
Move all pinvokes, win32 constants to the common\Interop folder where they can be shared across libraries.
1 parent 094eb33 commit aae2782

25 files changed

+534
-244
lines changed

src/Common/src/Interop/Windows/Interop.Libraries.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ internal static partial class Interop
66
private static class Libraries
77
{
88
internal const string Debug = "api-ms-win-core-debug-l1-1-0.dll";
9-
internal const string Localization = "api-ms-win-core-localization-l1-2-0.dll";
109
internal const string Handle = "api-ms-win-core-handle-l1-1-0.dll";
10+
internal const string Localization = "api-ms-win-core-localization-l1-2-0.dll";
11+
internal const string PROCESSENVIRONMENT_L1_APISET = "api-ms-win-core-processenvironment-l1-1-0.dll";
12+
internal const string REGISTRY_L1_APISET = "api-ms-win-core-registry-l1-1-0.dll";
13+
internal const string REGISTRY_L2_APISET = "api-ms-win-core-registry-l2-1-0.dll";
1114
internal const string User32 = "user32.dll";
1215
}
1316
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
internal partial class Interop
5+
{
6+
internal partial class mincore
7+
{
8+
internal partial class Errors
9+
{
10+
internal const int ERROR_SUCCESS = 0x0;
11+
internal const int ERROR_FILE_NOT_FOUND = 0x2;
12+
internal const int ERROR_ACCESS_DENIED = 0x5;
13+
internal const int ERROR_INVALID_HANDLE = 0x6;
14+
internal const int ERROR_FILENAME_EXCED_RANGE = 0xCE; // filename too long.
15+
internal const int ERROR_MORE_DATA = 0xEA;
16+
internal const int ERROR_DLL_INIT_FAILED = 0x45A;
17+
internal const int ERROR_BAD_IMPERSONATION_LEVEL = 0x542;
18+
}
19+
}
20+
}

src/Common/src/Interop/Windows/mincore/Interop.FormatMessage.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ internal partial class mincore
1515

1616
private const int ERROR_INSUFFICIENT_BUFFER = 0x7A;
1717

18-
[DllImport(Libraries.Localization, CharSet = CharSet.Unicode, EntryPoint="FormatMessageW", SetLastError = true, BestFitMapping = true)]
19-
private static extern int FormatMessage(int dwFlags, IntPtr lpSource_mustBeNull, uint dwMessageId,
20-
int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr[] arguments);
18+
[DllImport(Libraries.Localization, CharSet = CharSet.Unicode, EntryPoint = "FormatMessageW", SetLastError = true, BestFitMapping = true)]
19+
private static extern int FormatMessage(
20+
int dwFlags,
21+
IntPtr lpSource_mustBeNull,
22+
uint dwMessageId,
23+
int dwLanguageId,
24+
StringBuilder lpBuffer,
25+
int nSize,
26+
IntPtr[] arguments);
2127

2228
/// <summary>
2329
/// Returns a string message for the specified Win32 error code.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Win32.SafeHandles;
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
internal partial class Interop
9+
{
10+
internal partial class mincore
11+
{
12+
[DllImport(Libraries.REGISTRY_L1_APISET)]
13+
internal extern static int RegCloseKey(IntPtr hKey);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Win32.SafeHandles;
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
internal partial class Interop
9+
{
10+
internal partial class mincore
11+
{
12+
[DllImport(Libraries.REGISTRY_L2_APISET, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegConnectRegistryW")]
13+
internal static extern int RegConnectRegistry(String machineName, SafeRegistryHandle key, out SafeRegistryHandle result);
14+
}
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Win32.SafeHandles;
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
internal partial class Interop
9+
{
10+
internal partial class mincore
11+
{
12+
// Note: RegCreateKeyEx won't set the last error on failure - it returns
13+
// an error code if it fails.
14+
[DllImport(Libraries.REGISTRY_L1_APISET, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegCreateKeyExW")]
15+
internal static extern int RegCreateKeyEx(
16+
SafeRegistryHandle hKey,
17+
String lpSubKey,
18+
int Reserved,
19+
String lpClass,
20+
int dwOptions,
21+
int samDesired,
22+
ref SECURITY_ATTRIBUTES secAttrs,
23+
out SafeRegistryHandle hkResult,
24+
out int lpdwDisposition);
25+
}
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Win32.SafeHandles;
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
internal partial class Interop
9+
{
10+
internal partial class mincore
11+
{
12+
[DllImport(Libraries.REGISTRY_L1_APISET, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegDeleteKeyExW")]
13+
internal static extern int RegDeleteKeyEx(SafeRegistryHandle hKey, String lpSubKey, int samDesired, int Reserved);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Win32.SafeHandles;
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
internal partial class Interop
9+
{
10+
internal partial class mincore
11+
{
12+
[DllImport(Libraries.REGISTRY_L1_APISET, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegDeleteValueW")]
13+
internal static extern int RegDeleteValue(SafeRegistryHandle hKey, String lpValueName);
14+
}
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Win32.SafeHandles;
5+
using System;
6+
using System.Runtime.InteropServices;
7+
using System.Text;
8+
9+
internal partial class Interop
10+
{
11+
internal partial class mincore
12+
{
13+
[DllImport(Libraries.REGISTRY_L1_APISET, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegEnumKeyExW")]
14+
internal unsafe static extern int RegEnumKeyEx(
15+
SafeRegistryHandle hKey,
16+
int dwIndex,
17+
char* lpName,
18+
ref int lpcbName,
19+
int[] lpReserved,
20+
[Out]StringBuilder lpClass,
21+
int[] lpcbClass,
22+
long[] lpftLastWriteTime);
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Win32.SafeHandles;
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
internal partial class Interop
9+
{
10+
internal partial class mincore
11+
{
12+
[DllImport(Libraries.REGISTRY_L1_APISET, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegEnumValueW")]
13+
internal unsafe static extern int RegEnumValue(
14+
SafeRegistryHandle hKey,
15+
int dwIndex,
16+
char* lpValueName,
17+
ref int lpcbValueName,
18+
IntPtr lpReserved_MustBeZero,
19+
int[] lpType,
20+
byte[] lpData,
21+
int[] lpcbData);
22+
}
23+
}

0 commit comments

Comments
 (0)