@@ -12,12 +12,12 @@ namespace Files.App.Storage
12
12
/// <summary>
13
13
/// Handles bulk file operations in Windows, such as copy, move, delete, create, and rename, supporting progress tracking and event notifications.
14
14
/// </summary>
15
- public sealed partial class WindowsBulkOperations : IDisposable
15
+ public unsafe partial class WindowsBulkOperations : IDisposable
16
16
{
17
17
// Fields
18
18
19
- private readonly ComPtr < IFileOperation > _pFileOperation ;
20
- private readonly ComPtr < IFileOperationProgressSink > _pProgressSink ;
19
+ private readonly IFileOperation * _pFileOperation ;
20
+ private readonly IFileOperationProgressSink * _pProgressSink ;
21
21
private readonly uint _progressSinkCookie ;
22
22
23
23
// Events
@@ -70,24 +70,20 @@ public sealed partial class WindowsBulkOperations : IDisposable
70
70
/// <param name="flags">Defines the behavior of the file operation, such as allowing undo and suppressing directory confirmation.</param>
71
71
public unsafe WindowsBulkOperations ( HWND ownerHWnd = default , FILEOPERATION_FLAGS flags = FILEOPERATION_FLAGS . FOF_ALLOWUNDO | FILEOPERATION_FLAGS . FOF_NOCONFIRMMKDIR )
72
72
{
73
- var clsid = typeof ( FileOperation ) . GUID ;
74
- var iid = typeof ( IFileOperation ) . GUID ;
73
+ IFileOperation * pFileOperation = null ;
75
74
76
- HRESULT hr = PInvoke . CoCreateInstance (
77
- & clsid ,
78
- null ,
79
- CLSCTX . CLSCTX_LOCAL_SERVER ,
80
- & iid ,
81
- ( void * * ) _pFileOperation . GetAddressOf ( ) )
82
- . ThrowIfFailedOnDebug ( ) ;
75
+ HRESULT hr = PInvoke . CoCreateInstance ( CLSID . CLSID_FileOperation , null , CLSCTX . CLSCTX_LOCAL_SERVER , IID . IID_IFileOperation , ( void * * ) & pFileOperation ) ;
76
+ hr . ThrowIfFailedOnDebug ( ) ;
77
+
78
+ _pFileOperation = pFileOperation ;
83
79
84
80
if ( ownerHWnd != default )
85
- hr = _pFileOperation . Get ( ) ->SetOwnerWindow ( ownerHWnd ) . ThrowIfFailedOnDebug ( ) ;
81
+ hr = _pFileOperation ->SetOwnerWindow ( ownerHWnd ) . ThrowIfFailedOnDebug ( ) ;
86
82
87
- hr = _pFileOperation . Get ( ) ->SetOperationFlags ( flags ) . ThrowIfFailedOnDebug ( ) ;
83
+ hr = _pFileOperation ->SetOperationFlags ( flags ) . ThrowIfFailedOnDebug ( ) ;
88
84
89
- _pProgressSink . Attach ( ( IFileOperationProgressSink * ) WindowsBulkOperationsSink . Create ( this ) ) ;
90
- hr = _pFileOperation . Get ( ) ->Advise ( _pProgressSink . Get ( ) , out var progressSinkCookie ) . ThrowIfFailedOnDebug ( ) ;
85
+ _pProgressSink = ( IFileOperationProgressSink * ) WindowsBulkOperationsSink . Create ( this ) ;
86
+ hr = _pFileOperation ->Advise ( _pProgressSink , out var progressSinkCookie ) . ThrowIfFailedOnDebug ( ) ;
91
87
_progressSinkCookie = progressSinkCookie ;
92
88
}
93
89
@@ -101,7 +97,7 @@ public unsafe WindowsBulkOperations(HWND ownerHWnd = default, FILEOPERATION_FLAG
101
97
public unsafe HRESULT QueueCopyOperation ( WindowsStorable targetItem , WindowsFolder destinationFolder , string ? copyName )
102
98
{
103
99
fixed ( char * pszCopyName = copyName )
104
- return _pFileOperation . Get ( ) ->CopyItem ( targetItem . ThisPtr . Get ( ) , destinationFolder . ThisPtr . Get ( ) , pszCopyName , _pProgressSink . Get ( ) ) ;
100
+ return _pFileOperation ->CopyItem ( targetItem . ThisPtr , destinationFolder . ThisPtr , pszCopyName , _pProgressSink ) ;
105
101
}
106
102
107
103
/// <summary>
@@ -111,7 +107,7 @@ public unsafe HRESULT QueueCopyOperation(WindowsStorable targetItem, WindowsFold
111
107
/// <returns>If this method succeeds, it returns <see cref="HRESULT.S_OK"/>. Otherwise, it returns an <see cref="HRESULT"/> error code.</returns>
112
108
public unsafe HRESULT QueueDeleteOperation ( WindowsStorable targetItem )
113
109
{
114
- return _pFileOperation . Get ( ) ->DeleteItem ( targetItem . ThisPtr . Get ( ) , _pProgressSink . Get ( ) ) ;
110
+ return _pFileOperation ->DeleteItem ( targetItem . ThisPtr , _pProgressSink ) ;
115
111
}
116
112
117
113
/// <summary>
@@ -124,7 +120,7 @@ public unsafe HRESULT QueueDeleteOperation(WindowsStorable targetItem)
124
120
public unsafe HRESULT QueueMoveOperation ( WindowsStorable targetItem , WindowsFolder destinationFolder , string ? newName )
125
121
{
126
122
fixed ( char * pszNewName = newName )
127
- return _pFileOperation . Get ( ) ->MoveItem ( targetItem . ThisPtr . Get ( ) , destinationFolder . ThisPtr . Get ( ) , pszNewName , null ) ;
123
+ return _pFileOperation ->MoveItem ( targetItem . ThisPtr , destinationFolder . ThisPtr , pszNewName , null ) ;
128
124
}
129
125
130
126
/// <summary>
@@ -138,7 +134,7 @@ public unsafe HRESULT QueueMoveOperation(WindowsStorable targetItem, WindowsFold
138
134
public unsafe HRESULT QueueCreateOperation ( WindowsFolder destinationFolder , FILE_FLAGS_AND_ATTRIBUTES fileAttributes , string name , string ? templateName )
139
135
{
140
136
fixed ( char * pszName = name , pszTemplateName = templateName )
141
- return _pFileOperation . Get ( ) ->NewItem ( destinationFolder . ThisPtr . Get ( ) , ( uint ) fileAttributes , pszName , pszTemplateName , _pProgressSink . Get ( ) ) ;
137
+ return _pFileOperation ->NewItem ( destinationFolder . ThisPtr , ( uint ) fileAttributes , pszName , pszTemplateName , _pProgressSink ) ;
142
138
}
143
139
144
140
/// <summary>
@@ -150,7 +146,7 @@ public unsafe HRESULT QueueCreateOperation(WindowsFolder destinationFolder, FILE
150
146
public unsafe HRESULT QueueRenameOperation ( WindowsStorable targetItem , string newName )
151
147
{
152
148
fixed ( char * pszNewName = newName )
153
- return _pFileOperation . Get ( ) ->RenameItem ( targetItem . ThisPtr . Get ( ) , pszNewName , _pProgressSink . Get ( ) ) ;
149
+ return _pFileOperation ->RenameItem ( targetItem . ThisPtr , pszNewName , _pProgressSink ) ;
154
150
}
155
151
156
152
/// <summary>
@@ -159,19 +155,19 @@ public unsafe HRESULT QueueRenameOperation(WindowsStorable targetItem, string ne
159
155
/// <returns>If this method succeeds, it returns <see cref="HRESULT.S_OK"/>. Otherwise, it returns an <see cref="HRESULT"/> error code.</returns>
160
156
public unsafe HRESULT PerformAllOperations ( )
161
157
{
162
- return _pFileOperation . Get ( ) ->PerformOperations ( ) ;
158
+ return _pFileOperation ->PerformOperations ( ) ;
163
159
}
164
160
165
161
// Disposer
166
162
167
163
/// <inheritdoc/>
168
164
public unsafe void Dispose ( )
169
165
{
170
- if ( ! _pProgressSink . IsNull )
171
- _pFileOperation . Get ( ) ->Unadvise ( _progressSinkCookie ) ;
166
+ if ( _pProgressSink is not null )
167
+ _pFileOperation ->Unadvise ( _progressSinkCookie ) ;
172
168
173
- _pFileOperation . Dispose ( ) ;
174
- _pProgressSink . Dispose ( ) ;
169
+ _pFileOperation -> Release ( ) ;
170
+ _pProgressSink -> Release ( ) ;
175
171
}
176
172
}
177
173
}
0 commit comments