2018-11-20 00:48:22 +00:00
|
|
|
// 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)
|
|
|
|
|
{
|
2018-12-14 22:54:23 +00:00
|
|
|
IntPtr nativeString = NativeMethods.OrtGetErrorMessage(status);
|
2018-11-20 00:48:22 +00:00
|
|
|
string str = Marshal.PtrToStringAnsi(nativeString); //assumes charset = ANSI
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2018-12-14 22:54:23 +00:00
|
|
|
ErrorCode statusCode = NativeMethods.OrtGetErrorCode(nativeStatus);
|
2018-11-20 00:48:22 +00:00
|
|
|
string errorMessage = GetErrorMessage(nativeStatus);
|
2018-12-18 19:39:46 +00:00
|
|
|
NativeMethods.OrtReleaseStatus(nativeStatus);
|
2018-11-26 07:44:39 +00:00
|
|
|
throw new OnnxRuntimeException(statusCode, errorMessage);
|
2018-11-20 00:48:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|