Skip to content

Commit b846527

Browse files
authored
Replace DllImport with LibraryImport in SMA 1 (PowerShell#18520)
1 parent 9f459c0 commit b846527

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace System.Management.Automation
3030
{
3131
/// <summary>
3232
/// </summary>
33-
public static class CompletionCompleters
33+
public static partial class CompletionCompleters
3434
{
3535
static CompletionCompleters()
3636
{
@@ -4751,8 +4751,8 @@ private struct SHARE_INFO_1
47514751
AttributesToSkip = 0 // Default is to skip Hidden and System files, so we clear this to retain existing behavior
47524752
};
47534753

4754-
[DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
4755-
private static extern int NetShareEnum(string serverName, int level, out IntPtr bufptr, int prefMaxLen,
4754+
[LibraryImport("Netapi32.dll", StringMarshalling = StringMarshalling.Utf16)]
4755+
private static partial int NetShareEnum(string serverName, int level, out IntPtr bufptr, int prefMaxLen,
47564756
out uint entriesRead, out uint totalEntries, ref uint resumeHandle);
47574757

47584758
internal static List<string> GetFileShares(string machine, bool ignoreHidden)

src/System.Management.Automation/namespaces/FileSystemProvider.cs

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7150,7 +7150,7 @@ internal static bool WinPathIsNetworkPath(string path)
71507150
return NativeMethods.PathIsNetworkPath(path); // call the native method
71517151
}
71527152

7153-
private static class NativeMethods
7153+
private static partial class NativeMethods
71547154
{
71557155
/// <summary>
71567156
/// WNetAddConnection2 API makes a connection to a network resource
@@ -7196,8 +7196,8 @@ private static class NativeMethods
71967196
/// else the error code describing the type of failure that occurred while
71977197
/// trying to remove the connection is returned.
71987198
/// </returns>
7199-
[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
7200-
internal static extern int WNetCancelConnection2(string driveName, int flags, bool force);
7199+
[LibraryImport("mpr.dll", EntryPoint ="WNetCancelConnection2W", StringMarshalling = StringMarshalling.Utf16)]
7200+
internal static partial int WNetCancelConnection2(string driveName, int flags, [MarshalAs(UnmanagedType.Bool)] bool force);
72017201

72027202
/// <summary>
72037203
/// WNetGetConnection function retrieves the name of the network resource associated with a local device.
@@ -7223,8 +7223,8 @@ private static class NativeMethods
72237223
/// Path of the file being executed
72247224
/// </param>
72257225
/// <returns>Returns 0 through 25 (corresponding to 'A' through 'Z') if the path has a drive letter, or -1 otherwise.</returns>
7226-
[DllImport("api-ms-win-core-shlwapi-legacy-l1-1-0.dll", CharSet = CharSet.Unicode)]
7227-
internal static extern int PathGetDriveNumber(string path);
7226+
[LibraryImport("api-ms-win-core-shlwapi-legacy-l1-1-0.dll", EntryPoint ="PathGetDriveNumberW", StringMarshalling = StringMarshalling.Utf16)]
7227+
internal static partial int PathGetDriveNumber(string path);
72287228

72297229
private static bool _WNetApiAvailable = true;
72307230

@@ -7288,9 +7288,9 @@ internal static bool PathIsNetworkPath(string path)
72887288
/// Path of the file being executed.
72897289
/// </param>
72907290
/// <returns>True if the path is a network path or else returns false.</returns>
7291-
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
7291+
[LibraryImport("shlwapi.dll", EntryPoint = "PathIsNetworkPathW", StringMarshalling = StringMarshalling.Utf16)]
72927292
[return: MarshalAs(UnmanagedType.Bool)]
7293-
internal static extern bool PathIsNetworkPath(string path);
7293+
internal static partial bool PathIsNetworkPath(string path);
72947294
#endif
72957295

72967296
/// <summary>
@@ -7320,9 +7320,9 @@ internal static bool PathIsNetworkPath(string path)
73207320
/// <param name="destination">Path of the target of the symbolic link.</param>
73217321
/// <param name="symbolicLinkFlags">Flag values from SymbolicLinkFlags enum.</param>
73227322
/// <returns>1 on successful creation.</returns>
7323-
[DllImport(PinvokeDllNames.CreateSymbolicLinkDllName, CharSet = CharSet.Unicode, SetLastError = true)]
7323+
[LibraryImport(PinvokeDllNames.CreateSymbolicLinkDllName, EntryPoint = "CreateSymbolicLinkW", StringMarshalling = StringMarshalling.Utf16)]
73247324
[return: MarshalAs(UnmanagedType.I1)]
7325-
internal static extern bool CreateSymbolicLink(string name, string destination, SymbolicLinkFlags symbolicLinkFlags);
7325+
internal static partial bool CreateSymbolicLink(string name, string destination, SymbolicLinkFlags symbolicLinkFlags);
73267326

73277327
/// <summary>
73287328
/// Flags used when creating a symbolic link.
@@ -7353,26 +7353,26 @@ internal enum SymbolicLinkFlags
73537353
/// <param name="existingFileName">Path to the target of the hard link.</param>
73547354
/// <param name="SecurityAttributes"></param>
73557355
/// <returns></returns>
7356-
[DllImport(PinvokeDllNames.CreateHardLinkDllName, CharSet = CharSet.Unicode, SetLastError = true)]
7356+
[LibraryImport(PinvokeDllNames.CreateHardLinkDllName, EntryPoint = "CreateHardLinkW", StringMarshalling = StringMarshalling.Utf16)]
73577357
[return: MarshalAs(UnmanagedType.Bool)]
7358-
internal static extern bool CreateHardLink(string name, string existingFileName, IntPtr SecurityAttributes);
7358+
internal static partial bool CreateHardLink(string name, string existingFileName, IntPtr SecurityAttributes);
73597359

73607360
// OneDrive placeholder support
73617361
#if !UNIX
73627362
/// <summary>
73637363
/// Returns the placeholder compatibility mode for the current process.
73647364
/// </summary>
73657365
/// <returns>The process's placeholder compatibily mode (PHCM_xxx), or a negative value on error (PCHM_ERROR_xxx).</returns>
7366-
[DllImport("ntdll.dll")]
7367-
internal static extern sbyte RtlQueryProcessPlaceholderCompatibilityMode();
7366+
[LibraryImport("ntdll.dll")]
7367+
internal static partial sbyte RtlQueryProcessPlaceholderCompatibilityMode();
73687368

73697369
/// <summary>
73707370
/// Sets the placeholder compatibility mode for the current process.
73717371
/// </summary>
73727372
/// <param name="pcm">The placeholder compatibility mode to set.</param>
73737373
/// <returns>The process's previous placeholder compatibily mode (PHCM_xxx), or a negative value on error (PCHM_ERROR_xxx).</returns>
7374-
[DllImport("ntdll.dll")]
7375-
internal static extern sbyte RtlSetProcessPlaceholderCompatibilityMode(sbyte pcm);
7374+
[LibraryImport("ntdll.dll")]
7375+
internal static partial sbyte RtlSetProcessPlaceholderCompatibilityMode(sbyte pcm);
73767376

73777377
internal const sbyte PHCM_APPLICATION_DEFAULT = 0;
73787378
internal const sbyte PHCM_DISGUISE_PLACEHOLDER = 1;
@@ -7870,7 +7870,7 @@ public class FileSystemProviderRemoveItemDynamicParameters
78707870
/// <summary>
78717871
/// Class to find the symbolic link target.
78727872
/// </summary>
7873-
public static class InternalSymbolicLinkLinkCodeMethods
7873+
public static partial class InternalSymbolicLinkLinkCodeMethods
78747874
{
78757875
// This size comes from measuring the size of the header of REPARSE_GUID_DATA_BUFFER
78767876
private const int REPARSE_GUID_DATA_BUFFER_HEADER_SIZE = 24;
@@ -7984,9 +7984,9 @@ private struct REPARSE_DATA_BUFFER_MOUNTPOINT
79847984
private struct BY_HANDLE_FILE_INFORMATION
79857985
{
79867986
public uint FileAttributes;
7987-
public System.Runtime.InteropServices.ComTypes.FILETIME CreationTime;
7988-
public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
7989-
public System.Runtime.InteropServices.ComTypes.FILETIME LastWriteTime;
7987+
public FILE_TIME CreationTime;
7988+
public FILE_TIME LastAccessTime;
7989+
public FILE_TIME LastWriteTime;
79907990
public uint VolumeSerialNumber;
79917991
public uint FileSizeHigh;
79927992
public uint FileSizeLow;
@@ -7995,6 +7995,12 @@ private struct BY_HANDLE_FILE_INFORMATION
79957995
public uint FileIndexLow;
79967996
}
79977997

7998+
internal struct FILE_TIME
7999+
{
8000+
public uint dwLowDateTime;
8001+
public uint dwHighDateTime;
8002+
}
8003+
79988004
[StructLayout(LayoutKind.Sequential)]
79998005
private struct GUID
80008006
{
@@ -8018,20 +8024,21 @@ private struct REPARSE_GUID_DATA_BUFFER
80188024
public char[] DataBuffer;
80198025
}
80208026

8021-
[DllImport(PinvokeDllNames.DeviceIoControlDllName, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
8022-
private static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode,
8027+
[LibraryImport(PinvokeDllNames.DeviceIoControlDllName, StringMarshalling = StringMarshalling.Utf16, SetLastError = true)]
8028+
[return: MarshalAs(UnmanagedType.Bool)]
8029+
private static partial bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode,
80238030
IntPtr InBuffer, int nInBufferSize,
80248031
IntPtr OutBuffer, int nOutBufferSize,
80258032
out int pBytesReturned, IntPtr lpOverlapped);
80268033

8027-
[DllImport(PinvokeDllNames.GetFileInformationByHandleDllName, SetLastError = true, CharSet = CharSet.Unicode)]
8034+
[LibraryImport(PinvokeDllNames.GetFileInformationByHandleDllName)]
80288035
[return: MarshalAs(UnmanagedType.Bool)]
8029-
private static extern bool GetFileInformationByHandle(
8036+
private static partial bool GetFileInformationByHandle(
80308037
IntPtr hFile,
80318038
out BY_HANDLE_FILE_INFORMATION lpFileInformation);
80328039

8033-
[DllImport(PinvokeDllNames.CreateFileDllName, SetLastError = true, CharSet = CharSet.Unicode)]
8034-
internal static extern IntPtr CreateFile(
8040+
[LibraryImport(PinvokeDllNames.CreateFileDllName, EntryPoint = "CreateFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
8041+
internal static partial IntPtr CreateFile(
80358042
string lpFileName,
80368043
FileDesiredAccess dwDesiredAccess,
80378044
FileShareMode dwShareMode,
@@ -8040,7 +8047,7 @@ internal static extern IntPtr CreateFile(
80408047
FileAttributes dwFlagsAndAttributes,
80418048
IntPtr hTemplateFile);
80428049

8043-
internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
8050+
internal sealed partial class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
80448051
{
80458052
private SafeFindHandle() : base(true) { }
80468053

@@ -8049,23 +8056,23 @@ protected override bool ReleaseHandle()
80498056
return FindClose(this.handle);
80508057
}
80518058

8052-
[DllImport(PinvokeDllNames.FindCloseDllName)]
8059+
[LibraryImport(PinvokeDllNames.FindCloseDllName)]
80538060
[return: MarshalAs(UnmanagedType.Bool)]
8054-
private static extern bool FindClose(IntPtr handle);
8061+
private static partial bool FindClose(IntPtr handle);
80558062
}
80568063

80578064
// We use 'FindFirstFileW' instead of 'FindFirstFileExW' because the latter doesn't work correctly with Unicode file names on FAT32.
80588065
// See https://github.com/PowerShell/PowerShell/issues/16804
8059-
[DllImport(PinvokeDllNames.FindFirstFileDllName, EntryPoint = "FindFirstFileW", SetLastError = true, CharSet = CharSet.Unicode)]
8060-
private static extern SafeFindHandle FindFirstFile(string lpFileName, ref WIN32_FIND_DATA lpFindFileData);
8066+
[LibraryImport(PinvokeDllNames.FindFirstFileDllName, EntryPoint = "FindFirstFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
8067+
private static partial SafeFindHandle FindFirstFile(string lpFileName, ref WIN32_FIND_DATA lpFindFileData);
80618068

80628069
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
80638070
internal unsafe struct WIN32_FIND_DATA
80648071
{
80658072
internal uint dwFileAttributes;
8066-
internal System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
8067-
internal System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
8068-
internal System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
8073+
internal FILE_TIME ftCreationTime;
8074+
internal FILE_TIME ftLastAccessTime;
8075+
internal FILE_TIME ftLastWriteTime;
80698076
internal uint nFileSizeHigh;
80708077
internal uint nFileSizeLow;
80718078
internal uint dwReserved0;
@@ -8539,7 +8546,7 @@ public class AlternateStreamData
85398546
/// </summary>
85408547
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes",
85418548
Justification = "Needed by both the FileSystem provider and Unblock-File cmdlet.")]
8542-
public static class AlternateDataStreamUtilities
8549+
public static partial class AlternateDataStreamUtilities
85438550
{
85448551
/// <summary>
85458552
/// List all of the streams on a file.
@@ -8704,15 +8711,15 @@ internal static void SetZoneOfOrigin(string path, SecurityZone securityZone)
87048711
// the code above seems cleaner and more robust than the IAttachmentExecute approach
87058712
}
87068713

8707-
internal static class NativeMethods
8714+
internal static partial class NativeMethods
87088715
{
87098716
internal const int ERROR_HANDLE_EOF = 38;
87108717
internal const int ERROR_INVALID_PARAMETER = 87;
87118718

87128719
internal enum StreamInfoLevels { FindStreamInfoStandard = 0 }
87138720

8714-
[DllImport(PinvokeDllNames.CreateFileDllName, CharSet = CharSet.Unicode, SetLastError = true)]
8715-
internal static extern SafeFileHandle CreateFile(string lpFileName,
8721+
[LibraryImport(PinvokeDllNames.CreateFileDllName, EntryPoint = "CreateFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
8722+
internal static partial SafeFileHandle CreateFile(string lpFileName,
87168723
FileAccess dwDesiredAccess, FileShare dwShareMode,
87178724
IntPtr lpSecurityAttributes, FileMode dwCreationDisposition,
87188725
int dwFlagsAndAttributes, IntPtr hTemplateFile);
@@ -8733,7 +8740,7 @@ internal static extern bool FindNextStreamW(
87338740
AlternateStreamNativeData lpFindStreamData);
87348741
}
87358742

8736-
internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
8743+
internal sealed partial class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
87378744
{
87388745
private SafeFindHandle() : base(true) { }
87398746

@@ -8742,9 +8749,9 @@ protected override bool ReleaseHandle()
87428749
return FindClose(this.handle);
87438750
}
87448751

8745-
[DllImport(PinvokeDllNames.FindCloseDllName)]
8752+
[LibraryImport(PinvokeDllNames.FindCloseDllName)]
87468753
[return: MarshalAs(UnmanagedType.Bool)]
8747-
private static extern bool FindClose(IntPtr handle);
8754+
private static partial bool FindClose(IntPtr handle);
87488755
}
87498756

87508757
/// <summary>

src/System.Management.Automation/utils/ClrFacade.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace System.Management.Automation
1919
/// ClrFacade contains all diverging code (different implementation for FullCLR and CoreCLR using if/def).
2020
/// It exposes common APIs that can be used by the rest of the code base.
2121
/// </summary>
22-
internal static class ClrFacade
22+
internal static partial class ClrFacade
2323
{
2424
/// <summary>
2525
/// Initialize powershell AssemblyLoadContext and register the 'Resolving' event, if it's not done already.
@@ -366,13 +366,13 @@ internal static string ToDmtfDateTime(DateTime date)
366366
/// <summary>
367367
/// Native methods that are used by facade methods.
368368
/// </summary>
369-
private static class NativeMethods
369+
private static partial class NativeMethods
370370
{
371371
/// <summary>
372372
/// Pinvoke for GetOEMCP to get the OEM code page.
373373
/// </summary>
374-
[DllImport(PinvokeDllNames.GetOEMCPDllName, SetLastError = false, CharSet = CharSet.Unicode)]
375-
internal static extern uint GetOEMCP();
374+
[LibraryImport(PinvokeDllNames.GetOEMCPDllName)]
375+
internal static partial uint GetOEMCP();
376376
}
377377
}
378378
}

0 commit comments

Comments
 (0)