@@ -34,6 +34,7 @@ public sealed partial class ShellPreviewViewModel : BasePreviewModel
3434 WNDPROC _windProc = null ! ;
3535 HWND _hWnd = HWND . Null ;
3636 bool _isOfficePreview = false ;
37+ unsafe char * _pszClassName ;
3738
3839 // Constructor
3940
@@ -136,42 +137,41 @@ public unsafe void LoadPreview(UIElement presenter)
136137 var szClassName = $ "{ nameof ( ShellPreviewViewModel ) } -{ Guid . NewGuid ( ) } ";
137138 var szWindowName = $ "Preview";
138139
139- fixed ( char * pszClassName = szClassName )
140- {
141- _windProc = new ( WndProc ) ;
142- var pWindProc = Marshal . GetFunctionPointerForDelegate ( _windProc ) ;
143- var pfnWndProc = ( delegate * unmanaged[ Stdcall] < HWND , uint , WPARAM , LPARAM , LRESULT > ) pWindProc ;
140+ _pszClassName = ( char * ) Marshal . StringToHGlobalUni ( szClassName ) ;
144141
145- _windowClass = new WNDCLASSEXW ( )
146- {
147- cbSize = ( uint ) sizeof ( WNDCLASSEXW ) ,
148- lpfnWndProc = pfnWndProc ,
149- hInstance = hInst ,
150- lpszClassName = pszClassName ,
151- style = 0 ,
152- hIcon = default ,
153- hIconSm = default ,
154- hCursor = default ,
155- hbrBackground = default ,
156- lpszMenuName = null ,
157- cbClsExtra = 0 ,
158- cbWndExtra = 0 ,
159- } ;
160-
161- PInvoke . RegisterClassEx ( _windowClass ) ;
162-
163- fixed ( char * pszWindowName = szWindowName )
164- {
165- _hWnd = PInvoke . CreateWindowEx (
166- WINDOW_EX_STYLE . WS_EX_LAYERED | WINDOW_EX_STYLE . WS_EX_COMPOSITED ,
167- pszClassName ,
168- pszWindowName ,
169- WINDOW_STYLE . WS_CHILD | WINDOW_STYLE . WS_CLIPSIBLINGS | WINDOW_STYLE . WS_VISIBLE ,
170- 0 , 0 , 0 , 0 ,
171- new ( parent ) ,
172- HMENU . Null ,
173- hInst ) ;
174- }
142+ _windProc = new ( WndProc ) ;
143+ var pWindProc = Marshal . GetFunctionPointerForDelegate ( _windProc ) ;
144+ var pfnWndProc = ( delegate * unmanaged[ Stdcall] < HWND , uint , WPARAM , LPARAM , LRESULT > ) pWindProc ;
145+
146+ _windowClass = new WNDCLASSEXW ( )
147+ {
148+ cbSize = ( uint ) sizeof ( WNDCLASSEXW ) ,
149+ lpfnWndProc = pfnWndProc ,
150+ hInstance = hInst ,
151+ lpszClassName = _pszClassName ,
152+ style = 0 ,
153+ hIcon = default ,
154+ hIconSm = default ,
155+ hCursor = default ,
156+ hbrBackground = default ,
157+ lpszMenuName = null ,
158+ cbClsExtra = 0 ,
159+ cbWndExtra = 0 ,
160+ } ;
161+
162+ PInvoke . RegisterClassEx ( _windowClass ) ;
163+
164+ fixed ( char * pszWindowName = szWindowName )
165+ {
166+ _hWnd = PInvoke . CreateWindowEx (
167+ WINDOW_EX_STYLE . WS_EX_LAYERED | WINDOW_EX_STYLE . WS_EX_COMPOSITED ,
168+ _pszClassName ,
169+ pszWindowName ,
170+ WINDOW_STYLE . WS_CHILD | WINDOW_STYLE . WS_CLIPSIBLINGS | WINDOW_STYLE . WS_VISIBLE ,
171+ 0 , 0 , 0 , 0 ,
172+ new ( parent ) ,
173+ HMENU . Null ,
174+ hInst ) ;
175175 }
176176
177177 _ = ChildWindowToXaml ( parent , presenter ) ;
@@ -252,7 +252,7 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
252252 . Succeeded ;
253253 }
254254
255- public void UnloadPreview ( )
255+ public unsafe void UnloadPreview ( )
256256 {
257257 if ( _hWnd != HWND . Null )
258258 {
@@ -267,6 +267,12 @@ public void UnloadPreview()
267267 _contentExternalOutputLink = null ;
268268
269269 PInvoke . UnregisterClass ( _windowClass . lpszClassName , PInvoke . GetModuleHandle ( default ( PWSTR ) ) ) ;
270+
271+ if ( _pszClassName is not null )
272+ {
273+ Marshal . FreeHGlobal ( ( nint ) _pszClassName ) ;
274+ _pszClassName = null ;
275+ }
270276 }
271277
272278 public unsafe void PointerEntered ( bool onPreview )
0 commit comments