Skip to content

Commit 00bee97

Browse files
committed
Move TryGetApiExport from launcher to plugin
1 parent aefac9b commit 00bee97

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Utility/PluginPInvokeExtension.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

0 commit comments

Comments
 (0)