How to create a new port and assign it to a printer

We have a virtual printer (provided by a third party) that is assigned an invalid local printer port. The printer is always local (we are not dealing with a remote print server or something like that). I would like to create a new local port (specifically for our application), and then configure the printer for this port instead of the random (and often incorrect) port that the print driver installer chooses. A.

I believe that I need to use XcvData and / or XcvDataPort to do this, but I'm losing a bit of how.

Does anyone have examples or pointers on how to proceed?

I would suggest that I need to do the following:

  • Make sure that the port name does not exist yet (maybe I can use EnumPorts , but I'm not sure which is the best approach, given that I should also create ports)
  • Create a port name if it exists
  • Change the printer configuration to use the new port.

and to delete:

  1. Remove port
+3
source share
2 answers

Wow, it looks like this has stalled everyone ... After much digging, here's how to do it:

DWORD CreatePort(LPWSTR portName)
{
      HANDLE hPrinter;
      PRINTER_DEFAULTS PrinterDefaults;
      memset(&PrinterDefaults, 0, sizeof(PrinterDefaults));

      PrinterDefaults.pDatatype = NULL;
      PrinterDefaults.pDevMode = NULL;
      PrinterDefaults.DesiredAccess = SERVER_ACCESS_ADMINISTER;

      DWORD needed;
      DWORD rslt;


      if (!OpenPrinter(",XcvMonitor Local Port", &hPrinter, &PrinterDefaults))
          return -1;

      DWORD xcvresult= 0;
      if (!XcvData(hPrinter, L"AddPort", (BYTE *)portName, (lstrlenW(portName) + 1)*2, NULL, 0, &needed, &xcvresult))
            rslt= GetLastError();

      if (!ClosePrinter(hPrinter))
          rslt= GetLastError();

      return rslt;
}

The port setting on this printer is relatively straightforward - OpenPrinter (), GetPrinter () with PRINTER_INFO_2, SetPrinter (), ClosePrinter ()

Cheerio.

+2
source

, . https://docs.microsoft.com/en-us/windows-hardware/drivers/print/tcpmon-xcv-commands ( ), :

PORT_DATA_1 pdPortData;  
wcscpy_s(pdPortData.sztPortName, MAX_PORTNAME_LEN, lpPortName);  
[...]
if (!XcvData(hXcv, L"AddPort", (BYTE*) &pdPortData, sizeof(PORT_DATA_1), NULL, 0, &dwNeeded, &dwStatus))
[...]

sztPortName PORT_DATA_1. , , .

0

Source: https://habr.com/ru/post/1715950/


All Articles