Skip to content

Commit ea8a473

Browse files
committed
fix crash when version mismatch
1 parent 583efd3 commit ea8a473

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed

ZEDCamera/Assets/SDK/NativeInterface/ZEDCamera.cs

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,50 @@
77

88
namespace sl
99
{
10+
public static class NativeWrapper
11+
{
12+
[DllImport("kernel32.dll")]
13+
private static extern IntPtr LoadLibrary(string dllToLoad);
14+
15+
[DllImport("kernel32.dll")]
16+
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
17+
18+
[DllImport("kernel32.dll")]
19+
private static extern bool FreeLibrary(IntPtr hModule);
20+
21+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
22+
private delegate int CheckZEDPluginDelegate(int Major, int Minor);
23+
24+
private static CheckZEDPluginDelegate _checkZEDPlugin;
25+
26+
public static bool IsWrapperLoaded => _checkZEDPlugin != null;
27+
28+
public static bool Init()
29+
{
30+
string DllPath = "Assets/SDK/Plugins/win64/" + sl.ZEDCommon.NameDLL;
31+
IntPtr pDll = LoadLibrary(DllPath);
32+
if (pDll == IntPtr.Zero)
33+
{
34+
Console.WriteLine("Failed to load DLL.");
35+
return false;
36+
}
37+
IntPtr pAddressOfFunctionToCall = GetProcAddress(pDll, "sl_check_plugin");
38+
if (pAddressOfFunctionToCall != IntPtr.Zero)
39+
{
40+
_checkZEDPlugin = Marshal.GetDelegateForFunctionPointer<CheckZEDPluginDelegate>(pAddressOfFunctionToCall);
41+
return true;
42+
}
43+
return false;
44+
}
45+
46+
public static int CheckPlugin()
47+
{
48+
if (_checkZEDPlugin == null)
49+
throw new InvalidOperationException("Function not loaded.");
50+
return _checkZEDPlugin(ZEDCamera.PluginVersion.Major, ZEDCamera.PluginVersion.Minor);
51+
}
52+
}
53+
1054
/// <summary>
1155
/// Main interface between Unity and the ZED SDK. Primarily consists of extern calls to the ZED SDK wrapper .dll and
1256
/// low-level logic to process data sent to/received from it.
@@ -773,8 +817,9 @@ private static extern int dllz_get_objects_batch_data(int cameraID, int batch_in
773817
/*
774818
* Specific plugin functions
775819
*/
776-
[DllImport(nameDll, EntryPoint = "sl_check_plugin")]
777-
private static extern int dllz_check_plugin(int major, int minor);
820+
821+
/* [DllImport(nameDll, EntryPoint = "sl_check_plugin")]
822+
private static extern int dllz_check_plugin(int major, int minor);*/
778823

779824
[DllImport(nameDll, EntryPoint = "sl_get_sdk_version")]
780825
private static extern IntPtr dllz_get_sdk_version();
@@ -931,25 +976,19 @@ public static string GenerateUniqueID()
931976
/// </summary>
932977
public static bool CheckPlugin()
933978
{
934-
try
979+
if (NativeWrapper.Init())
935980
{
936-
int res = dllz_check_plugin(PluginVersion.Major, PluginVersion.Minor);
937-
if (res!= 0)
981+
int res = NativeWrapper.CheckPlugin();
982+
if (res == 0)
938983
{
939-
//0 = installed SDK is compatible with plugin. 1 otherwise.
940-
Debug.LogError(ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_DEPENDENCIES_ISSUE));
941-
return false;
984+
pluginIsReady = true;
985+
return true;
942986
}
943987
}
944-
catch (DllNotFoundException) //In case could not resolve the dll/.so
945-
{
946-
Debug.Log("DllNotFoundException");
947-
Debug.LogError(ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_DEPENDENCIES_ISSUE));
948-
return false;
949-
}
950-
951-
pluginIsReady = true;
952-
return true;
988+
989+
//0 = installed SDK is compatible with plugin. 1 otherwise.
990+
Debug.LogError(ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_DEPENDENCIES_ISSUE));
991+
return false;
953992
}
954993

955994
/// <summary>

0 commit comments

Comments
 (0)