33using System . IO ;
44using System . Linq ;
55using System . Runtime . InteropServices ;
6+ using System . Threading ;
7+ using System . Threading . Tasks ;
68using Microsoft . Win32 . SafeHandles ;
79
810namespace BinaryNinja
@@ -133,9 +135,9 @@ public ulong Length
133135 }
134136
135137 public BinaryView ? Load (
136- bool updateAnalysis ,
137- string options ,
138- ProgressDelegate ? progress
138+ bool updateAnalysis = false ,
139+ string options = "" ,
140+ ProgressDelegate ? progress = null
139141 )
140142 {
141143 return BinaryView . TakeHandle (
@@ -152,6 +154,63 @@ public ulong Length
152154 )
153155 ) ;
154156 }
157+
158+ public Task < BinaryView ? > LoadAsync (
159+ bool updateAnalysis = false ,
160+ string options = "" ,
161+ CancellationToken ? cancellationToken = null
162+ )
163+ {
164+ return Task . Run ( ( ) =>
165+ {
166+ bool cancelled = false ;
167+
168+ ProgressDelegate progress = ( param2 , param3 ) =>
169+ {
170+ if ( null != cancellationToken )
171+ {
172+ if ( cancellationToken . GetValueOrDefault ( ) . IsCancellationRequested )
173+ {
174+ cancelled = true ;
175+
176+ return false ;
177+ }
178+ }
179+
180+ return true ;
181+ } ;
182+
183+ if ( cancelled )
184+ {
185+ return null ;
186+ }
187+
188+ BinaryView ? view = BinaryView . TakeHandle (
189+ NativeMethods . BNLoadBinaryView (
190+ this . handle ,
191+ updateAnalysis ,
192+ options ,
193+ Marshal . GetFunctionPointerForDelegate < NativeDelegates . BNProgressFunction > (
194+ UnsafeUtils . WrapProgressDelegate ( progress )
195+ ) ,
196+ IntPtr . Zero
197+ )
198+ ) ;
199+
200+ if ( null == view )
201+ {
202+ return null ;
203+ }
204+
205+ if ( cancelled && ( null != cancellationToken ) )
206+ {
207+ cancellationToken . GetValueOrDefault ( ) . ThrowIfCancellationRequested ( ) ;
208+ }
209+
210+ return view ;
211+ }
212+ ) ;
213+ }
155214
156215 /// <summary>
157216 ///
@@ -182,7 +241,64 @@ public ulong Length
182241 ) ;
183242 }
184243
185- public static BinaryView ? OpenExisting ( string filename , ProgressDelegate ? progress = null )
244+ public static Task < BinaryView ? > LoadFileAsync (
245+ string filename ,
246+ bool updateAnalysis = false ,
247+ string options = "" ,
248+ CancellationToken ? cancellationToken = null
249+ )
250+ {
251+ return Task . Run ( ( ) =>
252+ {
253+ bool cancelled = false ;
254+
255+ ProgressDelegate progress = ( param2 , param3 ) =>
256+ {
257+ if ( null != cancellationToken )
258+ {
259+ if ( cancellationToken . GetValueOrDefault ( ) . IsCancellationRequested )
260+ {
261+ cancelled = true ;
262+
263+ return false ;
264+ }
265+ }
266+
267+ return true ;
268+ } ;
269+
270+ if ( cancelled )
271+ {
272+ return null ;
273+ }
274+
275+ BinaryView ? view = BinaryView . TakeHandle (
276+ NativeMethods . BNLoadFilename (
277+ filename ,
278+ updateAnalysis ,
279+ options ,
280+ Marshal . GetFunctionPointerForDelegate < NativeDelegates . BNProgressFunction > (
281+ UnsafeUtils . WrapProgressDelegate ( progress ) ) ,
282+ IntPtr . Zero
283+ )
284+ ) ;
285+
286+ if ( null == view )
287+ {
288+ return null ;
289+ }
290+
291+ if ( cancelled && ( null != cancellationToken ) )
292+ {
293+ cancellationToken . GetValueOrDefault ( ) . ThrowIfCancellationRequested ( ) ;
294+ }
295+
296+ return view ;
297+ }
298+ ) ;
299+ }
300+
301+ public static BinaryView ? OpenExistingDatabase ( string filename , ProgressDelegate ? progress = null )
186302 {
187303 FileMetadata file = new FileMetadata ( filename ) ;
188304
@@ -210,6 +326,62 @@ public ulong Length
210326 }
211327 }
212328
329+ public static Task < BinaryView ? > OpenExistingDatabaseAsync (
330+ string filename ,
331+ CancellationToken ? cancellationToken = null
332+ )
333+ {
334+ return Task . Run ( ( ) =>
335+ {
336+ bool cancelled = false ;
337+
338+ ProgressDelegate progress = ( param2 , param3 ) =>
339+ {
340+ if ( null != cancellationToken )
341+ {
342+ if ( cancellationToken . GetValueOrDefault ( ) . IsCancellationRequested )
343+ {
344+ cancelled = true ;
345+
346+ return false ;
347+ }
348+ }
349+
350+ return true ;
351+ } ;
352+
353+ if ( cancelled )
354+ {
355+ return null ;
356+ }
357+
358+ FileMetadata file = new FileMetadata ( filename ) ;
359+
360+ BinaryView ? view = BinaryView . TakeHandle (
361+ NativeMethods . BNOpenExistingDatabaseWithProgress (
362+ file . DangerousGetHandle ( ) ,
363+ filename ,
364+ Marshal . GetFunctionPointerForDelegate < NativeDelegates . BNProgressFunction > (
365+ UnsafeUtils . WrapProgressDelegate ( progress ) ) ,
366+ IntPtr . Zero
367+ )
368+ ) ;
369+
370+ if ( null == view )
371+ {
372+ return null ;
373+ }
374+
375+ if ( cancelled && ( null != cancellationToken ) )
376+ {
377+ cancellationToken . GetValueOrDefault ( ) . ThrowIfCancellationRequested ( ) ;
378+ }
379+
380+ return view ;
381+ }
382+ ) ;
383+ }
384+
213385 public BinaryView ? Parent
214386 {
215387 get
0 commit comments