Skip to content

Commit 19325b6

Browse files
moooyoYu Leng
authored andcommitted
[AOT] move some LibraryImport define to common to avoid dup code (#40006)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request To avoid dup code, try to move some LibraryImport define to common folder. This can make other modules/extensions easy to use. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] **Closes:** #xxx - [x] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed --------- Co-authored-by: Yu Leng <yuleng@microsoft.com>
1 parent c61cab0 commit 19325b6

30 files changed

+429
-408
lines changed

PowerToys.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkspacesEditorUITest", "s
723723
EndProject
724724
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CalculatorEngineCommon", "src\common\CalculatorEngineCommon\CalculatorEngineCommon.vcxproj", "{2CF78CF7-8FEB-4BE1-9591-55FA25B48FC6}"
725725
EndProject
726+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManagedCsWin32", "src\common\ManagedCsWin32\ManagedCsWin32.csproj", "{14AFD976-B4D2-49D0-9E6C-AA93CC061B8A}"
727+
EndProject
726728
Global
727729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
728730
Debug|ARM64 = Debug|ARM64
@@ -2649,6 +2651,14 @@ Global
26492651
{2CF78CF7-8FEB-4BE1-9591-55FA25B48FC6}.Release|ARM64.Build.0 = Release|ARM64
26502652
{2CF78CF7-8FEB-4BE1-9591-55FA25B48FC6}.Release|x64.ActiveCfg = Release|x64
26512653
{2CF78CF7-8FEB-4BE1-9591-55FA25B48FC6}.Release|x64.Build.0 = Release|x64
2654+
{14AFD976-B4D2-49D0-9E6C-AA93CC061B8A}.Debug|ARM64.ActiveCfg = Debug|ARM64
2655+
{14AFD976-B4D2-49D0-9E6C-AA93CC061B8A}.Debug|ARM64.Build.0 = Debug|ARM64
2656+
{14AFD976-B4D2-49D0-9E6C-AA93CC061B8A}.Debug|x64.ActiveCfg = Debug|x64
2657+
{14AFD976-B4D2-49D0-9E6C-AA93CC061B8A}.Debug|x64.Build.0 = Debug|x64
2658+
{14AFD976-B4D2-49D0-9E6C-AA93CC061B8A}.Release|ARM64.ActiveCfg = Release|ARM64
2659+
{14AFD976-B4D2-49D0-9E6C-AA93CC061B8A}.Release|ARM64.Build.0 = Release|ARM64
2660+
{14AFD976-B4D2-49D0-9E6C-AA93CC061B8A}.Release|x64.ActiveCfg = Release|x64
2661+
{14AFD976-B4D2-49D0-9E6C-AA93CC061B8A}.Release|x64.Build.0 = Release|x64
26522662
EndGlobalSection
26532663
GlobalSection(SolutionProperties) = preSolution
26542664
HideSolutionNode = FALSE
@@ -2926,6 +2936,7 @@ Global
29262936
{5F63C743-F6CE-4DBA-A200-2B3F8A14E8C2} = {3846508C-77EB-4034-A702-F8BB263C4F79}
29272937
{2694E2FB-DCD5-4BFF-A418-B6C3C7CE3B8E} = {89E20BCE-EB9C-46C8-8B50-E01A82E6FDC3}
29282938
{2CF78CF7-8FEB-4BE1-9591-55FA25B48FC6} = {1AFB6476-670D-4E80-A464-657E01DFF482}
2939+
{14AFD976-B4D2-49D0-9E6C-AA93CC061B8A} = {1AFB6476-670D-4E80-A464-657E01DFF482}
29292940
EndGlobalSection
29302941
GlobalSection(ExtensibilityGlobals) = postSolution
29312942
SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0}

