intrface.cpp

来自「W2kPrintDrvSample Feature ======= *」· C++ 代码 · 共 900 行 · 第 1/2 页

CPP
900
字号
/*
 * @file uni/intrface.cpp
 *
 * W2kPrintDrvSample
 *
 * Copyright (C) 2004, terrificskyfox <terrificskyfox@yahoo.com.cn>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "precomp.h"
#include <INITGUID.H>
#include <PRCOMOEM.H>
#include "debug.h"
#include "oem.h"
#include "oemuni.h"
#include "devmode.h"
#include "name.h"
#include "enable.h"
#include "imgproc.h"
#include "intrface.h"


/*
 *  Internal datas
 */

static long g_cComponents = 0;     // Count of active components
static long g_cServerLocks = 0;    // Count of locks


/*
 *  IOEMUni implementation
 */

/*
 * Function: IOEMUni::~IOEMUni
 *
 * Purpose: Destruction.
 *
 * Return: NULL
 */
IOEMUni::~IOEMUni()
{
    // make sure that helper interface is released
    if(NULL != m_pOEMHelp)
    {
        m_pOEMHelp->Release();
        m_pOEMHelp = NULL;
    }

    // If this instance of the object is being deleted, then the reference 
    // count should be zero.
    assert(0 == m_cRef);
}

/*
 * Function: IOEMUni::QueryInterface
 *
 * Purpose: 
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::QueryInterface(const IID& iid, void** ppv)
{
	VERBOSE(UNITEXT("IOEMUni::QueryInterface entry.\r\n"));

    if(iid == IID_IUnknown)
    {
        *ppv = static_cast<IUnknown*>(this); 
        VERBOSE(UNITEXT("IOEMUni: QueryInterface IUnknown.\r\n")); 
    }
    else if(iid == IID_IPrintOemUni)
    {
        *ppv = static_cast<IPrintOemUni*>(this);
        VERBOSE(UNITEXT("IOEMUni: QueryInterface IPrintOemUni.\r\n")); 
    }
    else
    {
        *ppv = NULL;
        VERBOSE(UNITEXT("IOEMUni: QueryInterface NULL. Returning E_NOINTERFACE.\r\n")); 
        return E_NOINTERFACE;
    }
    reinterpret_cast<IUnknown*>(*ppv)->AddRef();
    return S_OK;
}

/*
 * Function: IOEMUni::AddRef
 *
 * Purpose: Lifetime management.
 *
 * Return: ULONG
 */
ULONG __stdcall IOEMUni::AddRef()
{
    VERBOSE(UNITEXT("IOEMUni::AddRef() entry.\r\n"));

    return InterlockedIncrement(&m_cRef);
}

/*
 * Function: IOEMUni::Release
 *
 * Purpose: Lifetime management.
 *
 * Return: ULONG
 */
ULONG __stdcall IOEMUni::Release() 
{
    VERBOSE(UNITEXT("IOEMUni::Release() entry.\r\n"));

    if(InterlockedDecrement(&m_cRef) == 0)
    {
        delete this;
        return 0;
    }
    return m_cRef;
}


// *** Implemented methods ***

