打印机的端口设置

来源:互联网 发布:android 淘宝订单效果 编辑:程序博客网 时间:2024/04/30 14:58

// PrintPortTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <winspool.h>
#include <iostream>
using namespace std;


#define MAX_PORTNAME_LEN                63 +1       // port name length
#define MAX_NETWORKNAME_LEN             48 +1       // host name length
#define MAX_SNMP_COMMUNITY_STR_LEN      32 +1       // SNMP Community String Name
#define MAX_QUEUENAME_LEN               32 +1       // lpr print que name
#define MAX_IPADDR_STR_LEN              15 +1       // ip address; string version
#define MAX_ADDRESS_STR_LEN             12 +1       // hw address length
#define MAX_DEVICEDESCRIPTION_STR_LEN   256+1      


typedef struct _PORT_DATA_1 {
    WCHAR  sztPortName[MAX_PORTNAME_LEN];
    DWORD  dwVersion;
    DWORD  dwProtocol;
    DWORD  cbSize;
    DWORD  dwReserved;
    WCHAR  sztHostAddress[MAX_NETWORKNAME_LEN];
    WCHAR  sztSNMPCommunity[MAX_SNMP_COMMUNITY_STR_LEN];
    DWORD  dwDoubleSpool;
    WCHAR  sztQueue[MAX_QUEUENAME_LEN];
    WCHAR  sztIPAddress[MAX_IPADDR_STR_LEN];
    BYTE   Reserved[540];
    DWORD  dwPortNumber;
    DWORD  dwSNMPEnabled;
    DWORD  dwSNMPDevIndex;
} PORT_DATA_1, *PPORT_DATA_1;

BOOL MySetPrinter(LPTSTR pPrinterName, LPTSTR portname)
{
    HANDLE hPrinter = NULL;
    DWORD dwNeeded = 0;
    PRINTER_INFO_2 *pi2 = NULL;
    DEVMODE *pDevMode = NULL;
    PRINTER_DEFAULTS pd;
    BOOL bFlag;
    LONG lFlag;

    // Open printer handle (on Windows NT, you need full-access because you
    // will eventually use SetPrinter)...
    ZeroMemory(&pd, sizeof(pd));
    pd.DesiredAccess = PRINTER_ALL_ACCESS;
    bFlag = OpenPrinter(pPrinterName, &hPrinter, &pd);
    if (!bFlag || (hPrinter == NULL))
        return FALSE;

    // The first GetPrinter tells you how big the buffer should be in
    // order to hold all of PRINTER_INFO_2. Note that this should fail with
    // ERROR_INSUFFICIENT_BUFFER.  If GetPrinter fails for any other reason
    // or dwNeeded isn't set for some reason, then there is a problem...
    SetLastError(0);
    bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
    if ((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
        (dwNeeded == 0))
    {
        ClosePrinter(hPrinter);
        return FALSE;
    }

    // Allocate enough space for PRINTER_INFO_2...
    pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
    if (pi2 == NULL)
    {
        ClosePrinter(hPrinter);
        return FALSE;
    }

    // The second GetPrinter fills in all the current settings, so all you
    // need to do is modify what you're interested in...
    bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);
    if (!bFlag)
    {
        GlobalFree(pi2);
        ClosePrinter(hPrinter);
        return FALSE;
    }

    // If GetPrinter didn't fill in the DEVMODE, try to get it by calling
    // DocumentProperties...
    if (pi2->pDevMode == NULL)
    {
        dwNeeded = DocumentProperties(NULL, hPrinter,
            pPrinterName,
            NULL, NULL, 0);
        if (dwNeeded <= 0)
        {
            GlobalFree(pi2);
            ClosePrinter(hPrinter);
            return FALSE;
        }

        pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
        if (pDevMode == NULL)
        {
            GlobalFree(pi2);
            ClosePrinter(hPrinter);
            return FALSE;
        }

        lFlag = DocumentProperties(NULL, hPrinter,
            pPrinterName,
            pDevMode, NULL,
            DM_OUT_BUFFER);
        if (lFlag != IDOK || pDevMode == NULL)
        {
            GlobalFree(pDevMode);
            GlobalFree(pi2);
            ClosePrinter(hPrinter);
            return FALSE;
        }

        pi2->pDevMode = pDevMode;
    }

    // Driver is reporting that it doesn't support this change...
    if (!(pi2->pDevMode->dmFields & DM_ORIENTATION))
    {
        GlobalFree(pi2);
        ClosePrinter(hPrinter);
        if (pDevMode)
            GlobalFree(pDevMode);
        return FALSE;
    }

    // Specify exactly what we are attempting to change...
    pi2->pDevMode->dmFields = DM_ORIENTATION;
    //pi2->pDevMode->dmOrientation = dmOrientation;
    pi2->pPortName = portname;

    // Do not attempt to set security descriptor...
    pi2->pSecurityDescriptor = NULL;

    // Make sure the driver-dependent part of devmode is updated...
    lFlag = DocumentProperties(NULL, hPrinter,
        pPrinterName,
        pi2->pDevMode, pi2->pDevMode,
        DM_IN_BUFFER | DM_OUT_BUFFER);
    if (lFlag != IDOK)
    {
        GlobalFree(pi2);
        ClosePrinter(hPrinter);
        if (pDevMode)
            GlobalFree(pDevMode);
        return FALSE;
    }

    // Update printer information...
    bFlag = SetPrinter(hPrinter, 2, (LPBYTE)pi2, 0);
    if (!bFlag)
        // The driver doesn't support, or it is unable to make the change...
    {
        GlobalFree(pi2);
        ClosePrinter(hPrinter);
        if (pDevMode)
            GlobalFree(pDevMode);
        return FALSE;
    }

    // Tell other apps that there was a change...
    SendMessageTimeout(HWND_BROADCAST, WM_DEVMODECHANGE, 0L,
        (LPARAM)(LPCSTR)pPrinterName,
        SMTO_NORMAL, 1000, NULL);

    // Clean up...
    if (pi2)
        GlobalFree(pi2);
    if (hPrinter)
        ClosePrinter(hPrinter);
    if (pDevMode)
        GlobalFree(pDevMode);

    return TRUE;
}


