开发者

How can i get the printer's network path (\\COMPUTER\PRINTERNAME)

开发者 https://www.devze.com 2022-12-20 12:44 出处:网络
How can i get the printer\'s network path (\\COMPUTER\\PRINTERNAME) from default prin开发者_StackOverflowter window c++.This bit of code found at this forum post might do the trick:

How can i get the printer's network path (\COMPUTER\PRINTERNAME) from default prin开发者_StackOverflowter window c++.


This bit of code found at this forum post might do the trick:

You will most certainly need the Windows SDK, of course. You may also need to link with the winspool.lib library as mentioned in the thread, though it looks like this code retrieves the name from the registry so my guess is that you do not. If you use this code please comment on any quirks you fix or if you needed to link with winspool.lib or not.

Furthermore you might want to check this other related Stack Overflow post. If I were you, I would use the GetDefaultPrinter function first, and then default to the code below if it returns a printer name length of 0.

#ifdef UNICODE
  #define GETDEFAULTPRINTER "GetDefaultPrinterW"
#else
  #define GETDEFAULTPRINTER "GetDefaultPrinterA"
#endif

CString GetDefaultPrinterName()
{
    CString strPrinterName = TEXT("") ;
    OSVERSIONINFO osv;

    // --- Get the operationg system versin info ---
    osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx( &osv ) ;

    BOOL bRet = FALSE ;
    // If Windows 95 or 98, use EnumPrinters.
    if (osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
    {
        // The first EnumPrinters() tells you how big our buffer must
        // be to hold ALL of PRINTER_INFO_2. Note that this will
        // typically return FALSE. This only means that the buffer (the 4th
        // parameter) was not filled in. You do not want it filled in here.
        DWORD dwNeeded = 0 , dwReturned = 0 ;
        SetLastError(0);
        bRet = EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 2, NULL, 0, &dwNeeded, &dwReturned);
        if ((GetLastError() != ERROR_INSUFFICIENT_BUFFER) || (dwNeeded == 0))
            return CString("") ;

        PRINTER_INFO_2 * ppi2 = reinterpret_cast<PRINTER_INFO_2 *>(new char[dwNeeded] ) ;
        if( !ppi2 )
            return CString("") ;

        // The second EnumPrinters() will fill in all the current information.
        bRet = EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded, &dwReturned);
        if( !bRet )
        {
            char * pTemp = reinterpret_cast<char *>( ppi2 ) ;
            delete []pTemp ;

            return CString("");
        }

        // --- Get the printer name ---
        strPrinterName = ppi2->pPrinterName ;

        // --- Free memory ---
        char * pTemp = reinterpret_cast<char *>( ppi2 ) ;
        delete []pTemp ;
    }

    // If Windows NT, use the GetDefaultPrinter API for Windows 2000,
    // or GetProfileString for version 4.0 and earlier.
    else if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
        // Windows 2000 or later (use explicit call)
        // --- Call GetDefaultPrinter() to get the printer name ---
        if (osv.dwMajorVersion >= 5) 
        {
            // --- Load library winspool.drv ---
            HMODULE hSpoolDrv = LoadLibrary("winspool.drv") ;
            if( !hSpoolDrv )
                return CString("");

            // --- function type definition ---
            typedef BOOL (FAR PASCAL *FNGETPRINTER)(LPTSTR ,LPDWORD );
            FNGETPRINTER fnGetPrinter = (FNGETPRINTER)GetProcAddress( hSpoolDrv, GETDEFAULTPRINTER ) ;
            if( fnGetPrinter )
            {
                LPTSTR szPrinterName[MAX_PATH] ;
                DWORD nLen = MAX_PATH ;
                bRet = fnGetPrinter((LPTSTR)szPrinterName,&nLen);
                // --- Function call succeeds, then set the printer name ---
                if( bRet )
                    strPrinterName = (char *)szPrinterName ;
            }
            FreeLibrary( hSpoolDrv ) ;
        }
        else// --- NT4.0 or earlier ---
        {
            // Retrieve the default string from Win.ini (the registry).
            // String will be in form "printername,drivername,portname".
            TCHAR szBuffer[MAX_PATH] ;
            // --- HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Version ---
            if (GetProfileString("windows", "device", ",,,", szBuffer, MAX_PATH ) <= 0)
                return CString("");

            // Printer name precedes first "," character.
            strtok(szBuffer, ",");

            if( lstrlen(szBuffer) <= 0 )
                return CString("") ;

            // --- Set the printer name ---
            strPrinterName = szBuffer ;
        }
    }

    return strPrinterName ;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消