src/common/ManagedCsWin32/CLSID.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace ManagedCsWin32;
12+
13+
public static partial class CLSID
14+
{
15+
public static readonly Guid SearchManager = new Guid("7D096C5F-AC08-4F1F-BEB7-5C22C517CE39");
16+
public static readonly Guid CollatorDataSource = new Guid("9E175B8B-F52A-11D8-B9A5-505054503030");
17+
public static readonly Guid ApplicationActivationManager = new Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C");
18+
public static readonly Guid VirtualDesktopManager = new("aa509086-5ca9-4c25-8f95-589d3c07b48a");
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
using System.Runtime.InteropServices.Marshalling;
8+
9+
namespace ManagedCsWin32;
10+
11+
public static class ComHelper
12+
{
13+
private static StrategyBasedComWrappers cw = new StrategyBasedComWrappers();
14+
15+
public static T CreateComInstance<T>(ref Guid rclsid, CLSCTX dwClsContext)
16+
{
17+
var riid = typeof(T).GUID;
18+
19+
var hr = Ole32.CoCreateInstance(ref rclsid, IntPtr.Zero, dwClsContext, ref riid, out IntPtr comPtr);
20+
if (hr != 0)
21+
{
22+
throw new ArgumentException($"Failed to create {typeof(T).Name} instance. HR: {hr}");
23+
}
24+
25+
if (comPtr == IntPtr.Zero)
26+
{
27+
throw new ArgumentException($"Failed to create {typeof(T).Name} instance. CoCreateInstance return null ptr.");
28+
}
29+
30+
try
31+
{
32+
var comObject = cw.GetOrCreateObjectForComInstance(comPtr, CreateObjectFlags.None);
33+
if (comObject == null)
34+
{
35+
throw new ArgumentException($"Failed to create {typeof(T).Name} instance. Cast error.");
36+
}
37+
38+
return (T)comObject;
39+
}
40+
finally
41+
{
42+
Marshal.Release(comPtr);
43+
}
44+
}
45+
}

src/common/ManagedCsWin32/IID.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace ManagedCsWin32;
12+
13+
public static partial class IID
14+
{
15+
public static readonly Guid ISearchManager = new Guid("AB310581-AC80-11D1-8DF3-00C04FB6EF69");
16+
public static readonly Guid IPropertyStore = new Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99");
17+
public static readonly Guid IApplicationActivationManager = new Guid("2e941141-7f97-4756-ba1d-9decde894a3d");
18+
public static readonly Guid IVirtualDesktopManager = new("a5cd92ff-29be-454c-8d04-d82879fb3f1b");
19+
}

src/common/ManagedCsWin32/Kernel32.cs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
namespace ManagedCsWin32;
9+
10+
public static partial class Kernel32
11+
{
12+
[LibraryImport("kernel32.dll", SetLastError = true)]
13+
[return: MarshalAs(UnmanagedType.Bool)]
14+
public static partial bool DeviceIoControl(
15+
IntPtr hDevice,
16+
uint dwIoControlCode,
17+
IntPtr inBuffer,
18+
int nInBufferSize,
19+
IntPtr outBuffer,
20+
int nOutBufferSize,
21+
out int pBytesReturned,
22+
IntPtr lpOverlapped);
23+
24+
[LibraryImport("kernel32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
25+
public static partial int CreateFile(
26+
string lpFileName,
27+
FileAccessType dwDesiredAccess,
28+
FileShareType dwShareMode,
29+
IntPtr lpSecurityAttributes,
30+
CreationDisposition dwCreationDisposition,
31+
FileAttributes dwFlagsAndAttributes,
32+
IntPtr hTemplateFile);
33+
}
34+
35+
[Flags]
36+
public enum FileAccessType : uint
37+
{
38+
DELETE = 0x00010000,
39+
READ_CONTROL = 0x00020000,
40+
WRITE_DAC = 0x00040000,
41+
WRITE_OWNER = 0x00080000,
42+
SYNCHRONIZE = 0x00100000,
43+
44+
STANDARD_RIGHTS_REQUIRED = 0x000F0000,
45+
46+
STANDARD_RIGHTS_READ = READ_CONTROL,
47+
STANDARD_RIGHTS_WRITE = READ_CONTROL,
48+
STANDARD_RIGHTS_EXECUTE = READ_CONTROL,
49+
50+
STANDARD_RIGHTS_ALL = 0x001F0000,
51+
52+
SPECIFIC_RIGHTS_ALL = 0x0000FFFF,
53+
54+
ACCESS_SYSTEM_SECURITY = 0x01000000,
55+
56+
MAXIMUM_ALLOWED = 0x02000000,
57+
58+
GENERIC_READ = 0x80000000,
59+
GENERIC_WRITE = 0x40000000,
60+
GENERIC_EXECUTE = 0x20000000,
61+
GENERIC_ALL = 0x10000000,
62+
63+
FILE_READ_DATA = 0x0001,
64+
FILE_WRITE_DATA = 0x0002,
65+
FILE_APPEND_DATA = 0x0004,
66+
FILE_READ_EA = 0x0008,
67+
FILE_WRITE_EA = 0x0010,
68+
FILE_EXECUTE = 0x0020,
69+
FILE_READ_ATTRIBUTES = 0x0080,
70+
FILE_WRITE_ATTRIBUTES = 0x0100,
71+
72+
FILE_ALL_ACCESS =
73+
STANDARD_RIGHTS_REQUIRED |
74+
SYNCHRONIZE
75+
| 0x1FF,
76+
77+
FILE_GENERIC_READ =
78+
STANDARD_RIGHTS_READ |
79+
FILE_READ_DATA |
80+
FILE_READ_ATTRIBUTES |
81+
FILE_READ_EA |
82+
SYNCHRONIZE,
83+
84+
FILE_GENERIC_WRITE =
85+
STANDARD_RIGHTS_WRITE |
86+
FILE_WRITE_DATA |
87+
FILE_WRITE_ATTRIBUTES |
88+
FILE_WRITE_EA |
89+
FILE_APPEND_DATA |
90+
SYNCHRONIZE,
91+
92+
FILE_GENERIC_EXECUTE =
93+
STANDARD_RIGHTS_EXECUTE |
94+
FILE_READ_ATTRIBUTES |
95+
FILE_EXECUTE |
96+
SYNCHRONIZE,
97+
}
98+
99+
[Flags]
100+
public enum FileShareType : uint
101+
{
102+
None = 0x00000000,
103+
Read = 0x00000001,
104+
Write = 0x00000002,
105+
Delete = 0x00000004,
106+
}
107+
108+
public enum CreationDisposition : uint
109+
{
110+
New = 1,
111+
CreateAlways = 2,
112+
OpenExisting = 3,
113+
OpenAlways = 4,
114+
TruncateExisting = 5,
115+
}
116+
117+
[Flags]
118+
public enum FileAttributes : uint
119+
{
120+
Readonly = 0x00000001,
121+
Hidden = 0x00000002,
122+
System = 0x00000004,
123+
Directory = 0x00000010,
124+
Archive = 0x00000020,
125+
Device = 0x00000040,
126+
Normal = 0x00000080,
127+
Temporary = 0x00000100,
128+
SparseFile = 0x00000200,
129+
ReparsePoint = 0x00000400,
130+
Compressed = 0x00000800,
131+
Offline = 0x00001000,
132+
NotContentIndexed = 0x00002000,
133+
Encrypted = 0x00004000,
134+
Write_Through = 0x80000000,
135+
Overlapped = 0x40000000,
136+
NoBuffering = 0x20000000,
137+
RandomAccess = 0x10000000,
138+
SequentialScan = 0x08000000,
139+
DeleteOnClose = 0x04000000,
140+
BackupSemantics = 0x02000000,
141+
PosixSemantics = 0x01000000,
142+
OpenReparsePoint = 0x00200000,
143+
OpenNoRecall = 0x00100000,
144+
FirstPipeInstance = 0x00080000,
145+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<!-- Look at Directory.Build.props in root for common stuff as well -->
3+
<Import Project="..\..\.\Common.Dotnet.CsWinRT.props" />
4+
5+
<PropertyGroup>
6+
<Description>PowerToys ManagedCsWin32</Description>
7+
<AssemblyName>PowerToys.ManagedCsWin32</AssemblyName>
8+
</PropertyGroup>
9+
</Project>

src/common/ManagedCsWin32/Ole32.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
namespace ManagedCsWin32;
9+
10+
public static partial class Ole32
11+
{
12+
[LibraryImport("ole32.dll")]
13+
public static partial int CoCreateInstance(
14+
ref Guid rclsid,
15+
IntPtr pUnkOuter,
16+
CLSCTX dwClsContext,
17+
ref Guid riid,
18+
out IntPtr rReturnedComObject);
19+
}
20+
21+
[Flags]
22+
public enum CLSCTX : uint
23+
{
24+
InProcServer = 0x1,
25+
InProcHandler = 0x2,
26+
LocalServer = 0x4,
27+
InProcServer16 = 0x8,
28+
RemoteServer = 0x10,
29+
InProcHandler16 = 0x20,
30+
Reserved1 = 0x40,
31+
Reserved2 = 0x80,
32+
Reserved3 = 0x100,
33+
Reserved4 = 0x200,
34+
NoCodeDownload = 0x400,
35+
Reserved5 = 0x800,
36+
NoCustomMarshal = 0x1000,
37+
EnableCodeDownload = 0x2000,
38+
NoFailureLog = 0x4000,
39+
DisableAAA = 0x8000,
40+
EnableAAA = 0x10000,
41+
FromDefaultContext = 0x20000,
42+
ActivateX86Server = 0x40000,
43+
#pragma warning disable CA1069 // Keep the original defines for compatibility
44+
Activate32BitServer = 0x40000, // Same as ActivateX86Server
45+
#pragma warning restore CA1069 // Keep the original defines for compatibility
46+
Activate64BitServer = 0x80000,
47+
EnableCloaking = 0x100000,
48+
AppContainer = 0x400000,
49+
ActivateAAAAsIU = 0x800000,
50+
Reserved6 = 0x1000000,
51+
ActivateARM32Server = 0x2000000,
52+
AllowLowerTrustRegistration = 0x4000000,
53+
PSDll = 0x80000000,
54+
55+
INPROC = InProcServer | InProcHandler,
56+
SERVER = InProcServer | LocalServer | RemoteServer,
57+
ALL = InProcHandler | SERVER,
58+
}

src/common/ManagedCsWin32/Shell32.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
namespace ManagedCsWin32;
9+
10+
public static partial class Shell32
11+
{
12+
[LibraryImport("SHELL32.dll", EntryPoint = "ShellExecuteExW", SetLastError = true)]
13+
[return: MarshalAs(UnmanagedType.Bool)]
14+
public static partial bool ShellExecuteEx(ref SHELLEXECUTEINFOW lpExecInfo);
15+
16+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
17+
public struct SHELLEXECUTEINFOW
18+
{
19+
public uint CbSize;
20+
public uint FMask;
21+
public IntPtr Hwnd;
22+
23+
public IntPtr LpVerb;
24+
public IntPtr LpFile;
25+
public IntPtr LpParameters;
26+
public IntPtr LpDirectory;
27+
public int Show;
28+
public IntPtr HInstApp;
29+
public IntPtr LpIDList;
30+
public IntPtr LpClass;
31+
public IntPtr HkeyClass;
32+
public uint DwHotKey;
33+
public IntPtr HIconOrMonitor;
34+
public IntPtr Process;
35+
}
36+
}

src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Microsoft.CmdPal.Ext.Apps.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<PackageReference Include="WyHash" />
2121

2222
<ProjectReference Include="..\..\..\..\common\ManagedCommon\ManagedCommon.csproj" />
23-
23+
<ProjectReference Include="..\..\..\..\common\ManagedCsWin32\ManagedCsWin32.csproj" />
2424
<ProjectReference Include="..\..\extensionsdk\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.csproj" />
2525
</ItemGroup>
2626

0 commit comments

Comments
 (0)