Skip to content

Commit 0b86e85

Browse files
committed
Update
1 parent e3fee27 commit 0b86e85

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

src/Files.App/Utils/Shell/PreviewHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ unsafe void SetupHandler(Guid clsid)
170170
null,
171171
CLSCTX.CLSCTX_LOCAL_SERVER,
172172
ref iid,
173-
out object previewHandlerObject);
173+
out void* previewHandlerPtr);
174+
175+
object previewHandlerObject = *(IPreviewHandler*)previewHandlerPtr;
174176

175177
// See https://blogs.msdn.microsoft.com/adioltean/2005/06/24/when-cocreateinstance-returns-0x80080005-co_e_server_exec_failure/
176178
// CO_E_SERVER_EXEC_FAILURE also tends to happen when debugging in Visual Studio.
@@ -182,7 +184,7 @@ unsafe void SetupHandler(Guid clsid)
182184
if ((int)hr < 0)
183185
throw new COMException(cannotCreate, (int)hr);
184186

185-
pPreviewHandler = new(&previewHandlerObject);
187+
pPreviewHandler = new(previewHandlerPtr);
186188
previewHandler = (IPreviewHandler)previewHandlerObject;
187189
if (previewHandler == null)
188190
{

src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Windows.Win32.Graphics.Direct3D11;
1414
using Windows.Win32.Graphics.DirectComposition;
1515
using Windows.Win32.Graphics.Dwm;
16+
using Windows.Win32.Graphics.Dxgi;
1617
using Windows.Win32.UI.Shell;
1718
using Windows.Win32.UI.WindowsAndMessaging;
1819
using WinRT;
@@ -71,34 +72,17 @@ public void SizeChanged(RECT size)
7172
if (hwnd != HWND.Null)
7273
PInvoke.SetWindowPos(hwnd, (HWND)0, size.left, size.top, size.Width, size.Height, SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE);
7374

74-
if (currentHandler != null)
75-
currentHandler.ResetBounds(new(0, 0, size.Width, size.Height));
75+
currentHandler?.ResetBounds(new(0, 0, size.Width, size.Height));
7676

7777
if (outputLink is not null)
7878
outputLink.PlacementVisual.Size = new(size.Width, size.Height);
7979
}
8080

8181
private unsafe LRESULT WndProc(HWND hwnd, uint msg, WPARAM wParam, LPARAM lParam)
8282
{
83-
//if (msg == 0x0081 /*WM_NCCREATE*/)
84-
//{
85-
// try
86-
// {
87-
// var cp = Marshal.PtrToStructure<CREATESTRUCTW>(lParam).lpCreateParams;
88-
// var pCreateParams = new nint(cp);
89-
// if (pCreateParams != nint.Zero && GCHandle.FromIntPtr(pCreateParams).Target is IWindowInit wnd)
90-
// return wnd.InitWndProcOnNCCreate(
91-
// hwnd,
92-
// msg,
93-
// Marshal.GetFunctionPointerForDelegate(wndProc ?? throw new NullReferenceException()),
94-
// lParam);
95-
// }
96-
// catch { }
97-
//}
98-
//else
9983
if (msg == 0x0001 /*WM_CREATE*/)
10084
{
101-
var clsid = FindPreviewHandlerFor(Item.FileExtension, hwnd.DangerousGetHandle());
85+
var clsid = FindPreviewHandlerFor(Item.FileExtension, hwnd);
10286

10387
isOfficePreview = new Guid?[]
10488
{
@@ -139,10 +123,13 @@ public unsafe void LoadPreview(UIElement presenter)
139123

140124
fixed (char* pszClassName = szClassName)
141125
{
126+
var pWindProc = Marshal.GetFunctionPointerForDelegate(WndProc);
127+
var pfnWndProc = (delegate* unmanaged[Stdcall]<HWND, uint, WPARAM, LPARAM, LRESULT>)pWindProc;
128+
142129
wCls = new WNDCLASSEXW
143130
{
144131
cbSize = (uint)Marshal.SizeOf(typeof(WNDCLASSEXW)),
145-
lpfnWndProc = new(WndProc),
132+
lpfnWndProc = pfnWndProc,
146133
hInstance = hInst,
147134
lpszClassName = pszClassName,
148135
style = 0,
@@ -159,7 +146,7 @@ public unsafe void LoadPreview(UIElement presenter)
159146
{
160147
hwnd = PInvoke.CreateWindowEx(
161148
WINDOW_EX_STYLE.WS_EX_LAYERED | WINDOW_EX_STYLE.WS_EX_COMPOSITED,
162-
wCls.Value.lpszClassName,
149+
pszClassName,
163150
pszWindowName,
164151
WINDOW_STYLE.WS_CHILD | WINDOW_STYLE.WS_CLIPSIBLINGS | WINDOW_STYLE.WS_VISIBLE,
165152
0, 0, 0, 0,
@@ -213,7 +200,7 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
213200
IUnknown* pControlSurface = default;
214201

215202
pDCompositionDevice->CreateVisual(&pChildVisual);
216-
pDCompositionDevice->CreateSurfaceFromHwnd(new(hwnd.DangerousGetHandle()), &pControlSurface);
203+
pDCompositionDevice->CreateSurfaceFromHwnd(hwnd, &pControlSurface);
217204
pChildVisual->SetContent(pControlSurface);
218205
if (pChildVisual is null || pControlSurface is null)
219206
return false;
@@ -251,8 +238,8 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
251238

252239
public void UnloadPreview()
253240
{
254-
if (hwnd != HWND.NULL)
255-
DestroyWindow(hwnd);
241+
if (hwnd != HWND.Null)
242+
PInvoke.DestroyWindow(hwnd);
256243

257244
//outputLink?.Dispose();
258245
outputLink = null;

0 commit comments

Comments
 (0)