public static void SetObject(PeerObject peerObject) { PeerCollaborationPermission.UnrestrictedPeerCollaborationPermission.Demand(); if (peerObject == null){ throw new ArgumentNullException("peerObject"); } CollaborationHelperFunctions.Initialize(); if (Logging.P2PTraceSource.Switch.ShouldTrace(TraceEventType.Information)){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "SetObject() is called with the following info"); peerObject.TracePeerObject(); } PEER_OBJECT po = new PEER_OBJECT(); int errorCode = 0; // // Create the native PEER_OBJECT struct // po.guid = CollaborationHelperFunctions.ConvertGuidToGUID(peerObject.Id); po.dwPublicationScope = (uint)peerObject.PeerScope; SafeCollabMemory data = null; try{ if ((peerObject.Data != null) && (peerObject.Data.Length > 0)){ data = new SafeCollabMemory(peerObject.Data.Length); po.data.pbData = data.DangerousGetHandle(); po.data.cbData = (UInt32)peerObject.Data.Length; Marshal.Copy(peerObject.Data, 0, po.data.pbData, peerObject.Data.Length); } else{ po.data.pbData = IntPtr.Zero; po.data.cbData = 0; } errorCode = UnsafeCollabNativeMethods.PeerCollabSetObject(ref po); } finally{ if (data != null) data.Dispose(); } if (errorCode == UnsafeCollabReturnCodes.PEER_E_ALREADY_EXISTS){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabSetObject returned with errorcode {0}. Object already set.", errorCode); throw new ArgumentException(SR.GetString(SR.Collab_ObjectSetFailed) + " " + SR.GetString(SR.Collab_ObjectExists)); } else if (errorCode != 0){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabSetObject returned with errorcode {0}", errorCode); throw (PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_ObjectSetFailed), errorCode)); } Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Set Object successful."); }
internal static PEER_CONTACT ConvertPeerContactToPEER_CONTACT(PeerContact peerContact, ref SafeCollabMemory safeCredentials) { PEER_CONTACT pc = new PEER_CONTACT(); pc.pwzDisplayName = peerContact.DisplayName; pc.pwzEmailAddress = (peerContact.EmailAddress == null) ? null : peerContact.EmailAddress.ToString(); pc.pwzNickname = peerContact.Nickname; pc.pwzPeerName = peerContact.PeerName.ToString(); pc.fWatch = peerContact.IsSubscribed; pc.WatcherPermissions = peerContact.SubscribeAllowed; PEER_DATA pd = new PEER_DATA(); if (peerContact.Credentials != null){ SafeCertStore certHandle = UnsafeCollabNativeMethods.CertOpenStore(new IntPtr(/*CERT_STORE_PROV_MEMORY*/ 2), 0, IntPtr.Zero, 0x00002000/*CERT_STORE_CREATE_NEW_FLAG*/ | 0x00000001/*CERT_STORE_NO_CRYPT_RELEASE_FLAG*/, IntPtr.Zero); if (certHandle == null || certHandle.IsInvalid){ int win32ErrorCode = Marshal.GetLastWin32Error(); throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CredentialsError), win32ErrorCode); } try{ X509Store certStore = new X509Store(certHandle.DangerousGetHandle()); certStore.Add(peerContact.Credentials as X509Certificate2); bool returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle, 0x00000001/*X509_ASN_ENCODING*/| 0x00010000/*PKCS_7_ASN_ENCODING*/, 2 /*CERT_STORE_SAVE_AS_STORE*/, 2, /*CERT_STORE_SAVE_TO_MEMORY*/ ref pd, 0); if ((pd.cbData != 0) && (returnCode)){ safeCredentials = new SafeCollabMemory((int)pd.cbData); pd.pbData = safeCredentials.DangerousGetHandle(); returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle, 0x00000001/*X509_ASN_ENCODING*/| 0x00010000/*PKCS_7_ASN_ENCODING*/, 2 /*CERT_STORE_SAVE_AS_STORE*/, 2, /*CERT_STORE_SAVE_TO_MEMORY*/ ref pd,// Clean up memory from here; 0); } else{ pd.cbData = 0; pd.pbData = IntPtr.Zero; } } finally{ if (certHandle != null) certHandle.Dispose(); } } else{ pd.cbData = 0; pd.pbData = IntPtr.Zero; } pc.credentials = pd; return pc; }
public static void RegisterApplication(PeerApplication application, PeerApplicationRegistrationType type) { PeerCollaborationPermission.UnrestrictedPeerCollaborationPermission.Demand(); if (application == null){ throw new ArgumentNullException("application"); } if (application.Path == null){ throw new ArgumentException(SR.GetString(SR.Collab_AppRegNoPathError)); } if ((type < PeerApplicationRegistrationType.CurrentUser) || (type > PeerApplicationRegistrationType.AllUsers)){ throw new ArgumentOutOfRangeException("type"); } CollaborationHelperFunctions.Initialize(); int errorCode = 0; // // Convert PeerApplication.Guid into native GUID struct // PEER_APPLICATION_REGISTRATION_INFO appRegInfo = new PEER_APPLICATION_REGISTRATION_INFO(); appRegInfo.application.guid = CollaborationHelperFunctions.ConvertGuidToGUID(application.Id); appRegInfo.pwzApplicationArguments = application.CommandLineArgs; appRegInfo.pwzApplicationToLaunch = application.Path; appRegInfo.dwPublicationScope = (uint)application.PeerScope; if (Logging.P2PTraceSource.Switch.ShouldTrace(TraceEventType.Information)){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "RegisterApplication() is called with the following Info"); application.TracePeerApplication(); } unsafe{ SafeCollabMemory data = null; appRegInfo.application.data.cbData = (application.Data!=null) ? (UInt32)application.Data.Length : 0; GCHandle descHandle = new GCHandle(); try{ // // Marshal any data to send to native call // if ((application.Data!=null) && (application.Data.Length > 0)) { data = new SafeCollabMemory(application.Data.Length); appRegInfo.application.data.pbData = data.DangerousGetHandle(); Marshal.Copy(application.Data, 0, appRegInfo.application.data.pbData, application.Data.Length); } else appRegInfo.application.data.pbData = IntPtr.Zero; descHandle = GCHandle.Alloc(application.Description, GCHandleType.Pinned); appRegInfo.application.pwzDescription = descHandle.AddrOfPinnedObject(); errorCode = UnsafeCollabNativeMethods.PeerCollabRegisterApplication(ref appRegInfo, type); } finally{ if (descHandle.IsAllocated) descHandle.Free(); if (data != null) data.Dispose(); } } if (errorCode == UnsafeCollabReturnCodes.PEER_E_ALREADY_EXISTS){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabRegisterApplication returned with errorcode {0}. Application already registered.", errorCode); throw new ArgumentException(SR.GetString(SR.Collab_AppRegFailed) + " " + SR.GetString(SR.Collab_AppExists)); } else if (errorCode != 0){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabRegisterApplication returned with errorcode {0}", errorCode); throw (PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_AppRegFailed), errorCode)); } Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "RegisterApplication successful"); }
internal static PEER_CONTACT ConvertPeerContactToPEER_CONTACT(PeerContact peerContact, ref SafeCollabMemory safeCredentials) { PEER_CONTACT pc = new PEER_CONTACT(); pc.pwzDisplayName = peerContact.DisplayName; pc.pwzEmailAddress = (peerContact.EmailAddress == null) ? null : peerContact.EmailAddress.ToString(); pc.pwzNickname = peerContact.Nickname; pc.pwzPeerName = peerContact.PeerName.ToString(); pc.fWatch = peerContact.IsSubscribed; pc.WatcherPermissions = peerContact.SubscribeAllowed; PEER_DATA pd = new PEER_DATA(); if (peerContact.Credentials != null) { SafeCertStore certHandle = UnsafeCollabNativeMethods.CertOpenStore(new IntPtr(/*CERT_STORE_PROV_MEMORY*/ 2), 0, IntPtr.Zero, 0x00002000 /*CERT_STORE_CREATE_NEW_FLAG*/ | 0x00000001 /*CERT_STORE_NO_CRYPT_RELEASE_FLAG*/, IntPtr.Zero); if (certHandle == null || certHandle.IsInvalid) { int win32ErrorCode = Marshal.GetLastWin32Error(); throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CredentialsError), win32ErrorCode); } try{ X509Store certStore = new X509Store(certHandle.DangerousGetHandle()); certStore.Add(peerContact.Credentials as X509Certificate2); bool returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle, 0x00000001 /*X509_ASN_ENCODING*/ | 0x00010000 /*PKCS_7_ASN_ENCODING*/, 2 /*CERT_STORE_SAVE_AS_STORE*/, 2, /*CERT_STORE_SAVE_TO_MEMORY*/ ref pd, 0); if ((pd.cbData != 0) && (returnCode)) { safeCredentials = new SafeCollabMemory((int)pd.cbData); pd.pbData = safeCredentials.DangerousGetHandle(); returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle, 0x00000001 /*X509_ASN_ENCODING*/ | 0x00010000 /*PKCS_7_ASN_ENCODING*/, 2 /*CERT_STORE_SAVE_AS_STORE*/, 2, /*CERT_STORE_SAVE_TO_MEMORY*/ ref pd, // Clean up memory from here; 0); } else { pd.cbData = 0; pd.pbData = IntPtr.Zero; } } finally{ if (certHandle != null) { certHandle.Dispose(); } } } else { pd.cbData = 0; pd.pbData = IntPtr.Zero; } pc.credentials = pd; return(pc); }