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.
ASKER
Yes I am sure because i don't want a dialog for adding a port.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.DesiredAcc ess = 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)
#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.DesiredAcc
if (!OpenPrinter(",XcvMonitor
{
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)
Are you sure you don't want AddPort()?