/// <summary> /// Updates the current printer device default DEVMODE /// </summary> /// <param name="devModeBytes">New DEVMODE settings to apply</param> /// <param name="biDirectional">If true updates the devModeBytes array with resolved merge conflicts</param> /// </summary> private void SetDocumentProperties(byte[] devModeBytes, bool biDirectional) { long result = -1; DocumentPropertiesFlags flags = biDirectional ? (DocumentPropertiesFlags.DM_IN_BUFFER | DocumentPropertiesFlags.DM_OUT_BUFFER) : DocumentPropertiesFlags.DM_IN_BUFFER; SafeMemoryHandle outPtr = SafeMemoryHandle.Null; using (SafeMemoryHandle buffer = SafeMemoryHandle.Create(devModeBytes.Length)) { buffer.CopyFromArray(devModeBytes, 0, devModeBytes.Length); if (biDirectional) { outPtr = buffer; } result = UnsafeNativeMethods.DocumentPropertiesW(new HandleRef(this, IntPtr.Zero), this._deviceHandle, this._deviceName, outPtr, buffer, flags); if (result < 0) { throw new Win32Exception(); } if (!outPtr.IsInvalid) { outPtr.CopyToArray(devModeBytes, 0, devModeBytes.Length); } } }
/// <summary> /// Updates the current printer device default DEVMODE /// </summary> /// <param name="devModeBytes">New DEVMODE settings to apply</param> /// <param name="biDirectional">If true updates the devModeBytes array with resolved merge conflicts</param> /// </summary> private void SetDocumentProperties(byte[] devModeBytes, bool biDirectional) { long result = -1; DocumentPropertiesFlags flags = biDirectional ? (DocumentPropertiesFlags.DM_IN_BUFFER | DocumentPropertiesFlags.DM_OUT_BUFFER) : DocumentPropertiesFlags.DM_IN_BUFFER; SafeMemoryHandle outPtr = SafeMemoryHandle.Null; // Assert Unmanaged code - to allocate a native buffer for DocumentPropertiesW // - and read from\write to that buffer new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); try { using (SafeMemoryHandle buffer = SafeMemoryHandle.Create(devModeBytes.Length)) { buffer.CopyFromArray(devModeBytes, 0, devModeBytes.Length); if (biDirectional) { outPtr = buffer; } result = UnsafeNativeMethods.DocumentPropertiesW(new HandleRef(this, IntPtr.Zero), this._deviceHandle, this._deviceName, outPtr, buffer, flags); if (result < 0) { throw new Win32Exception(); } if (!outPtr.IsInvalid) { outPtr.CopyToArray(devModeBytes, 0, devModeBytes.Length); } } } finally { CodeAccessPermission.RevertAssert(); } }