c++底层操作打印任务,最大权限获取打印任务各种信息。

#include <iostream>
#include <string.h>
#include <windows.h>
#include <winspool.h>
#include <stdlib.h>
#include <cstring>
using namespace std;

//打印机信息
struct PRINTINFO {
    char* PrintName;//打印机名称
    char* PrintDiver;//打印机驱动名称
};

struct JOBINFO {
    int JobId; //任务编号
    int Status; //任务状态
    char* Document; //文档名称
    int TotalPages; //文档页数
    int PageSize; //纸张大小
    int Copyes;//打印份数
    int Color;//打印颜色
    int Orientation;//纸张方向
    int Duplex;//是否为双面 1-单面 其他-双面
    int Collate;//是否逐份打印
    char* FormName;//纸张名称
    int MediaType;//纸质
};

struct JOB
{
    int JobId;
    int MediaType;
    int PageSize;
    int Color;
    int Orientation;
    int Duplex;
    int Copyes;
};

PRINTINFO* PRINTINFOARRAY;
JOBINFO* JOBINFOARRAY;

//纸张大小
int GetPageSize(short pagesize)
{
    int size = 3001;//A4
    switch (pagesize) {
    case DMPAPER_B5:
        size = 3002;
        break;
    case DMPAPER_A3:
        size = 3003;
        break;
    case DMPAPER_A2:
        size = 3006;
        break;
    case DMPAPER_A5:
        size = 3007;
        break;
    case DMPAPER_A6:
        size = 3008;
        break;
    case DMPAPER_B4:
        size = 3010;
        break;
    case DMPAPER_LETTER:
        size = 3011;
        break;
    default:
        size = 3001;
    }
    return size;
}

//打印类型
int GetDuplex(short flag) {
    int state = 2002;
    switch (flag) {
    case DMDUP_SIMPLEX:
        state = 2001;
        break;
    default:
        state = 2002;
    }
    return state;
}

//打印颜色
int GetColor(short color) {
    int state = 4001;
    switch (color) {
    case DMCOLOR_COLOR:
        state = 4002;
        break;
    default:
        state = 4001;
    }
    return state;
}

//纸质
int GetMediaType(short flag) {
    int state = 1001;
    switch (flag) {
    case DMMEDIA_STANDARD:
        state = 1001;
        break;
    case DMMEDIA_TRANSPARENCY:
        state = 1013;
        break;
    case DMMEDIA_GLOSSY:
        state = 1004;
        break;
    default:
        state = 1001;
    }
    return state;
}

wchar_t* CharToWchar(const char* c)
{
    wchar_t *m_wchar;
    int len = MultiByteToWideChar(CP_ACP, 0, c, strlen(c), NULL, 0);
    m_wchar = new wchar_t[len + 1];
    MultiByteToWideChar(CP_ACP, 0, c, strlen(c), m_wchar, len);
    m_wchar[len] = '\0';
    return m_wchar;
}

char* WcharToChar(const wchar_t* wp)
{
    char *m_char;
    int len = WideCharToMultiByte(CP_ACP, 0, wp, wcslen(wp), NULL, 0, NULL, NULL);
    m_char = new char[len + 1];
    WideCharToMultiByte(CP_ACP, 0, wp, wcslen(wp), m_char, len, NULL, NULL);
    m_char[len] = '\0';
    return m_char;
}


//获取本地打印机数量
extern "C" __declspec(dllexport) int GetPrintersCount() {
    DWORD Flags = PRINTER_ENUM_FAVORITE | PRINTER_ENUM_LOCAL;
    DWORD cbBuf;
    DWORD pcReturned;
    DWORD Level = 2;
    TCHAR Name[500] = {0};
    ::EnumPrinters(Flags, Name, Level, NULL, 0, &cbBuf, &pcReturned);
    const LPPRINTER_INFO_2 pPrinterEnum = (LPPRINTER_INFO_2)LocalAlloc(LPTR, cbBuf + 4);
    if (!pPrinterEnum)
    {
        return -1;
    }
    if (!EnumPrinters(Flags, Name, Level, (LPBYTE)pPrinterEnum, cbBuf, &cbBuf, &pcReturned))
    {
        GlobalFree((HGLOBAL)pPrinterEnum);
        return -2;
    }
    if (pcReturned > 0)
    {
        PRINTINFO *printinfoarray = new PRINTINFO[2];
        for (unsigned int j = 0; j < pcReturned; j++)
        {
            LPPRINTER_INFO_2 pInfo = &pPrinterEnum[j];
            printinfoarray[j].PrintName = pInfo->pPrinterName;
            printinfoarray[j].PrintDiver = pInfo->pDriverName;
        }
        PRINTINFOARRAY = printinfoarray;
    }
    return pcReturned;
}

//获取打印机列表
extern "C" __declspec(dllexport) void GetPrinters(PRINTINFO* refPrints[],int length) {
    for (int j = 0; j < length; j++) {
        refPrints[j]->PrintName = PRINTINFOARRAY[j].PrintName;
        refPrints[j]->PrintDiver = PRINTINFOARRAY[j].PrintDiver;
    }
}

