internal static PeerToPeerException CreateFromHr(string message, Int32 hr)
        {
            PeerToPeerException p2pEx = null;
            int    facility           = ((hr >> 16) & 0x1FFF);
            IntPtr NativeMessagePtr   = IntPtr.Zero;

            try
            {
                UInt32 dwLength = UnsafeSystemNativeMethods.FormatMessage(
                    FormatMessageFlags.FORMAT_MESSAGE_ALLOCATE_BUFFER |
                    FormatMessageFlags.FORMAT_MESSAGE_ARGUMENT_ARRAY |
                    (facility == FACILITY_P2P ? FormatMessageFlags.FORMAT_MESSAGE_FROM_HMODULE : FormatMessageFlags.FORMAT_MESSAGE_FROM_SYSTEM),
                    (facility == FACILITY_P2P ? PeerToPeerOSHelper.P2PModuleHandle : IntPtr.Zero),
                    (uint)(hr),
                    0,
                    ref NativeMessagePtr,
                    0,
                    IntPtr.Zero);
                if (dwLength != 0)
                {
                    string NativeMessage = Marshal.PtrToStringUni(NativeMessagePtr);
                    p2pEx = new PeerToPeerException(message, new Win32Exception(hr, NativeMessage));
                }
                else
                {
                    p2pEx = new PeerToPeerException(message, Marshal.GetExceptionForHR(hr));
                }
            }
            catch (Exception ex)
            {
                Logging.P2PTraceSource.TraceEvent(TraceEventType.Warning, 0, "Could not get the error message for error code {0} - Exception {1}", hr, ex);
                if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
                {
                    throw;
                }
            }
            finally
            {
                if (NativeMessagePtr != IntPtr.Zero)
                {
                    UnsafeSystemNativeMethods.LocalFree(NativeMessagePtr);
                }
            }
            if (p2pEx == null)
            {
                Logging.P2PTraceSource.TraceEvent(TraceEventType.Warning, 0, "Could not get the error message for error code {0}", hr);
                p2pEx = new PeerToPeerException(message + "Underlying native error " + hr);
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "Exception: {0}", p2pEx);
            return(p2pEx);
        }
 static PeerToPeerOSHelper()
 {
     if (IsSupportedOS())
     {
         // if OS is supported, but p2p.dll is not available, P2P is not supported (original behavior)
         string dllFileName = Path.Combine(Environment.SystemDirectory, UnsafeP2PNativeMethods.P2P);
         s_P2PLibrary = SafeLoadLibrary.LoadLibraryEx(dllFileName);
         if (!s_P2PLibrary.IsInvalid)
         {
             IntPtr Address = UnsafeSystemNativeMethods.GetProcAddress(s_P2PLibrary, "PeerCreatePeerName");
             if (Address != IntPtr.Zero)
             {
                 s_supportsP2P = true;
             }
         }
     }
     //else --> the SafeLoadLibrary would have already been marked
     //          closed by the LoadLibraryEx call above.
 }