CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Location
    Netherlands
    Posts
    57

    How to detect if any printer installed

    How can i detect if there is any printer installed?
    And can i put these printernames into a list?

    Thanks in advance,

    Mark


  2. #2
    Join Date
    May 1999
    Posts
    8

    Re: How to detect if any printer installed

    There are several questions that need answers first: Is this Windows 95/98 or NT? Do you want local or network printers (or both). Let's see...

    If you want to browse for network printers (under 95/98 or NT), you can use the SHBrowseForFolder() function. Here is some code that I use to browse for network printers:

    // Get the shell's allocator.
    LPMALLOC g_pMalloc;
    if ( !SUCCEEDED( SHGetMalloc( &g_pMalloc ) ) ) {
    MessageBox( _T("Failed to get IMalloc interface"), _T("Select Printer Error"), MB_ICONSTOP | MB_OK );
    return;
    }
    // Allocate some memory for our buffer
    LPTSTR location = (LPTSTR)g_pMalloc->Alloc( MAX_PATH );
    if ( location == NULL ) {
    MessageBox( _T("Failed to allocate memory for printer location"), _T("Select Printer Error"), MB_ICONSTOP | MB_OK );
    return;
    }

    // Get the PIDL for the network folder
    LPITEMIDLIST pidlNetwork; // PIDL for Network folder
    if ( !SUCCEEDED( SHGetSpecialFolderLocation( GetSafeHwnd(), CSIDL_NETWORK, &pidlNetwork ) ) ) {
    // Free allocated memory
    g_pMalloc->Free( location );
    MessageBox( _T("Failed to get the network folder"), _T("Select Printer Error"), MB_ICONSTOP | MB_OK );
    return;
    }

    // Browse for network printer
    BROWSEINFO bi;
    memset( &bi, 0, sizeof( BROWSEINFO ) );
    bi.hwndOwner = GetSafeHwnd();
    // bi.pidlRoot = NULL; // Desktop
    bi.pidlRoot = pidlNetwork; // Networks
    bi.pszDisplayName = location;
    bi.lpszTitle = _T("Select the network printer that you want to use.\nNote: Printers are usually attached to computers.");
    bi.ulFlags = BIF_BROWSEFORPRINTER;
    bi.lpfn = NULL;
    bi.lParam = NULL;
    bi.iImage = 0;

    LPITEMIDLIST pPidl = SHBrowseForFolder( &bi );
    if ( pPidl != NULL ) {
    if ( SHGetPathFromIDList( pPidl, location ) ) {
    // Set the printer name
    m_sPrinterName = location;
    // Send the data to the dialog
    UpdateData( FALSE );
    }
    // Free the system allocated pidl
    g_pMalloc->Free( pPidl );
    }

    // Free our network pidl
    g_pMalloc->Free( pidlNetwork );
    // Free our location holder
    g_pMalloc->Free( location );

    For local printers, you'll probably have to use the EnumPrinters() command with the PRINTER_ENUM_LOCAL flag. Sorry, no code snippets here. Hope this helps. Good luck!

    Kevin


  3. #3
    Join Date
    May 1999
    Posts
    25

    Re: How to detect if any printer installed

    Lookup the help on the EnumPrinters() function.
    There is a lot of data of various type that this function can give you.




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured