2
2
// The Microsoft Corporation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ using System . Runtime . InteropServices ;
6
+ using System . Runtime . Versioning ;
5
7
using CommunityToolkit . Common ;
6
8
using CommunityToolkit . Mvvm . ComponentModel ;
7
9
using CommunityToolkit . Mvvm . Input ;
13
15
using Microsoft . CmdPal . UI . ViewModels . Models ;
14
16
using Microsoft . CommandPalette . Extensions ;
15
17
using Microsoft . Extensions . DependencyInjection ;
16
- using Windows . Win32 ;
18
+ using WinRT ;
17
19
18
20
namespace Microsoft . CmdPal . UI . ViewModels ;
19
21
@@ -148,14 +150,16 @@ public void SetActiveExtension(IExtensionWrapper? extension)
148
150
// need to handle that
149
151
_activeExtension = extension ;
150
152
151
- var extensionComObject = _activeExtension ? . GetExtensionObject ( ) ;
152
- if ( extensionComObject != null )
153
+ var extensionWinRtObject = _activeExtension ? . GetExtensionObject ( ) ;
154
+ if ( extensionWinRtObject != null )
153
155
{
154
156
try
155
157
{
156
158
unsafe
157
159
{
158
- var hr = PInvoke . CoAllowSetForegroundWindow ( extensionComObject ) ;
160
+ var winrtObj = ( IWinRTObject ) extensionWinRtObject ;
161
+ var intPtr = winrtObj . NativeObject . ThisPtr ;
162
+ var hr = Native . CoAllowSetForegroundWindow ( intPtr ) ;
159
163
if ( hr != 0 )
160
164
{
161
165
Logger . LogWarning ( $ "Error giving foreground rights: 0x{ hr . Value : X8} ") ;
@@ -174,4 +178,18 @@ public void GoHome()
174
178
{
175
179
SetActiveExtension ( null ) ;
176
180
}
181
+
182
+ // You may ask yourself, why aren't we using CsWin32 for this?
183
+ // The CsWin32 projected version includes some object marshalling, like so:
184
+ //
185
+ // HRESULT CoAllowSetForegroundWindow([MarshalAs(UnmanagedType.IUnknown)] object pUnk,...)
186
+ //
187
+ // And if you do it like that, then the IForegroundTransfer interface isn't marshalled correctly
188
+ internal sealed class Native
189
+ {
190
+ [ DllImport ( "OLE32.dll" , ExactSpelling = true ) ]
191
+ [ DefaultDllImportSearchPaths ( DllImportSearchPath . System32 ) ]
192
+ [ SupportedOSPlatform ( "windows5.0" ) ]
193
+ internal static extern unsafe global ::Windows . Win32 . Foundation . HRESULT CoAllowSetForegroundWindow ( nint pUnk , [ Optional ] void * lpvReserved ) ;
194
+ }
177
195
}
0 commit comments