File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Runtime . CompilerServices ;
3+ using System . Runtime . InteropServices ;
4+
5+ namespace Hi3Helper . Plugin . Core . Utility ;
6+
7+ public static class PluginPInvokeExtension
8+ {
9+ public static unsafe bool TryGetExport < T > ( this nint handle , string exportName , out T callback )
10+ where T : Delegate
11+ {
12+ const string tryGetApiExportName = "TryGetApiExport" ;
13+
14+ Unsafe . SkipInit ( out callback ) ;
15+
16+ if ( ! NativeLibrary . TryGetExport ( handle , tryGetApiExportName , out nint tryGetApiExportP ) ||
17+ tryGetApiExportP == nint . Zero )
18+ {
19+ return false ;
20+ }
21+
22+ delegate * unmanaged[ Cdecl] < char * , void * * , int > tryGetApiExportCallback =
23+ ( delegate * unmanaged[ Cdecl] < char * , void * * , int > ) tryGetApiExportP ;
24+
25+ nint exportP = nint . Zero ;
26+ char * exportNameP = exportName . GetPinnableStringPointer ( ) ;
27+ int tryResult = tryGetApiExportCallback ( exportNameP , ( void * * ) & exportP ) ;
28+
29+ if ( tryResult != 0 ||
30+ exportP == nint . Zero )
31+ {
32+ return false ;
33+ }
34+
35+ callback = Marshal . GetDelegateForFunctionPointer < T > ( exportP ) ;
36+ return true ;
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments