HOWTO: Programmatically Share a Printer Under Windows NT

Last reviewed: November 13, 1997
Article ID: Q176704
The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK)

SUMMARY

In Windows NT, printers may be shared (or shares removed) using the Win32 SDK API SetPrinter(). The PRINTER_INFO_2 structure contains an Attributes member and a pShareName member that can be used for this purpose. Note that the printer must be opened with administrative permissions for the SetPrinter() call to be successful.

NOTE: On Windows 95 there is no way to programmatically share a printer.

MORE INFORMATION

The following code demonstrates how to share a printer programmatically on Windows NT:

Sample Code

   BOOL DoSharePrinterNT( LPTSTR szPrinterName, LPTSTR szShareName, BOOL
   bShare )
   {
      HANDLE            hPrinter;
      PRINTER_DEFAULTS   pd;
      DWORD            dwNeeded;
      PRINTER_INFO_2      *pi2;

      // Fill in the PRINTER_DEFAULTS struct to get full permissions.
      ZeroMemory( &pd, sizeof(PRINTER_DEFAULTS) );
      pd.DesiredAccess = PRINTER_ALL_ACCESS;
      if( ! OpenPrinter( szPrinterName, &hPrinter, &pd ) )
      {
         // OpenPrinter() has failed - bail out.
         return FALSE;
      }
      // See how big a PRINTER_INFO_2 structure is.
      if( ! GetPrinter( hPrinter, 2, NULL, 0, &dwNeeded ) )
      {
         if( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
         {
            // GetPrinter() has failed - bail out.
            ClosePrinter( hPrinter );
            return FALSE;
         }
      }
      // Allocate enough memory for a PRINTER_INFO_2 and populate it.
      pi2 = malloc( dwNeeded );
      if( pi2 == NULL )
      {
         // malloc() has failed - bail out.
         ClosePrinter( hPrinter );
         return FALSE;
      }
      if( ! GetPrinter( hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded ) )
      {
         // Second call to GetPrinter() has failed - bail out.
         free( pi2 );
         ClosePrinter( hPrinter );
         return FALSE;
      }
      // If you want to share the printer, set the bit and the name.
      if( bShare )
      {
         pi2->pShareName = szShareName;
         pi2->Attributes |= PRINTER_ATTRIBUTE_SHARED;
      }
      else // Otherwise, clear the bit.
      {
         pi2->Attributes = pi2->Attributes & (~PRINTER_ATTRIBUTE_SHARED);
      }
      // Make the change.
      if( ! SetPrinter( hPrinter, 2, (LPBYTE)pi2, 0 ) )
      {
         // SetPrinter() has failed - bail out
         free( pi2 );
         ClosePrinter( hPrinter );
         return FALSE;
      }
      // Clean up.
      free( pi2 );
      ClosePrinter( hPrinter );
      return TRUE;
   }


Additional query words: share printer network port control panel sharing
Keywords : GdiPrn
Version : WINNT:
Platform : winnt
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 13, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.