/*
 * Function: IOEMUni::DevMode
 *
 * Purpose: Performs operations on OEM DEVMODE members.
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::DevMode(
    DWORD       dwMode,
    POEMDMPARAM pOemDMParam)
{   
	VERBOSE(UNITEXT("IOEMUni:DevMode(%d) entry.\n"), dwMode);

	return hrOEMDevMode(dwMode, pOemDMParam);
}

/*
 * Function: IOEMUni::DisableDriver
 *
 * Purpose: Free resources that were allocated by EnableDriver method.
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::DisableDriver(VOID)
{
	TERSE(UNITEXT("IOEMUni::DisaleDriver() entry.\r\n"));

	OEMDisableDriver();

	// release reference to Unidrv's interface
	if(this->m_pOEMHelp)
    {
		this->m_pOEMHelp->Release();
		this->m_pOEMHelp = NULL;
    }

    return S_OK;
}

/*
 * Function: IOEMUni::DisablePDEV
 *
 * Purpose: Delete the oem PDEV structure that was allocated by EnablePDEV method.
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::DisablePDEV(
    PDEVOBJ         pdevobj)
{
	TERSE(UNITEXT("IOEMUni::DisablePDEV() entry.\r\n"));

	OEMDisablePDEV(pdevobj);

	return S_OK;
}

/*
 * Function: IOEMUni::DriverDMS
 *
 * Purpose: Allows rendering plug-in for Unidrv to indicate that it will
 *		    use a device-managed drawing surface.
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::DriverDMS(
    PVOID   pDevObj,
    PVOID   pBuffer,
    DWORD   cbSize,
    PDWORD  pcbNeeded)
{
	VERBOSE(UNITEXT("IOEMUni::DriverDMS() entry.\r\n"));

	return S_OK;
}

/*
 * Function: IOEMUni::EnableDriver
 *
 * Purpose: Hook out some DDI functions.
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::EnableDriver(
	DWORD          dwDriverVersion,
	DWORD          cbSize,
	PDRVENABLEDATA pded)
{
	TERSE(UNITEXT("IOEMUni::EnableDriver() entry.\r\n"));

	OEMEnableDriver(dwDriverVersion, cbSize, pded);

	// Even if nothing is done, need to return S_OK so 
	// that DisableDriver() will be called, which releases
	// the reference to the Printer Driver's interface.
	return S_OK;
}

/*
 * Function: IOEMUni::EnablePDEV
 *
 * Purpose: Create oem PDEV structure.
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::EnablePDEV(
    PDEVOBJ         pdevobj,
    PWSTR           pPrinterName,
    ULONG           cPatterns,
    HSURF          *phsurfPatterns,
    ULONG           cjGdiInfo,
    GDIINFO        *pGdiInfo,
    ULONG           cjDevInfo,
    DEVINFO        *pDevInfo,
    DRVENABLEDATA  *pded,
    OUT PDEVOEM    *pDevOem)
{
	TERSE(UNITEXT("IOEMUni::EnablePDEV() entry.\r\n"));

	*pDevOem = OEMEnablePDEV(pdevobj,
				 			 pPrinterName,
							 cPatterns,
							 phsurfPatterns,
							 cjGdiInfo,
							 pGdiInfo,
							 cjDevInfo,
							 pDevInfo,
							 pded,
							 this->m_pOEMHelp);

	return (NULL != *pDevOem ? S_OK : E_FAIL);
}

/*
 * Function: IOEMUni::GetImplementedMethod
 *
 * Purpose: Used by Unidrv to determine which IPrintOemUni interface methods
 *		    have been implemented by a rendering plug-in.
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::GetImplementedMethod(
	PSTR pMethodName)
{
	VERBOSE(UNITEXT("IOEMUni::GetImplementedMethod() entry.\r\n"));


	HRESULT Result = S_FALSE;

	switch(*pMethodName)
    {
	case 'I':
		if(!strcmp(NAME_ImageProcessing, pMethodName))
			Result = S_OK;

		break;
	}

	return Result;
}

/*
 * Function: IOEMUni::GetInfo
 *
 * Purpose: Returns identification information.
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::GetInfo (
    DWORD   dwMode,
    PVOID   pBuffer,
    DWORD   cbSize,
    PDWORD  pcbNeeded)
{
	VERBOSE(UNITEXT("IOEMUni::GetInfo(%d) entry.\r\n"), dwMode);

    // validate parameters
    if((NULL == pcbNeeded)
       ||
       ((OEMGI_GETSIGNATURE != dwMode) && (OEMGI_GETVERSION != dwMode) && (OEMGI_GETPUBLISHERINFO != dwMode))
      )
    {
        WARNING(UNITEXT("IOEMUni: GetInfo() exit pcbNeeded is NULL! ERROR_INVALID_PARAMETER.\r\n"));
        SetLastError(ERROR_INVALID_PARAMETER);
        return E_FAIL;
    }

    // set expected buffer size
    if(OEMGI_GETPUBLISHERINFO != dwMode)
    {
        *pcbNeeded = sizeof(DWORD);
    }
    else
    {
        *pcbNeeded = sizeof(PUBLISHERINFO);
        return E_FAIL;
    }

    // check buffer size is sufficient
    if((cbSize < *pcbNeeded) || (NULL == pBuffer))
    {
        VERBOSE(UNITEXT("IOEMUni: GetInfo() exit insufficient buffer!\r\n"));
        SetLastError(ERROR_INSUFFICIENT_BUFFER);
        return E_FAIL;
    }

    switch(dwMode)
    {
        // DLL Signature
        case OEMGI_GETSIGNATURE:
            *(PDWORD)pBuffer = OEM_SIGNATURE;
            break;

        // OEM DLL version
        case OEMGI_GETVERSION:
            *(PDWORD)pBuffer = OEM_VERSION;
            break;

        case OEMGI_GETPUBLISHERINFO:
            // fall through to default case

        // dwMode not supported
        default:
            // set written bytes to zero since nothing was written
            WARNING(UNITEXT("IOEMUni: GetInfo() exit mode not supported.\r\n"));
            *pcbNeeded = 0;
            SetLastError(ERROR_NOT_SUPPORTED);
            return E_FAIL;
    }

    return S_OK;
}

/*
 * Function: IOEMUni::ImageProcessing
 *
 * Purpose: Perform customized format convertion or halftoning.
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::ImageProcessing(
	PDEVOBJ             pdevobj,
	PBYTE               pSrcBitmap,
	PBITMAPINFOHEADER   pBitmapInfoHeader,
	PBYTE               pColorTable,
	DWORD               dwCallbackID,
	PIPPARAMS           pIPParams,
	OUT PBYTE           *ppbResult)
{
	TERSE(UNITEXT("IOEMUni::ImageProcessing() entry.\r\n"));


	POEMPDEV pOemPDEV = (POEMPDEV)pdevobj->pdevOEM;


	// record information for processed bands
	if(!pOemPDEV->DIBInfo.bPageStarted)
	{
		pOemPDEV->DIBInfo.DIBSize.cx = pBitmapInfoHeader->biWidth;
		pOemPDEV->DIBInfo.DIBSize.cy = 0;

        if(dwCallbackID == 24) // 24bpp
            pOemPDEV->DIBInfo.WidthBytes = (pOemPDEV->DIBInfo.DIBSize.cx * 24 + 31 ) / 32 * 4;
        else // 1bpp
		    pOemPDEV->DIBInfo.WidthBytes = (pOemPDEV->DIBInfo.DIBSize.cx + 31) / 32 * 4;

		pOemPDEV->DIBInfo.dwDIBTotalBytes = 0;

		pOemPDEV->DIBInfo.bPageStarted = TRUE;
	}

    // dump bits
    dump(pdevobj, pSrcBitmap, pBitmapInfoHeader, pIPParams);

	// we do NOT need sending data to spooler or moving cursor,
	// so there is NO need to call IPrintOemDriverUni::DrvWriteSpoolBuf,
	// IPrintOemDriverUni::DrvXMoveTo or IPrintOemDriverUni::DrvYMoveTo.

	// it is very important to return bmp pointer to Unidrv,
	// otherwise, NextBand will return FALSE.
	*ppbResult = pSrcBitmap;

	return S_OK;
}

/*
 * Function: IOEMUni::PublishDriverInterface
 *
 * Purpose: Obtain the Unidrv driver's IPrintOemDriverUni interface.
 *
 * Return: HRESULT
 */
HRESULT __stdcall IOEMUni::PublishDriverInterface(
	IUnknown *pIUnknown)
{
	VERBOSE(UNITEXT("IOEMUni::PublishDriverInterface() entry.\r\n"));

	// need to store pointer to Driver Helper functions, if we already haven't
	if(this->m_pOEMHelp == NULL)
	{
		HRESULT hResult;

		// get Interface to Helper Functions
		hResult = pIUnknown->QueryInterface(IID_IPrintOemDriverUni, (void** ) &(this->m_pOEMHelp));

		if(FAILED(hResult))
		{
			// make sure that interface pointer reflects interface query failure
			this->m_pOEMHelp = NULL;
			return E_FAIL;
		}
	}

	return S_OK;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?