Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / SystemNet / Net / PeerToPeer / UnsafePeerToPeerMethods.cs / 2 / UnsafePeerToPeerMethods.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
using System.Security.Permissions;
namespace System.Net.PeerToPeer
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32.SafeHandles;
using System.Security;
using System.Runtime.InteropServices;
using System.Runtime.ConstrainedExecution;
using System.Threading;
using System.Net.Sockets;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct PEER_PNRP_CLOUD_INFO
{
internal IntPtr pwzCloudName;
internal UInt32 dwScope;
internal UInt32 dwScopeId;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct PEER_PNRP_REGISTRATION_INFO
{
internal string pwszCloudName;
internal string pwszPublishingIdentity;
internal UInt32 cAddresses;
internal IntPtr ArrayOfSOCKADDRIN6Pointers;
internal ushort wport;
internal string pwszComment;
internal PEER_DATA payLoad;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct PEER_PNRP_ENDPOINT_INFO
{
internal IntPtr pwszPeerName;
internal UInt32 cAddresses;
internal IntPtr ArrayOfSOCKADDRIN6Pointers;
internal IntPtr pwszComment;
internal PEER_DATA payLoad;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct PEER_DATA
{
internal UInt32 cbPayload;
internal IntPtr pbPayload;
}
[System.Security.SuppressUnmanagedCodeSecurityAttribute()]
internal static class UnsafeP2PNativeMethods
{
internal const string P2P = "p2p.dll";
[DllImport(P2P, CharSet = CharSet.Unicode)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal extern static void PeerFreeData(IntPtr dataToFree);
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerPnrpGetCloudInfo(out UInt32 pNumClouds, out SafePeerData pArrayOfClouds);
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerPnrpStartup(ushort versionRequired);
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32
PeerCreatePeerName(string identity, string classfier, out SafePeerData peerName);
//[DllImport(P2P, CharSet = CharSet.Unicode)]
//internal extern static Int32 PeerCreatePeerName(string identity, string classfier, out SafePeerData peerName);
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerIdentityGetDefault(out SafePeerData defaultIdentity);
/*
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerIdentityCreate(string classifier, string friendlyName, IntPtr hCryptoProv, out SafePeerData defaultIdentity);
*/
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerNameToPeerHostName(string peerName, out SafePeerData peerHostName);
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerHostNameToPeerName(string peerHostName, out SafePeerData peerName);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpRegister(string pcwzPeerName,
ref PEER_PNRP_REGISTRATION_INFO registrationInfo,
out SafePeerNameUnregister handle);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpUnregister(IntPtr handle);
[System.Security.SecurityCritical]
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpUpdateRegistration(SafePeerNameUnregister hRegistration,
ref PEER_PNRP_REGISTRATION_INFO registrationInfo);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpResolve(string pcwzPeerNAme,
string pcwzCloudName,
ref UInt32 pcEndPoints,
out SafePeerData pEndPoints);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpStartResolve(string pcwzPeerNAme,
string pcwzCloudName,
UInt32 cEndPoints,
SafeWaitHandle hEvent,
out SafePeerNameEndResolve safePeerNameEndResolve);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpGetEndpoint(IntPtr Handle,
out SafePeerData pEndPoint);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpEndResolve(IntPtr Handle);
private static object s_InternalSyncObject;
private static bool s_Initialized;
private const int PNRP_VERSION = 2;
private static object InternalSyncObject {
get {
if (s_InternalSyncObject == null) {
object o = new object();
Interlocked.CompareExchange(ref s_InternalSyncObject, o, null);
}
return s_InternalSyncObject;
}
}
//
//
//
//
[System.Security.SecurityCritical]
internal static void PnrpStartup()
{
if (!s_Initialized) {
lock (InternalSyncObject) {
if (!s_Initialized) {
Int32 result = PeerPnrpStartup(PNRP_VERSION);
if (result != 0) {
throw new PeerToPeerException(SR.GetString(SR.Pnrp_StartupFailed), Marshal.GetExceptionForHR(result));
}
s_Initialized = true;
}
}
}
} //end of method PnrpStartup
}
//
//
//
[System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
[System.Security.SuppressUnmanagedCodeSecurityAttribute()]
internal sealed class SafePeerData : SafeHandleZeroOrMinusOneIsInvalid
{
private SafePeerData() : base(true) { }
//private SafePeerData(bool ownsHandle) : base(ownsHandle) { }
internal string UnicodeString
{
get
{
return Marshal.PtrToStringUni(handle);
}
}
protected override bool ReleaseHandle()
{
UnsafeP2PNativeMethods.PeerFreeData(handle);
SetHandleAsInvalid(); //Mark it closed - This does not change the value of the handle it self
SetHandle(IntPtr.Zero); //Mark it invalid - Change the value to Zero
return true;
}
}
//
//
//
[System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
[System.Security.SuppressUnmanagedCodeSecurityAttribute()]
internal sealed class SafePeerNameUnregister : SafeHandleZeroOrMinusOneIsInvalid
{
internal SafePeerNameUnregister() : base(true) { }
//internal SafePeerNameUnregister(bool ownsHandle) : base(ownsHandle) { }
protected override bool ReleaseHandle()
{
UnsafeP2PNativeMethods.PeerPnrpUnregister(handle);
SetHandleAsInvalid(); //Mark it closed - This does not change the value of the handle it self
SetHandle(IntPtr.Zero); //Mark it invalid - Change the value to Zero
return true;
}
}
//
//
//
[System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
[System.Security.SuppressUnmanagedCodeSecurityAttribute()]
internal sealed class SafePeerNameEndResolve : SafeHandleZeroOrMinusOneIsInvalid
{
internal SafePeerNameEndResolve() : base(true) { }
//internal SafePeerNameEndResolve(bool ownsHandle) : base(ownsHandle) { }
protected override bool ReleaseHandle()
{
UnsafeP2PNativeMethods.PeerPnrpEndResolve(handle);
SetHandleAsInvalid(); //Mark it closed - This does not change the value of the handle it self
SetHandle(IntPtr.Zero); //Mark it invalid - Change the value to Zero
return true;
}
}
///
/// Determines whether P2P is installed
/// Note static constructors are guaranteed to be
/// run in a thread safe manner. so no locks are necessary
///
internal static class PeerToPeerOSHelper
{
private static bool s_supportsP2P = false;
private static SafeLoadLibrary s_P2PLibrary = null;
//
//
//
//
//
//
[System.Security.SecurityCritical]
static PeerToPeerOSHelper() {
s_P2PLibrary = SafeLoadLibrary.LoadLibraryEx(UnsafeP2PNativeMethods.P2P);
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.
}
internal static bool SupportsP2P {
get {
return s_supportsP2P;
}
}
internal static IntPtr P2PModuleHandle
{
//
//
//
//
//
//
[System.Security.SecurityCritical]
[SecurityPermissionAttribute(SecurityAction.LinkDemand, UnmanagedCode=true)]
get
{
if (!s_P2PLibrary.IsClosed && !s_P2PLibrary.IsInvalid)
return s_P2PLibrary.DangerousGetHandle();
return IntPtr.Zero;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
using System.Security.Permissions;
namespace System.Net.PeerToPeer
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32.SafeHandles;
using System.Security;
using System.Runtime.InteropServices;
using System.Runtime.ConstrainedExecution;
using System.Threading;
using System.Net.Sockets;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct PEER_PNRP_CLOUD_INFO
{
internal IntPtr pwzCloudName;
internal UInt32 dwScope;
internal UInt32 dwScopeId;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct PEER_PNRP_REGISTRATION_INFO
{
internal string pwszCloudName;
internal string pwszPublishingIdentity;
internal UInt32 cAddresses;
internal IntPtr ArrayOfSOCKADDRIN6Pointers;
internal ushort wport;
internal string pwszComment;
internal PEER_DATA payLoad;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct PEER_PNRP_ENDPOINT_INFO
{
internal IntPtr pwszPeerName;
internal UInt32 cAddresses;
internal IntPtr ArrayOfSOCKADDRIN6Pointers;
internal IntPtr pwszComment;
internal PEER_DATA payLoad;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct PEER_DATA
{
internal UInt32 cbPayload;
internal IntPtr pbPayload;
}
[System.Security.SuppressUnmanagedCodeSecurityAttribute()]
internal static class UnsafeP2PNativeMethods
{
internal const string P2P = "p2p.dll";
[DllImport(P2P, CharSet = CharSet.Unicode)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal extern static void PeerFreeData(IntPtr dataToFree);
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerPnrpGetCloudInfo(out UInt32 pNumClouds, out SafePeerData pArrayOfClouds);
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerPnrpStartup(ushort versionRequired);
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32
PeerCreatePeerName(string identity, string classfier, out SafePeerData peerName);
//[DllImport(P2P, CharSet = CharSet.Unicode)]
//internal extern static Int32 PeerCreatePeerName(string identity, string classfier, out SafePeerData peerName);
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerIdentityGetDefault(out SafePeerData defaultIdentity);
/*
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerIdentityCreate(string classifier, string friendlyName, IntPtr hCryptoProv, out SafePeerData defaultIdentity);
*/
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerNameToPeerHostName(string peerName, out SafePeerData peerHostName);
[DllImport(P2P, CharSet = CharSet.Unicode)]
internal extern static Int32 PeerHostNameToPeerName(string peerHostName, out SafePeerData peerName);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpRegister(string pcwzPeerName,
ref PEER_PNRP_REGISTRATION_INFO registrationInfo,
out SafePeerNameUnregister handle);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpUnregister(IntPtr handle);
[System.Security.SecurityCritical]
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpUpdateRegistration(SafePeerNameUnregister hRegistration,
ref PEER_PNRP_REGISTRATION_INFO registrationInfo);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpResolve(string pcwzPeerNAme,
string pcwzCloudName,
ref UInt32 pcEndPoints,
out SafePeerData pEndPoints);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpStartResolve(string pcwzPeerNAme,
string pcwzCloudName,
UInt32 cEndPoints,
SafeWaitHandle hEvent,
out SafePeerNameEndResolve safePeerNameEndResolve);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpGetEndpoint(IntPtr Handle,
out SafePeerData pEndPoint);
[DllImport(P2P, CharSet = CharSet.Unicode)]
public extern static Int32 PeerPnrpEndResolve(IntPtr Handle);
private static object s_InternalSyncObject;
private static bool s_Initialized;
private const int PNRP_VERSION = 2;
private static object InternalSyncObject {
get {
if (s_InternalSyncObject == null) {
object o = new object();
Interlocked.CompareExchange(ref s_InternalSyncObject, o, null);
}
return s_InternalSyncObject;
}
}
//
//
//
//
[System.Security.SecurityCritical]
internal static void PnrpStartup()
{
if (!s_Initialized) {
lock (InternalSyncObject) {
if (!s_Initialized) {
Int32 result = PeerPnrpStartup(PNRP_VERSION);
if (result != 0) {
throw new PeerToPeerException(SR.GetString(SR.Pnrp_StartupFailed), Marshal.GetExceptionForHR(result));
}
s_Initialized = true;
}
}
}
} //end of method PnrpStartup
}
//
//
//
[System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
[System.Security.SuppressUnmanagedCodeSecurityAttribute()]
internal sealed class SafePeerData : SafeHandleZeroOrMinusOneIsInvalid
{
private SafePeerData() : base(true) { }
//private SafePeerData(bool ownsHandle) : base(ownsHandle) { }
internal string UnicodeString
{
get
{
return Marshal.PtrToStringUni(handle);
}
}
protected override bool ReleaseHandle()
{
UnsafeP2PNativeMethods.PeerFreeData(handle);
SetHandleAsInvalid(); //Mark it closed - This does not change the value of the handle it self
SetHandle(IntPtr.Zero); //Mark it invalid - Change the value to Zero
return true;
}
}
//
//
//
[System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
[System.Security.SuppressUnmanagedCodeSecurityAttribute()]
internal sealed class SafePeerNameUnregister : SafeHandleZeroOrMinusOneIsInvalid
{
internal SafePeerNameUnregister() : base(true) { }
//internal SafePeerNameUnregister(bool ownsHandle) : base(ownsHandle) { }
protected override bool ReleaseHandle()
{
UnsafeP2PNativeMethods.PeerPnrpUnregister(handle);
SetHandleAsInvalid(); //Mark it closed - This does not change the value of the handle it self
SetHandle(IntPtr.Zero); //Mark it invalid - Change the value to Zero
return true;
}
}
//
//
//
[System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
[System.Security.SuppressUnmanagedCodeSecurityAttribute()]
internal sealed class SafePeerNameEndResolve : SafeHandleZeroOrMinusOneIsInvalid
{
internal SafePeerNameEndResolve() : base(true) { }
//internal SafePeerNameEndResolve(bool ownsHandle) : base(ownsHandle) { }
protected override bool ReleaseHandle()
{
UnsafeP2PNativeMethods.PeerPnrpEndResolve(handle);
SetHandleAsInvalid(); //Mark it closed - This does not change the value of the handle it self
SetHandle(IntPtr.Zero); //Mark it invalid - Change the value to Zero
return true;
}
}
///
/// Determines whether P2P is installed
/// Note static constructors are guaranteed to be
/// run in a thread safe manner. so no locks are necessary
///
internal static class PeerToPeerOSHelper
{
private static bool s_supportsP2P = false;
private static SafeLoadLibrary s_P2PLibrary = null;
//
//
//
//
//
//
[System.Security.SecurityCritical]
static PeerToPeerOSHelper() {
s_P2PLibrary = SafeLoadLibrary.LoadLibraryEx(UnsafeP2PNativeMethods.P2P);
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.
}
internal static bool SupportsP2P {
get {
return s_supportsP2P;
}
}
internal static IntPtr P2PModuleHandle
{
//
//
//
//
//
//
[System.Security.SecurityCritical]
[SecurityPermissionAttribute(SecurityAction.LinkDemand, UnmanagedCode=true)]
get
{
if (!s_P2PLibrary.IsClosed && !s_P2PLibrary.IsInvalid)
return s_P2PLibrary.DangerousGetHandle();
return IntPtr.Zero;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ActivityExecutorSurrogate.cs
- TextEditorMouse.cs
- DeclaredTypeElement.cs
- ProjectionCamera.cs
- DBBindings.cs
- StrokeCollectionConverter.cs
- SMSvcHost.cs
- ReaderContextStackData.cs
- EmptyImpersonationContext.cs
- X509PeerCertificateElement.cs
- PreviewKeyDownEventArgs.cs
- TypeViewSchema.cs
- ItemCheckEvent.cs
- AsynchronousChannelMergeEnumerator.cs
- DataViewSettingCollection.cs
- OleAutBinder.cs
- SecureConversationDriver.cs
- TextCharacters.cs
- WebExceptionStatus.cs
- HttpAsyncResult.cs
- Resources.Designer.cs
- ResourceDefaultValueAttribute.cs
- DataTableReader.cs
- ListControlBuilder.cs
- PartialList.cs
- SamlAttribute.cs
- DeviceContext.cs
- TypeBuilder.cs
- GridViewRowEventArgs.cs
- DbSetClause.cs
- DateTimeSerializationSection.cs
- Rect3D.cs
- DataGridViewImageCell.cs
- MouseButtonEventArgs.cs
- ImageFormatConverter.cs
- MenuItemStyle.cs
- BooleanKeyFrameCollection.cs
- TrustManager.cs
- Filter.cs
- Rect3DValueSerializer.cs
- SetterBase.cs
- ButtonAutomationPeer.cs
- UserCancellationException.cs
- SoapReflectionImporter.cs
- DataMemberListEditor.cs
- WebBrowsableAttribute.cs
- DetailsViewPagerRow.cs
- LinkedResourceCollection.cs
- MsmqTransportBindingElement.cs
- ResXBuildProvider.cs
- CodeIdentifiers.cs
- WindowsAuthenticationModule.cs
- PersonalizableTypeEntry.cs
- CodeMethodReturnStatement.cs
- HitTestWithPointDrawingContextWalker.cs
- MediaPlayerState.cs
- TextParaClient.cs
- InternalBufferOverflowException.cs
- UnsettableComboBox.cs
- FloaterBaseParagraph.cs
- RandomNumberGenerator.cs
- EdmMember.cs
- AtomParser.cs
- TypeDescriptionProviderAttribute.cs
- OverrideMode.cs
- SystemColors.cs
- OuterGlowBitmapEffect.cs
- XsltException.cs
- ChannelBuilder.cs
- PLINQETWProvider.cs
- Transform.cs
- DbResourceAllocator.cs
- GlobalEventManager.cs
- FormatException.cs
- NameValueSectionHandler.cs
- LinkLabel.cs
- ContentPresenter.cs
- EntityDataSourceEntityTypeFilterConverter.cs
- SignatureResourceHelper.cs
- TreeWalker.cs
- ListViewDesigner.cs
- AsymmetricAlgorithm.cs
- RtfControlWordInfo.cs
- ThousandthOfEmRealDoubles.cs
- DummyDataSource.cs
- ColorInterpolationModeValidation.cs
- DoubleLinkListEnumerator.cs
- DisplayInformation.cs
- TranslateTransform.cs
- UnsafeNativeMethods.cs
- Literal.cs
- GridView.cs
- RootBuilder.cs
- Quad.cs
- DataGridViewToolTip.cs
- CodeNamespaceCollection.cs
- TimeZone.cs
- InternalBase.cs
- UnsafeNativeMethodsTablet.cs
- Misc.cs