/// <summary>
        /// Gets a device capability represented as an array
        /// </summary>
        /// <param name="capability">Capability to retrieve</param>
        /// <returns>value of capability specified in capability argument</returns>
        private T[] GetArrayCapability <T>(DeviceCapability capability, ReadArray <T> readArray, int itemByteSize)
        {
            uint numOutputs = UnsafeNativeMethods.DeviceCapabilitiesW(this._deviceName, this._portName, capability, SafeMemoryHandle.Null, this._devMode);

            if (numOutputs < 1)
            {
                return(Array.Empty <T>());
            }

            HGlobalBuffer buffer = new HGlobalBuffer((int)(numOutputs * itemByteSize));

            try
            {
                numOutputs = UnsafeNativeMethods.DeviceCapabilitiesW(this._deviceName, this._portName, capability, buffer.Handle, this._devMode);
                if (numOutputs >= 0)
                {
                    return(readArray(buffer, itemByteSize));
                }
            }
            finally
            {
                buffer.Release();
            }

            return(null);
        }
Ejemplo n.º 2
0
        ///<summary>
        /// Executes an action delegate on an unmanaged buffer representing printer information
        ///</summary>
        ///<param name="dwLevel">Level of printer information to process</param>
        ///<param name="action">Delegate that processes printer information</param>
        ///<returns>False if tha call fails</returns>
        private bool GetPrinterW(uint dwLevel, Action <HGlobalBuffer> action)
        {
            uint dwNeeded = 0;

            UnsafeNativeMethods.GetPrinterW(this._deviceHandle, dwLevel, SafeMemoryHandle.Null, dwNeeded, ref dwNeeded);
            if (dwNeeded > 0)
            {
                HGlobalBuffer pPrinterBuffer = new HGlobalBuffer((int)dwNeeded);
                try
                {
                    if (UnsafeNativeMethods.GetPrinterW(this._deviceHandle, dwLevel, pPrinterBuffer.Handle, dwNeeded, ref dwNeeded))
                    {
                        action(pPrinterBuffer);
                        return(true);
                    }
                }
                finally
                {
                    pPrinterBuffer.Release();
                }
            }

            return(false);
        }