Ejemplo n.º 1
0
        /// <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);
                }
            }
        }
 public WinSpoolPrinterCapabilities(string deviceName, string driverName, string portName, DevMode devMode)
 {
     this._deviceName = deviceName;
     this._driverName = driverName;
     this._portName   = portName;
     if (devMode != null)
     {
         SafeMemoryHandle buffer = SafeMemoryHandle.Create(devMode.ByteData.Length);
         buffer.CopyFromArray(devMode.ByteData, 0, devMode.ByteData.Length);
         this._devMode = buffer;
     }
     else
     {
         this._devMode = SafeMemoryHandle.Null;
     }
 }
Ejemplo n.º 3
0
        /// <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();
            }
        }