Skip to content

Commit 5b4a34f

Browse files
authored
Replace DllImport with LibraryImport in SMA 4 (PowerShell#18579)
1 parent ea428a6 commit 5b4a34f

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#nullable enable
5+
6+
#if !UNIX
7+
using System.Diagnostics.CodeAnalysis;
8+
using System.Runtime.InteropServices;
9+
10+
internal static partial class Interop
11+
{
12+
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Keep native struct names.")]
13+
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter", Justification = "Keep native struct names.")]
14+
internal static partial class Windows
15+
{
16+
[StructLayout(LayoutKind.Sequential)]
17+
internal unsafe struct SHFILEINFO
18+
{
19+
internal nint hIcon;
20+
internal int iIcon;
21+
internal uint dwAttributes;
22+
internal fixed char szDisplayName[260];
23+
internal fixed char szTypeName[80];
24+
25+
public static readonly uint s_Size = (uint)sizeof(SHFILEINFO);
26+
}
27+
28+
[LibraryImport("shell32.dll", EntryPoint = "SHGetFileInfoW", StringMarshalling = StringMarshalling.Utf16)]
29+
internal static partial nint SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
30+
31+
internal static int SHGetFileInfo(string pszPath)
32+
{
33+
// flag used to ask to return exe type
34+
const uint SHGFI_EXETYPE = 0x000002000;
35+
var shinfo = new SHFILEINFO();
36+
return (int)SHGetFileInfo(pszPath, 0, ref shinfo, SHFILEINFO.s_Size, SHGFI_EXETYPE);
37+
}
38+
}
39+
}
40+
#endif

src/System.Management.Automation/engine/NativeCommandProcessor.cs

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,9 @@ private static bool IsWindowsApplication(string fileName)
11061106
return false;
11071107
}
11081108

1109-
SHFILEINFO shinfo = new SHFILEINFO();
1110-
IntPtr type = SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_EXETYPE);
1109+
int type = Interop.Windows.SHGetFileInfo(fileName);
11111110

1112-
switch ((int)type)
1111+
switch (type)
11131112
{
11141113
case 0x0:
11151114
// 0x0 = not an exe
@@ -1640,38 +1639,6 @@ private static string FindExecutable(string filename)
16401639

16411640
#endregion
16421641

1643-
#region Interop for SHGetFileInfo
1644-
1645-
private const int SCS_32BIT_BINARY = 0; // A 32-bit Windows-based application
1646-
private const int SCS_DOS_BINARY = 1; // An MS-DOS - based application
1647-
private const int SCS_WOW_BINARY = 2; // A 16-bit Windows-based application
1648-
private const int SCS_PIF_BINARY = 3; // A PIF file that executes an MS-DOS - based application
1649-
private const int SCS_POSIX_BINARY = 4; // A POSIX - based application
1650-
private const int SCS_OS216_BINARY = 5; // A 16-bit OS/2-based application
1651-
private const int SCS_64BIT_BINARY = 6; // A 64-bit Windows-based application.
1652-
1653-
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
1654-
private struct SHFILEINFO
1655-
{
1656-
public IntPtr hIcon;
1657-
public int iIcon;
1658-
public uint dwAttributes;
1659-
1660-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
1661-
public string szDisplayName;
1662-
1663-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
1664-
public string szTypeName;
1665-
}
1666-
1667-
private const uint SHGFI_EXETYPE = 0x000002000; // flag used to ask to return exe type
1668-
1669-
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
1670-
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes,
1671-
ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
1672-
1673-
#endregion
1674-
16751642
#region Minishell Interop
16761643

16771644
private bool _isMiniShell = false;

0 commit comments

Comments
 (0)