int _tmain(int argc, _TCHAR* argv[])
{
   // MySetPrinter(L"Microsoft XPS Document Writer", L"sampleport");


    HANDLE hXVCPrinter = NULL ;
    PRINTER_DEFAULTS PrinterDefaults;
    PrinterDefaults.pDatatype = NULL;
    PrinterDefaults.pDevMode = NULL;
    PrinterDefaults.DesiredAccess = SERVER_ACCESS_ADMINISTER;
    /*
        / *if(OpenPrinter(L",XcvMonitor Standard TCP/IP Port", &hXVCPrinter,&PrinterDefaults))
                printf("open printer success./n");* /*/
       

        PRINTER_DEFAULTS printerDefaults = { NULL,  NULL, SERVER_ACCESS_ADMINISTER };
        DWORD dwStatus;
        DWORD cbInputData = 100;
        PBYTE pOutputData = NULL;
        DWORD cbOutputNeeded  = 0;
        PORT_DATA_1 portData;
        HANDLE hXcv = NULL;
        HANDLE hPrinter = NULL;
        BOOL bResult;
   
        bResult = OpenPrinter(L",XcvMonitor Standard TCP/IP Port", &hXcv, &printerDefaults);
        try
        {
            pOutputData = new BYTE[cbInputData];
        }
        catch (...)
        {
            pOutputData = NULL;
        }
        //
        // ##########      fix
        // 此处可以作为参数进行传递
        //
        WCHAR * wchPortname = L"sampleport";
        WCHAR * qName = L"queuename";
        WCHAR * hostname = L"192.168.1.1";

        ZeroMemory(&portData, sizeof(PORT_DATA_1));
        wcscpy(portData.sztPortName, wchPortname);

        portData.dwPortNumber   =       515;
        portData.dwVersion              =       1;

        portData.dwProtocol     = 2;
        portData.cbSize         = sizeof(PORT_DATA_1);
        portData.dwReserved     = 0L;
        portData.dwSNMPEnabled = 0;
        portData.dwSNMPDevIndex = 0;
        portData.dwDoubleSpool = 0;

        wcscpy(portData.sztQueue, qName);
        wcscpy(portData.sztIPAddress, hostname);
        wcscpy(portData.sztHostAddress, hostname);

        bResult = XcvData(hXcv, L"AddPort", (PBYTE) &portData, sizeof(PORT_DATA_1), pOutputData, cbInputData,  &cbOutputNeeded, &dwStatus);

        DWORD errorCode = GetLastError();
        printf("get error: %d/n", errorCode);
        cout << bResult << endl;
   
        if (hPrinter != NULL)
        {
            ClosePrinter(hPrinter);
        }

        if (hXcv != NULL)
        {
            ClosePrinter(hXcv);
        }

        if (pOutputData != NULL)
        {
            delete [] pOutputData;
        }
        HANDLE configPrinter = NULL;

//         if(OpenPrinter(L"Microsoft XPS Document Writer", &configPrinter, &printerDefaults))
//         {
//             printf("open printer to config success.");
//             PORT_INFO_3 portInfo;
//             portInfo.dwSeverity = PORT_STATUS_TYPE_INFO;
//             portInfo.dwStatus = 0;
//             portInfo.pszStatus = L"Hello world!";
        //ConfigurePort(L"Microsoft XPS Document Writer", configPrinter, "sampleport");
//             //L"Microsoft XPS Document Writer"
//         bool bSetPort = SetPort(
//                 NULL,      // printer server name
//                 L"sampleport",  // printer port name
//                 3,     // information level
//                 (LPBYTE)&portInfo   // information buffer
//                 );
//         DWORD dwSetPort = GetLastError();
//         cout << endl << dwSetPort << endl;
//         }
//         else
//         {
//             printf("config port error.");
//         }
        cout << endl;
//        cout << "config port result: " << bSetPort << endl;
        cout << "赶紧去看看你的打印机端口…… ^_^"  << endl;

        cin.get();
        cin.get();


    }

原创粉丝点击