none
How to Add a TCPIP port programatically using winspool.drv? RRS feed

  • Question

  • I want to add a printer port using my c dll, just like the way the below mentioned vbs does. how to do it?

    'Adding Port
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set objNewPort = objWMIService.Get _
        ("Win32_TCPIPPrinterPort").SpawnInstance_

    Wscript.Echo " "
    Wscript.Echo PortName
    Wscript.Echo HostName
    Wscript.Echo PortNumber
    Wscript.Echo SNMPCommunity
    Wscript.Echo " "

    objNewPort.Name = PortName
    objNewPort.HostAddress = HostName
    objNewPort.Protocol = 1
    objNewPort.PortNumber = PortNumber
    objNewPort.SNMPEnabled = True
    objNewPort.SNMPCommunity = SNMPCommunity
    objNewPort.SNMPDevIndex = 1
    objNewPort.Put_

    Tuesday, August 5, 2014 12:37 PM

Answers

  • Quote http://msdn.microsoft.com/en-us/library/windows/desktop/dd183342%28v=vs.85%29.aspx:

    To add a port without displaying a dialog box, call the XcvData function instead of AddPort. For more information about XcvData, see the Microsoft Windows Driver Development Kit (DDK).

    And then check out http://msdn.microsoft.com/en-us/library/windows/hardware/ff559892%28v=vs.85%29.aspx: The XcvData function uses a PORT_DATA_1 structure when it adds a port or configures an existing port.

    Wednesday, August 6, 2014 5:54 PM

All replies

  • Quote http://msdn.microsoft.com/en-us/library/windows/desktop/dd183342%28v=vs.85%29.aspx:

    To add a port without displaying a dialog box, call the XcvData function instead of AddPort. For more information about XcvData, see the Microsoft Windows Driver Development Kit (DDK).

    And then check out http://msdn.microsoft.com/en-us/library/windows/hardware/ff559892%28v=vs.85%29.aspx: The XcvData function uses a PORT_DATA_1 structure when it adds a port or configures an existing port.

    Wednesday, August 6, 2014 5:54 PM
  • Hi Wei Mao,

    Thanks for you answer. I tried the same but the port is not getting created. it throws error 87. what i am doing wrong?. please help

    {
    HANDLE hXcv = INVALID_HANDLE_VALUE;
    PRINTER_DEFAULTS defaults;

    defaults.pDatatype = NULL;
    defaults.pDevMode = NULL;
    defaults.DesiredAccess = SERVER_ACCESS_ADMINISTER ;
    long error;
    // Handle to a local machine
    if (OpenPrinter(L",XcvMonitor Standard TCP/IP Port", &hXcv, &defaults ))
    {
     // hXvc contains an Xcv data handle to a local TCPMON port
    }
    error=GetLastError();
    PORT_DATA_1 pInputData;
    memset(&pInputData,0,sizeof(PORT_DATA_1));
    char* sztPortName = "HST_bababa.xyz.com";
    wchar_t prtname[32];
    mbstowcs(prtname, sztPortName, strlen(sztPortName)+1);
    LPWSTR portname = prtname;
    pInputData.sztPortName=portname;
    pInputData.dwVersion=1;
    pInputData.dwProtocol=PROTOCOL_RAWTCP_TYPE;
    pInputData.cbSize=sizeof(PORT_DATA_1);


    char* sztHostAddress = "bababa.xyz.com";
    wchar_t hstname[32];
    mbstowcs(hstname, sztHostAddress, strlen(sztHostAddress)+1);
    LPWSTR hosttname = hstname;
    char* sztSNMPCommunity = "public";
    wchar_t snmpcmtyname[32];
    mbstowcs(snmpcmtyname, sztSNMPCommunity, strlen(sztSNMPCommunity)+1);
    LPWSTR snmpcyname = snmpcmtyname;
    pInputData.sztHostAddress=hosttname;
    pInputData.sztSNMPCommunity=snmpcyname;
    pInputData.dwPortNumber=9100;
    pInputData.dwSNMPEnabled=TRUE;
    pInputData.dwSNMPDevIndex=1;
    DWORD pdwStatus=0;
    error=XcvData(hXcv,L"AddPort", (LPBYTE)&pInputData,  sizeof(pInputData), NULL, 0, NULL, &pdwStatus);
    error=GetLastError();
    if(hXcv)
    ClosePrinter(hXcv);
    }


     

    Thursday, August 7, 2014 9:47 AM
  • When calling XcvData, the pcbOutputNeeded parameter cannot be NULL.
    Tuesday, August 12, 2014 5:15 PM