From d5f567b90bf50b6a8ee44c409b928f4318f45e2f Mon Sep 17 00:00:00 2001 From: tinysec Date: Wed, 26 Nov 2025 11:59:42 +0800 Subject: [PATCH 1/2] feat: LoadFileAsync --- Handle/BNBinaryView.cs | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/Handle/BNBinaryView.cs b/Handle/BNBinaryView.cs index 2a34c7d..9b84543 100644 --- a/Handle/BNBinaryView.cs +++ b/Handle/BNBinaryView.cs @@ -3,6 +3,8 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; using Microsoft.Win32.SafeHandles; namespace BinaryNinja @@ -182,6 +184,63 @@ public ulong Length ); } + public static Task LoadFileAsync( + string filename , + bool updateAnalysis = false , + string options = "", + CancellationToken? cancellationToken = null + ) + { + return Task.Run(() => + { + bool cancelled = false; + + ProgressDelegate progress = (param2 , param3) => + { + if (null != cancellationToken) + { + if (cancellationToken.GetValueOrDefault().IsCancellationRequested) + { + cancelled = true; + + return false; + } + } + + return true; + }; + + if (cancelled) + { + return null; + } + + BinaryView? view = BinaryView.TakeHandle( + NativeMethods.BNLoadFilename( + filename , + updateAnalysis , + options , + Marshal.GetFunctionPointerForDelegate( + UnsafeUtils.WrapProgressDelegate(progress)) , + IntPtr.Zero + ) + ); + + if (null == view) + { + return null; + } + + if (cancelled && ( null != cancellationToken) ) + { + cancellationToken.GetValueOrDefault().ThrowIfCancellationRequested(); + } + + return view; + } + ); + } + public static BinaryView? OpenExisting(string filename , ProgressDelegate? progress = null) { FileMetadata file = new FileMetadata(filename); From 0893cdf3c93c3a3cb97995cd358de471c379b041 Mon Sep 17 00:00:00 2001 From: tinysec Date: Wed, 26 Nov 2025 12:06:32 +0800 Subject: [PATCH 2/2] feat: OpenExistingDatabaseAsync --- Handle/BNBinaryView.cs | 121 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 117 insertions(+), 4 deletions(-) diff --git a/Handle/BNBinaryView.cs b/Handle/BNBinaryView.cs index 9b84543..e648fbb 100644 --- a/Handle/BNBinaryView.cs +++ b/Handle/BNBinaryView.cs @@ -135,9 +135,9 @@ public ulong Length } public BinaryView? Load( - bool updateAnalysis , - string options , - ProgressDelegate? progress + bool updateAnalysis = false , + string options = "", + ProgressDelegate? progress = null ) { return BinaryView.TakeHandle( @@ -154,6 +154,63 @@ public ulong Length ) ); } + + public Task LoadAsync( + bool updateAnalysis = false, + string options = "" , + CancellationToken? cancellationToken = null + ) + { + return Task.Run(() => + { + bool cancelled = false; + + ProgressDelegate progress = (param2 , param3) => + { + if (null != cancellationToken) + { + if (cancellationToken.GetValueOrDefault().IsCancellationRequested) + { + cancelled = true; + + return false; + } + } + + return true; + }; + + if (cancelled) + { + return null; + } + + BinaryView? view = BinaryView.TakeHandle( + NativeMethods.BNLoadBinaryView( + this.handle , + updateAnalysis , + options , + Marshal.GetFunctionPointerForDelegate( + UnsafeUtils.WrapProgressDelegate(progress) + ), + IntPtr.Zero + ) + ); + + if (null == view) + { + return null; + } + + if (cancelled && ( null != cancellationToken) ) + { + cancellationToken.GetValueOrDefault().ThrowIfCancellationRequested(); + } + + return view; + } + ); + } /// /// @@ -241,7 +298,7 @@ public ulong Length ); } - public static BinaryView? OpenExisting(string filename , ProgressDelegate? progress = null) + public static BinaryView? OpenExistingDatabase(string filename , ProgressDelegate? progress = null) { FileMetadata file = new FileMetadata(filename); @@ -269,6 +326,62 @@ public ulong Length } } + public static Task OpenExistingDatabaseAsync( + string filename , + CancellationToken? cancellationToken = null + ) + { + return Task.Run(() => + { + bool cancelled = false; + + ProgressDelegate progress = (param2 , param3) => + { + if (null != cancellationToken) + { + if (cancellationToken.GetValueOrDefault().IsCancellationRequested) + { + cancelled = true; + + return false; + } + } + + return true; + }; + + if (cancelled) + { + return null; + } + + FileMetadata file = new FileMetadata(filename); + + BinaryView? view = BinaryView.TakeHandle( + NativeMethods.BNOpenExistingDatabaseWithProgress( + file.DangerousGetHandle() , + filename , + Marshal.GetFunctionPointerForDelegate( + UnsafeUtils.WrapProgressDelegate(progress)) , + IntPtr.Zero + ) + ); + + if (null == view) + { + return null; + } + + if (cancelled && ( null != cancellationToken) ) + { + cancellationToken.GetValueOrDefault().ThrowIfCancellationRequested(); + } + + return view; + } + ); + } + public BinaryView? Parent { get