Learn how to a build a cloud-first strategyRegister Now


XcvData - to add port

Posted on 2002-03-14
Medium Priority
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 300 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 http://www.osr.com/ddk/graphics/provider_7a5j.htm for how to add a TCP/IP port

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


Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

810 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