Debugging.cs
author sl
Wed, 14 May 2014 16:37:44 +0200
changeset 4 0220435cf48a
permissions -rw-r--r--
Newly added device are now showing green. Removed devices are showing red.
Other devices are showing black. Devices in unknown state are showing purple.
sl@0
     1
using System;
sl@0
     2
 
sl@0
     3
namespace GenericHid
sl@0
     4
{
sl@0
     5
	/// <summary>
sl@0
     6
	/// Used only in Debug.Write statements.
sl@0
     7
	/// </summary>
sl@0
     8
	/// 
sl@0
     9
	internal sealed partial class Debugging
sl@0
    10
    {         
sl@0
    11
        ///  <summary>
sl@0
    12
        ///  Get text that describes the result of an API call.
sl@0
    13
        ///  </summary>
sl@0
    14
        ///  
sl@0
    15
        ///  <param name="functionName"> the name of the API function. </param>
sl@0
    16
        ///  
sl@0
    17
        ///  <returns>
sl@0
    18
        ///  The text.
sl@0
    19
        ///  </returns>
sl@0
    20
          
sl@0
    21
        internal String ResultOfApiCall( String functionName ) 
sl@0
    22
        {
sl@0
    23
	        var resultString = new String(Convert.ToChar( 0 ), 129 ); 
sl@0
    24
            
sl@0
    25
            // Returns the result code for the last API call.
sl@0
    26
            
sl@0
    27
            Int32 resultCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); 
sl@0
    28
            
sl@0
    29
            // Get the result message that corresponds to the code.
sl@0
    30
sl@0
    31
            Int64 temp = 0;
sl@0
    32
			Int32 bytes = NativeMethods.FormatMessage(NativeMethods.FormatMessageFromSystem, ref temp, resultCode, 0, resultString, 128, 0); 
sl@0
    33
            
sl@0
    34
            // Subtract two characters from the message to strip the CR and LF.
sl@0
    35
            
sl@0
    36
            if ( bytes > 2 ) 
sl@0
    37
            { 
sl@0
    38
                resultString = resultString.Remove( bytes - 2, 2 ); 
sl@0
    39
            }             
sl@0
    40
            // Create the String to return.
sl@0
    41
sl@0
    42
            resultString = Environment.NewLine + functionName + Environment.NewLine + "Result = " + resultString + Environment.NewLine; 
sl@0
    43
            
sl@0
    44
            return resultString;             
sl@0
    45
        }         
sl@0
    46
    }  
sl@0
    47
}