//获取打印机JOBS详细信息 返回打印机当前任务数量。
extern "C" __declspec(dllexport) int GetJobsCount(LPSTR printNamestr) {
    HANDLE hPrinter;
    LPSTR printName = printNamestr;
    DWORD dwNeeded, dwReturne;
    JOB_INFO_2 * pJobInfo = NULL;

    //打开打印机
    if (!OpenPrinter(printName, &hPrinter, NULL)) {
        return -1;
    }
    //获得需要的内存数量
    if (!EnumJobs(hPrinter, 0, 127, 2, NULL, 0, &dwNeeded, &dwReturne))
    {
        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
        {
            ClosePrinter(hPrinter);
            return -2;
        }
    }
    //分配内存
    pJobInfo = (JOB_INFO_2*)GlobalAlloc(GPTR, dwNeeded);
    if (!pJobInfo)
    {
        //分配内存失败
        ClosePrinter(hPrinter);
        return -3;
    }

    //获得JOB_INFO_2的数组
    if (!EnumJobs(hPrinter, 0, 127, 2, (LPBYTE)pJobInfo, dwNeeded, &dwNeeded, &dwReturne))
    {
        ClosePrinter(hPrinter);
        GlobalFree((HGLOBAL)pJobInfo);
        return -4;

    }

    //关闭打印机
    ClosePrinter(hPrinter);

    if (dwReturne > 0) {

        JOBINFO *jobinfoarray = new JOBINFO[2];
        for (unsigned int j = 0; j < dwReturne; j++)
        {
            jobinfoarray[j].JobId = pJobInfo[j].JobId;
            jobinfoarray[j].Status = pJobInfo[j].Status;
            jobinfoarray[j].TotalPages = pJobInfo[j].TotalPages;
            jobinfoarray[j].Document = pJobInfo[j].pDocument;
            jobinfoarray[j].PageSize = GetPageSize(pJobInfo[j].pDevMode->dmPaperSize);
            jobinfoarray[j].Copyes = pJobInfo[j].pDevMode->dmCopies;
            jobinfoarray[j].Color = GetColor(pJobInfo[j].pDevMode->dmColor);
            jobinfoarray[j].Orientation = pJobInfo[j].pDevMode->dmOrientation;
            jobinfoarray[j].Duplex = GetDuplex(pJobInfo[j].pDevMode->dmDuplex);
            jobinfoarray[j].Collate = pJobInfo[j].pDevMode->dmCollate;
            jobinfoarray[j].FormName = (char*)pJobInfo[j].pDevMode->dmFormName;
            jobinfoarray[j].MediaType = GetMediaType(pJobInfo[j].pDevMode->dmMediaType);
        }
        JOBINFOARRAY = jobinfoarray;
    }
    return dwReturne;
}

//获取当前打印任务详细情况
extern "C" __declspec(dllexport) void GetJobs(JOBINFO* refJOBINFROArray[], int length) {

    for (int j = 0; j < length; j++) {

        refJOBINFROArray[j]->JobId = JOBINFOARRAY[j].JobId;
        refJOBINFROArray[j]->Status = JOBINFOARRAY[j].Status;
        refJOBINFROArray[j]->TotalPages = JOBINFOARRAY[j].TotalPages;
        refJOBINFROArray[j]->Document = JOBINFOARRAY[j].Document;
        refJOBINFROArray[j]->PageSize = JOBINFOARRAY[j].PageSize;
        refJOBINFROArray[j]->Copyes = JOBINFOARRAY[j].Copyes;
        refJOBINFROArray[j]->Color = JOBINFOARRAY[j].Color;
        refJOBINFROArray[j]->Orientation = JOBINFOARRAY[j].Orientation;
        refJOBINFROArray[j]->Duplex = JOBINFOARRAY[j].Duplex;
        refJOBINFROArray[j]->Collate = JOBINFOARRAY[j].Collate;
        refJOBINFROArray[j]->FormName = JOBINFOARRAY[j].FormName;
        refJOBINFROArray[j]->MediaType = JOBINFOARRAY[j].MediaType;
    }

}

//获取打印任务详细情况
extern "C" __declspec(dllexport) void GetJobInfo(JOB* refJOBINFRO, char* name,int jobid) {
    HANDLE hPrinter;
    LPSTR printName = name;
    DWORD pcbNeeded;
    JOB_INFO_2* pJobInfo = NULL;
    if (!OpenPrinter(printName, &hPrinter, NULL)) {return;}
    ::GetJob(hPrinter,jobid,2, NULL, 0,&pcbNeeded);
    pJobInfo = (JOB_INFO_2*)GlobalAlloc(GPTR, pcbNeeded);
    GetJob(hPrinter, jobid, 2, (LPBYTE)pJobInfo, pcbNeeded, &pcbNeeded);
    ClosePrinter(hPrinter);
    if (pcbNeeded>0)
    {
        refJOBINFRO->JobId = pJobInfo->JobId;
        refJOBINFRO->MediaType = GetMediaType(pJobInfo->pDevMode->dmMediaType);
        refJOBINFRO->PageSize = GetPageSize(pJobInfo->pDevMode->dmPaperSize);
        refJOBINFRO->Color = GetColor(pJobInfo->pDevMode->dmColor);
        refJOBINFRO->Orientation = pJobInfo->pDevMode->dmOrientation;
        refJOBINFRO->Duplex = GetDuplex(pJobInfo->pDevMode->dmDuplex);
        refJOBINFRO->Copyes = pJobInfo->pDevMode->dmCopies;
    }
}
最后修改:2022 年 06 月 17 日 10 : 35 AM
如果觉得我的文章对你有用,请随意赞赏