private static GCHandle GetSecurityDescriptorHandle(QueuePath queuePath, int securityInformation) { byte[] securityDescriptorBytes; int length; int lengthNeeded; uint result; string formatName = queuePath.ToString(); result = Security.MQGetQueueSecurity(formatName, securityInformation, IntPtr.Zero, 0, out lengthNeeded); if (result != Security.MQ_ERROR_SECURITY_DESCRIPTOR_TOO_SMALL) { string message = "There was an error calling MQGetQueueSecurity." + Environment.NewLine + "Error Number: " + result.ToString() + Environment.NewLine + "Error Message: " + Security.GetErrorMessage(result); throw new Exception(message); } length = lengthNeeded; securityDescriptorBytes = new byte[length]; IntPtr pSecurityDescriptor = new IntPtr(); GCHandle gchSecurityDescriptor = GCHandle.Alloc(securityDescriptorBytes, GCHandleType.Pinned); pSecurityDescriptor = gchSecurityDescriptor.AddrOfPinnedObject(); result = Security.MQGetQueueSecurity(formatName, securityInformation, pSecurityDescriptor, length, out lengthNeeded); if (result != Security.MQ_OK) { gchSecurityDescriptor.Free(); string message = "There was an error calling MQGetQueueSecurity to read the Security Descriptor. " + Environment.NewLine + "Error Number: " + result.ToString() + Environment.NewLine + "Error Message: " + Security.GetErrorMessage(result); throw new Exception(message); } var securityDescriptor = new SECURITY_DESCRIPTOR(); Marshal.PtrToStructure(pSecurityDescriptor, securityDescriptor); return(gchSecurityDescriptor); }
private static GCHandle GetSecurityDescriptorHandle(QueuePath queuePath, int securityInformation) { byte[] securityDescriptorBytes; int length; int lengthNeeded; uint result; string formatName = queuePath.ToString(); result = Security.MQGetQueueSecurity(formatName, securityInformation, IntPtr.Zero, 0, out lengthNeeded); if (result != Security.MQ_ERROR_SECURITY_DESCRIPTOR_TOO_SMALL) { string message = "There was an error calling MQGetQueueSecurity." + Environment.NewLine + "Error Number: " + result.ToString() + Environment.NewLine + "Error Message: " + Security.GetErrorMessage(result); throw new Exception(message); } length = lengthNeeded; securityDescriptorBytes = new byte[length]; IntPtr pSecurityDescriptor = new IntPtr(); GCHandle gchSecurityDescriptor = GCHandle.Alloc(securityDescriptorBytes, GCHandleType.Pinned); pSecurityDescriptor = gchSecurityDescriptor.AddrOfPinnedObject(); result = Security.MQGetQueueSecurity(formatName, securityInformation, pSecurityDescriptor, length, out lengthNeeded); if (result != Security.MQ_OK) { gchSecurityDescriptor.Free(); string message = "There was an error calling MQGetQueueSecurity to read the Security Descriptor. " + Environment.NewLine + "Error Number: " + result.ToString() + Environment.NewLine + "Error Message: " + Security.GetErrorMessage(result); throw new Exception(message); } var securityDescriptor = new SECURITY_DESCRIPTOR(); Marshal.PtrToStructure(pSecurityDescriptor, securityDescriptor); return gchSecurityDescriptor; }