예제 #1
0
        /// <summary>
        /// 执行参数executedUnmanagedCode包含存在返回值的非托管代码并检查是否写入了新的Win32错误代码。
        /// </summary>
        /// <typeparam name="T">用于表示参数executedUnmanagedCode所封装委托的返回值类型。</typeparam>
        /// <param name="executedUnmanagedCode">需要执行的非托管代码。</param>
        /// <param name="returnValue">参数executedUnmanagedCode封装的委托的返回值。</param>
        /// <param name="win32ErrorCode">用于已写入的新的Win32错误代码,如果该方法没有写入新的错误代码,则该参数将会保存操作系统中上一个Win32错误代码。</param>
        /// <returns>如果该操作不会写入新的Win32错误代码,则将会返回false,但是在写入任何Win32错误代码的情况下,该方法都会抛出true。</returns>
        public static bool IsWritedWin32ErrorCode <T>(Func <T> executedUnmanagedCode, out T returnValue, out long win32ErrorCode)
        {
            bool result             = false;
            long lastWin32ErrorCode = Win32ApiHelper.GetLastWin32ApiError();

            returnValue    = executedUnmanagedCode.Invoke();
            win32ErrorCode = Win32ApiHelper.GetLastWin32ApiError();
            if (lastWin32ErrorCode != win32ErrorCode)
            {
                result = true;
            }
            else
            {
                win32ErrorCode = lastWin32ErrorCode;
            }
            return(result);
        }
 /// <summary>
 /// 构造函数,创建一个由用户指定错误代码的Win32ApiExecutedResult实例。
 /// </summary>
 /// <param name="win32ApiErrorCode">指定的错误代码。</param>
 public Win32ApiExecutedResult(long win32ApiErrorCode)
 {
     _win32ApiErrorCode         = win32ApiErrorCode;
     _previousWin32ApiErrorCode = Win32ApiHelper.GetLastWin32ApiError();
     _formatedErrorInfoString   = Win32ApiHelper.FormatErrorCode(win32ApiErrorCode);
 }
 private const long WIN32API_ERROR_CODE_SUCCESS = 0x00000000; //API常量,表示Win32Api中的错误代码ERROR_SUCCESS(0x0000)。
 /// <summary>
 /// 构造函数,创建一个错误代码为0的Win32ApiExecutedResult实例。
 /// </summary>
 public Win32ApiExecutedResult()
 {
     _win32ApiErrorCode         = WIN32API_ERROR_CODE_SUCCESS;
     _previousWin32ApiErrorCode = Win32ApiHelper.GetLastWin32ApiError();
     _formatedErrorInfoString   = Win32ApiHelper.FormatErrorCode(_win32ApiErrorCode);
 }