/// <summary>
        ///
        /// </summary>
        /// <param name="errorCode">HRESULT</param>
        /// <param name="sb">buffer</param>
        /// <param name="errorMsg">output error message</param>
        /// <returns>Return false IFF when buffer space isnt enough</returns>
        private static unsafe bool TryGetMessage(int errorCode, int bufferSize, out string errorMsg)
        {
            errorMsg = null;
            char[] buffer = new char[bufferSize];
            int    result;

            fixed(char *pinned_lpBuffer = buffer)
            {
                result = ExternalInterop.FormatMessage(
                    FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM |
                    FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_MAX_WIDTH_MASK,
                    IntPtr.Zero, (uint)errorCode, 0, pinned_lpBuffer,
                    (uint)buffer.Length, IntPtr.Zero);
            }

            if (result != 0) //result hold the number of WCHARs stored in the output buffer(sb)
            {
                if (buffer[result - 1] == ' ')
                {
                    buffer[result - 1] = '\0';
                    result             = result - 1;
                }
                errorMsg = new string(buffer, 0, result);
                return(true);
            }
            else
            {
                if (ExternalInterop.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
                {
                    return(false);
                }
                return(true);
            }
        }
Esempio n. 2
0
 public static void SaveLastWin32Error()
 {
     s_lastWin32Error = ExternalInterop.GetLastWin32Error();
 }