XcvData - to add port

Posted on 2002-03-14
Last Modified: 2013-12-03
I want to add a port without a dialog using XcvData winapi function. The problem is it returns invalid handle for 1st parameter which requires a printer handle, but i do not have a printer installed.
Question by:bansaldeep
LVL 22

Expert Comment

ID: 6867347
That's a print spooler call back.  And, yes, the first parm is a handle to an extant, open printer.

Are you sure you don't want AddPort()?

Author Comment

ID: 6867410
Yes I am sure because i don't want a dialog for adding a port.

Accepted Solution

adebear71 earned 100 total points
ID: 8587171
There is currently nothing on the Internet (I have found) about doing this, I would have thought more people would have looked into this. I could only find this way to do it.
Hack the registry and restart the print spooler

 To add an extra LPT port add a new string named LPT3 (or next LPT port) to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports with
a Blank value. Restart the Print Spooler and viola.

For a TCP/IP port look add one to your local machine (point to and pick defaults and check the registry
entries in
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports\IP_127.0.0.1

hack these to your liking, restart print spooler and viola..

I had to do this for a project I needed to add a new port.

LVL 22

Expert Comment

ID: 8587257
Sounds like a perfectly reasonable solution.

Expert Comment

ID: 9247522
On the other hand, try this...

#include <windows.h>

#include <stdio.h>
int main(void)
      DWORD dwNeeded;
      DWORD dwStatus;
      WCHAR PortName[100];

      HANDLE hPrinter;
      PRINTER_DEFAULTS PrinterDefaults;

      PrinterDefaults.pDatatype = NULL;
      PrinterDefaults.pDevMode = NULL;
      PrinterDefaults.DesiredAccess = SERVER_ACCESS_ADMINISTER;
      if (!OpenPrinter(",XcvMonitor Local Port", &hPrinter, &PrinterDefaults))
            printf("OpenPrinter failed - %d\n", GetLastError());
            return -1;

      lstrcpyW(PortName, L"MyPort3");
      if (!XcvData(hPrinter, L"AddPort", (BYTE *)PortName, (lstrlenW(PortName) + 1) * 2, NULL, 0, &dwNeeded, &dwStatus))
            printf("XcvData failed - %d\n", GetLastError());
      if (dwStatus != 0)
            printf("XcvData - Returned %lu\n", dwStatus);

      if (!ClosePrinter(hPrinter))
            printf("ClosePrinter failed - %lu\n", GetLastError());

      return 0;

This will add a 'Local Port' port
See for how to add a TCP/IP port

(This is the 'proper' way to do it - according to Microsoft)


Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
In a recent question ( here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question