Solved

XcvData - to add port

Posted on 2002-03-14
5
2,666 Views
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.
0
Comment
Question by:bansaldeep
5 Comments
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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()?
0
 

Author Comment

by:bansaldeep
Comment Utility
Yes I am sure because i don't want a dialog for adding a port.
0
 

Accepted Solution

by:
adebear71 earned 100 total points
Comment Utility
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 127.0.0.1) 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.



0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Sounds like a perfectly reasonable solution.
0
 
LVL 5

Expert Comment

by:pscsuk
Comment Utility
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)

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now