public void TestBuild() { if (File.Exists(BuildConst.OutFTStreamFileName)) File.Delete(BuildConst.OutFTStreamFileName); int propListCount = 15; E15PropertyItem[] propertyTags = new E15PropertyItem[propListCount]; tagBegin = 0x8001; for (int i = 0; i < propListCount; i++) { propertyTags[i] = GetProperty(); } tagBegin = 0x7990; List<E15PropertyItem[]> recvPropertyTagsAll = new List<E15PropertyItem[]>(2); int recvLength = 8; E15PropertyItem[] recvPropertyTags = new E15PropertyItem[recvLength]; for (int i = 0; i < recvLength; i++) { recvPropertyTags[i] = GetProperty(); } recvPropertyTagsAll.Add(recvPropertyTags); tagBegin = 0x7990; int recv2Length = 7; E15PropertyItem[] recv2PropertyTags = new E15PropertyItem[recv2Length]; for (int i = 0; i < recv2Length; i++) { recv2PropertyTags[i] = GetProperty(); } recvPropertyTagsAll.Add(recv2PropertyTags); tagBegin = 0x3000; int attachment1Length = 4; List<E15PropertyItem[]> attachmentCollectionTag = new List<E15PropertyItem[]>(1); E15PropertyItem attachmentNumber = new E15PropertyItem(); attachmentNumber.nTag = 0x0E210003; attachmentNumber.nUsID = 0x0E21; attachmentNumber.PropertyType = MAPIPropertyType.Int32; attachmentNumber.nLength = 4; byte[] attachNumberBuffer = BitConverter.GetBytes((Int32)1); IntPtr unmanagedAttachNumberPointer = Marshal.AllocHGlobal(4); allNeedFreePtr.Add(unmanagedAttachNumberPointer); Marshal.Copy(attachNumberBuffer, 0, unmanagedAttachNumberPointer,4); attachmentNumber.pValue = unmanagedAttachNumberPointer; E15PropertyItem[] attachPropList = new E15PropertyItem[attachment1Length]; for (int i = 0; i < attachment1Length; i++) { attachPropList[i] = GetProperty(); } attachmentCollectionTag.Add(attachPropList); //int hResult = 0; using (IItemBuild build = new ItemBuild()) { Assert.AreEqual(build.StartBuild(), 0); if (propertyTags.Length > 0) { using (IPropListBuild propList = build.BuildPropList()) { Assert.AreEqual(propList.StartBuild(), 0); Assert.AreEqual(propList.BuildProperties(GetPropertiesPtr(propertyTags, propListCount), propListCount), 0); Assert.AreEqual(propList.EndBuild(), 0); } } if (recvPropertyTagsAll.Count > 0) { using (IRecipientCollectionBuild recvCollectionBuild = build.BuildRecipientCollection()) { Assert.AreEqual(recvCollectionBuild.StartBuild(), 0); int recvIndex = 0; foreach (E15PropertyItem[] recvItems in recvPropertyTagsAll) { using (IRecipientBuild recvBuild = recvCollectionBuild.BuildRecipient(recvIndex++)) { Assert.AreEqual(recvBuild.StartBuild(), 0); Assert.AreEqual(recvBuild.BuildProperties(GetPropertiesPtr(recvItems, recvItems.Length), recvItems.Length), 0); Assert.AreEqual(recvBuild.EndBuild(), 0); } recvIndex++; } Assert.AreEqual(recvCollectionBuild.EndBuild(), 0); } } if (attachmentCollectionTag.Count > 0) { using (IAttachmentCollectionBuild attachmentCollectionBuild = build.BuildAttachmentCollection()) { int attachIndex = 0; foreach (E15PropertyItem[] attachmentItems in attachmentCollectionTag) { Assert.AreEqual(attachmentCollectionBuild.StartBuild(), 0); using (IAttachmentBuild attachmentBuild = attachmentCollectionBuild.BuildAttachment(attachIndex)) { Assert.AreEqual(attachmentBuild.StartBuild(), 0); Assert.AreEqual(attachmentBuild.BuildAttachmentNumber(GetPropertyPtr(attachmentNumber)), 0); Assert.AreEqual(attachmentBuild.BuildProperties(GetPropertiesPtr(attachmentItems, attachmentItems.Length), attachmentItems.Length), 0); //todo Build Embeded; Assert.AreEqual(attachmentBuild.EndBuild(), 0); } Assert.AreEqual(attachmentCollectionBuild.EndBuild(), 0); attachIndex++; } } } build.EndBuild(); } foreach(IntPtr ptr in allNeedFreePtr) { Marshal.FreeHGlobal(ptr); } }
E15PropertyItem GetProperty() { E15PropertyItem item = new E15PropertyItem(); item.PropertyType = GetPropertyType(); item.nLength = GetPropertyLength(item.PropertyType); item.nTag = GetPropertyTag(item.PropertyType); item.nUsID = (UInt16)(item.nTag >> 16); IntPtr unmanagedPointer = Marshal.AllocHGlobal(item.nLength); allNeedFreePtr.Add(unmanagedPointer); byte[] value = new byte[item.nLength]; SetValue(value, item.nLength, item.PropertyType); Marshal.Copy(value, 0, unmanagedPointer, item.nLength); item.pValue = unmanagedPointer; if (item.nUsID >= 0x8000) { item.namedID.ulKind = (uint)( tagBegin % 2 == 0 ? 0 : 1); Guid guid = Guid.NewGuid(); byte[] guidBuffer = guid.ToByteArray(); var data1 = (UInt32)BitConverter.ToInt32(guidBuffer,0); var data2 = (UInt16)BitConverter.ToInt16(guidBuffer,4); var data3 = (UInt16)BitConverter.ToInt16(guidBuffer,6); var data4 = new byte[8]; Array.Copy(guidBuffer, 8,data4 ,0,8); PropSetGUID propSetGuid = new PropSetGUID(); propSetGuid.Data1 = data1; propSetGuid.Data2 = data2; propSetGuid.Data3 = data3; propSetGuid.Data4 = data4; IntPtr guidPointer = Marshal.AllocHGlobal(Marshal.SizeOf(propSetGuid)); Marshal.StructureToPtr(propSetGuid, guidPointer, false); allNeedFreePtr.Add(guidPointer); item.namedID.lpguid = guidPointer; if (item.namedID.ulKind == (uint)0) { item.namedID.Kind.IID = (int)item.nTag; } else { int length = random.Next() %64; StringBuilder sb = new StringBuilder(); for(int index = 0 ; index < length - 2;index++) { sb.Append(totalData++ % 10); } IntPtr stringPointer = Marshal.StringToHGlobalUni(sb.ToString()); item.namedID.Kind.lpwstrName = stringPointer; allNeedFreePtr.Add(stringPointer); } } return item; }
private IntPtr GetPropertyPtr(E15PropertyItem property) { IntPtr propPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(E15PropertyItem))); Marshal.StructureToPtr(property, propPtr, false); allNeedFreePtr.Add(propPtr); return propPtr; }
private IntPtr GetPropertiesPtr(E15PropertyItem[] properties, int length) { IntPtr propListPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(E15PropertyItem))*length); long longPtr = propListPtr.ToInt64(); for (int i = 0; i < length; i++) { IntPtr itemPtr = new IntPtr(longPtr); allNeedFreePtr.Add(itemPtr); Marshal.StructureToPtr(properties[i], itemPtr, false); longPtr += Marshal.SizeOf(typeof(E15PropertyItem)); } return propListPtr; }