Link to home
Start Free TrialLog in
Avatar of bansaldeep
bansaldeep

asked on

XcvData - to add port

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.
Avatar of cookre
cookre
Flag of United States of America image

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()?
Avatar of bansaldeep
bansaldeep

ASKER

Yes I am sure because i don't want a dialog for adding a port.
ASKER CERTIFIED SOLUTION
Avatar of adebear71
adebear71

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sounds like a perfectly reasonable solution.
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)