2012-10-19 7 views
5

Voglio verificare se la stampante è in linea. Per questo ottengo l'handle della stampante con OpenPrinter. Quindi voglio utilizzare PRINTER_STATUS_OFFLINE in PRINTER_INFO_6 con GetPrinter(). Il risultato è sempre 0?controllare se la stampante di rete installata è online

Come si ottiene lo stato offline della stampante?

Codice che ho usato.

bool IsPrinterOnline(wstring strPrinterFriendlyName) 
{ 
    HANDLE hPrinter ; 
    if (OpenPrinter(const_cast<LPWSTR>(strPrinterFriendlyName.c_str()), &hPrinter, NULL) == 0) 
    {  
    /*OpenPrinter call failed*/ 
    return false; 
    } 

    DWORD dwBufsize = 0; 
    PRINTER_INFO_6* pinfo = 0; 
    GetPrinter(hPrinter, 6,(LPBYTE)pinfo, dwBufsize, &dwBufsize); //Get dwBufsize 

    PRINTER_INFO_6* pinfo6 = (PRINTER_INFO_6*)malloc(dwBufsize); //Allocate with dwBufsize 
    GetPrinter(hPrinter, 6,(LPBYTE)pinfo6, dwBufsize, &dwBufsize); 

    DWORD dwStatus = pinfo6->dwStatus; //always returns 0 

    if (dwStatus == PRINTER_STATUS_OFFLINE) 
    { 
    free(pinfo6); 
    ClosePrinter(hPrinter); 
    return false; 
    } 

    free(pinfo6); 
    ClosePrinter(hPrinter); 
    return true; 
} 
+0

Entrambi "GetPrinter" hanno effettivamente esito positivo? – Jay

+0

Il secondo GetPrinter è riuscito (restituito diverso da zero). Ma lo stato è sempre zero. –

risposta

6

L'ho risolto. ho usato "pinfo2-> Attributi & PRINTER_ATTRIBUTE_WORK_OFFLINE".

Ecco il codice.

bool IsPrinterOnline(wstring strPrinterFriendlyName) 
{ 
    HANDLE hPrinter ; 
    if (OpenPrinter(const_cast<LPWSTR>(strPrinterFriendlyName.c_str()), &hPrinter, NULL) == 0) 
    {  
    /*OpenPrinter call failed*/ 
    return false; 
    } 

    DWORD dwBufsize = 0; 
    PRINTER_INFO_2* pinfo = 0; 
    int nRet = 0; 
    nRet = GetPrinter(hPrinter, 2,(LPBYTE)pinfo, dwBufsize, &dwBufsize); //Get dwBufsize 
    DWORD dwGetPrinter = 0; 
    if (nRet == 0) 
    { 
    dwGetPrinter = GetLastError(); 
    } 

    PRINTER_INFO_2* pinfo2 = (PRINTER_INFO_2*)malloc(dwBufsize); //Allocate with dwBufsize 
    nRet = GetPrinter(hPrinter, 2,reinterpret_cast<LPBYTE>(pinfo2), dwBufsize, &dwBufsize); 
    if (nRet == 0) 
    { 
    dwGetPrinter = GetLastError(); 
    return false; 
    } 

    if (pinfo2->Attributes & PRINTER_ATTRIBUTE_WORK_OFFLINE) 
    { 
    free(pinfo2); 
    ClosePrinter(hPrinter); 
    return false; 
    } 

    free(pinfo2); 
    ClosePrinter(hPrinter); 
    return true; 
} 
+0

Non corretto, almeno per le stampanti di rete. –

+0

@CristianAmarie: puoi spiegare il tuo commento? – Thalia

+0

Una stampante di rete può essere segnalata come online mentre il dispositivo fisico è effettivamente offline. Non sto dicendo che è sempre vero, ma sta succedendo. È accaduto anche il contrario: la stampante di rete ha segnalato sul mio computer come non in linea (che era falso) e non ho potuto stampare nulla. Ho chiesto a un collega di stampare qualcosa e voilà: la stampante ha aggiornato il suo stato su online. Molto probabilmente c'è un disaccordo tra lo spooler e il list manager di rete/UPnP/qualsiasi meccanismo che Win32 sta usando. –