Deleting a Standard TCP IP port using xcvdata not working

Viewed 499

Xcvdata() for deleting port.

BOOL DeletePortCus( TCHAR* PortName )
{
    HANDLE hPrinter;
    PRINTER_DEFAULTS PrinterDefaults;
    memset(&PrinterDefaults, 0, sizeof(PrinterDefaults));

    PrinterDefaults.pDatatype       = NULL;
    PrinterDefaults.pDevMode        = NULL;
    PrinterDefaults.DesiredAccess   = SERVER_ACCESS_ADMINISTER;

    DWORD needed = 0;
    DWORD rslt = 0;

    //Port data
    PORT_DATA_1 pOutputData ;
    DWORD error = 0;


    if (!OpenPrinter(L",XcvMonitor Standard TCP/IP Port", &hPrinter, &PrinterDefaults))
    {
        LPVOID lpMsgBuf; 
        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL, GetLastError(), NULL,(LPTSTR) &lpMsgBuf, 0, NULL ); 

        _tprintf( TEXT("Error in OpenPrinter. Error msg : %s"),lpMsgBuf);
        LocalFree( lpMsgBuf );

        return FALSE;
    }

    DWORD xcvresult= 0;

    if (
        !XcvData(
            hPrinter, 
            TEXT("DeletePort"),
            (PBYTE)PortName,
            (lstrlen(PortName) +1) * sizeof(TCHAR), //the 1 is for the trailing NULL
            ( byte * ) &pOutputData,
            sizeof(PORT_DATA_1), 
            &needed, 
            &xcvresult)
        )
    {
        LPVOID lpMsgBuf; 

        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL, GetLastError(), NULL,(LPTSTR) &lpMsgBuf, 0, NULL ); 

        _tprintf( TEXT("Error in XcvData. Error msg : %s; XcvDataPort status val = %d"), lpMsgBuf, xcvresult);
        LocalFree( lpMsgBuf );

        return FALSE;
    }

    ClosePrinter(hPrinter);
    return TRUE;
}

The highlight is both the functions (openprinter and xcvdata) succeed. But the port is not getting removed. I am completely at a loss here as I dont have any error to lookup.

Instead of ,XcvMonitor Standard TCP/IP Port I also tried with ,XcvPort <portname>. Still same. As Samer suggested below, I tried with OpenPrinter2 with no cache option.

PS: I know there's this simple alternative DeletePort(), but it invokes a UI dialog box if it fails, so I don't want to use it.

1 Answers
Related