onnxruntime/csharp/src/Microsoft.ML.OnnxRuntime/NativeApiStatus.cs
Dmitri Smirnov 2b460eaeca
Revise IDisposable implementation in C# interfaces (#4915)
Revise IDisposable implementation in C# interfaces
2020-08-27 09:17:42 -07:00

41 lines
1.4 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;
namespace Microsoft.ML.OnnxRuntime
{
class NativeApiStatus
{
private static string GetErrorMessage(IntPtr /*(ONNXStatus*)*/status)
{
// nativeString belongs to status, no need for separate release
IntPtr nativeString = NativeMethods.OrtGetErrorMessage(status);
return NativeOnnxValueHelper.StringFromNativeUtf8(nativeString);
}
/// <summary>
/// Checks the native Status if the errocode is OK/Success. Otherwise constructs an appropriate exception and throws.
/// Releases the native status object, as needed.
/// </summary>
/// <param name="nativeStatus"></param>
/// <throws></throws>
public static void VerifySuccess(IntPtr nativeStatus)
{
if (nativeStatus != IntPtr.Zero)
{
try
{
ErrorCode statusCode = NativeMethods.OrtGetErrorCode(nativeStatus);
string errorMessage = GetErrorMessage(nativeStatus);
throw new OnnxRuntimeException(statusCode, errorMessage);
}
finally
{
NativeMethods.OrtReleaseStatus(nativeStatus);
}
}
}
}
}