diff --git a/src/Messaging/Interop/MessagePropertyVariants.cs b/src/Messaging/Interop/MessagePropertyVariants.cs index aea2134..5724a71 100644 --- a/src/Messaging/Interop/MessagePropertyVariants.cs +++ b/src/Messaging/Interop/MessagePropertyVariants.cs @@ -6,7 +6,7 @@ namespace MSMQ.Messaging.Interop { - + // definition for tagMQPROPVARIANT @@ -309,8 +309,10 @@ public virtual MQPROPS Lock() newVectorProperties[usedProperties].vt = vt; if (vt == (short)(VT_VECTOR | VT_UI1)) { - if (handles[i] == null) + if (handles[i] == null || handles[i] is GCHandle) + { newVectorProperties[usedProperties].caub.cElems = (uint)((byte[])objects[i]).Length; + } else newVectorProperties[usedProperties].caub.cElems = (uint)handles[i]; @@ -342,6 +344,7 @@ public virtual MQPROPS Lock() } handleVectorIdentifiers = GCHandle.Alloc(newVectorIdentifiers, GCHandleType.Pinned); + handleVectorProperties = GCHandle.Alloc(newVectorProperties, GCHandleType.Pinned); handleVectorStatus = GCHandle.Alloc(newVectorStatus, GCHandleType.Pinned); vectorIdentifiers = newVectorIdentifiers; @@ -351,6 +354,7 @@ public virtual MQPROPS Lock() reference.propertyIdentifiers = handleVectorIdentifiers.AddrOfPinnedObject(); reference.propertyValues = handleVectorProperties.AddrOfPinnedObject(); reference.status = handleVectorStatus.AddrOfPinnedObject(); + return reference; } @@ -409,7 +413,12 @@ public virtual void Unlock() } else if (vt == VT_LPWSTR || vt == VT_CLSID || vt == (short)(VT_VECTOR | VT_UI1)) { - ((GCHandle)handles[vectorIdentifiers[i] - basePropertyId]).Free(); + object obj = handles[vectorIdentifiers[i] - basePropertyId]; + if (obj is GCHandle) + { + ((GCHandle)obj).Free(); + } + handles[vectorIdentifiers[i] - basePropertyId] = null; } else if (vt == VT_UI1 || vt == VT_I1) @@ -423,9 +432,20 @@ public virtual void Unlock() } - handleVectorIdentifiers.Free(); - handleVectorProperties.Free(); - handleVectorStatus.Free(); + if (handleVectorIdentifiers.IsAllocated) + { + handleVectorIdentifiers.Free(); + } + + if (handleVectorProperties.IsAllocated) + { + handleVectorProperties.Free(); + } + + if (handleVectorStatus.IsAllocated) + { + handleVectorStatus.Free(); + } } [StructLayout(LayoutKind.Sequential)]