Solved

Programatic printer configuration...

Posted on 1999-01-23
10
345 Views
Last Modified: 2013-12-16
Hi All, I'm looking for a programatic(ie done in
login script/batch file) way to setup and delete printers
under windows 95.  Basically, I want to take a win95 setup
with the correct drivers already on the HD and tell win95
which printers are there and which one is the default.  Any
help would be greatly appreciated.
0
Comment
Question by:dragonlord
  • 6
  • 4
10 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 1761749
#include <winspool.h>

class PrinterMgr  
{
public:
      PrinterMgr();
      virtual ~PrinterMgr();
      DWORD GetPrinterCount();
      PRINTER_INFO_2 *GetPrinterInfo(DWORD i);
      CString &GetDefaultPrinter();
      BOOL SetDefaultPrinter(CString &cs);
      LPTSTR EnumeratePrinters(int i);

private:
      PRINTER_INFO_2 *pPrinterInfo2;
      DWORD dwPrinterCount;
      BOOL isNT;
      CString csDefaultPrinter;
};




// PrinterMgr.cpp: implementation of the PrinterMgr class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <winspool.h>
#include "PrinterMgr.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

PrinterMgr::PrinterMgr()
{
      CString msg;
      DWORD dwBytesNeeded;

      DWORD dwBytesCopied;

      // NT or 95
      OSVERSIONINFO osvi;
      osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

      ::GetVersionEx(&osvi);

      if(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS){
            isNT = false;
      }
      else if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT){
            isNT = true;
      }
      else{
            msg.Format("Error: Unknown platform : %d", osvi.dwPlatformId);
            AfxMessageBox(msg, MB_OK);
            return;
      }

      // Enumerate all the printers configured by the system
      PRINTER_INFO_2 tmp;

      ::EnumPrinters(
            PRINTER_ENUM_LOCAL | (isNT ? PRINTER_ENUM_CONNECTIONS : PRINTER_ENUM_LOCAL),
            NULL,
            2,
            (LPBYTE)&tmp,
            1,
            &dwBytesNeeded,
            &dwPrinterCount
      );

      pPrinterInfo2 = new PRINTER_INFO_2[(dwBytesNeeded/sizeof(PRINTER_INFO_2))+1];

      ::EnumPrinters(
            PRINTER_ENUM_LOCAL | (isNT ? PRINTER_ENUM_CONNECTIONS : PRINTER_ENUM_LOCAL),
            NULL,
            2,
            (LPBYTE)pPrinterInfo2,
            dwBytesNeeded,
            &dwBytesCopied,
            &dwPrinterCount
      );

}

LPTSTR PrinterMgr::EnumeratePrinters(int i)
{
      if(i > (int)(dwPrinterCount-1)){
            return NULL;
      }
      return (pPrinterInfo2+i)->pPrinterName;
}

DWORD PrinterMgr::GetPrinterCount()
{
      return dwPrinterCount;
}

BOOL PrinterMgr::SetDefaultPrinter(CString &printer)
{
      
      HANDLE pHandle;
      BYTE *buffer;
      DWORD numbytes;
      CString msg;


      if(!isNT){  // Windows 95 can use XPrinter functions
            if(!OpenPrinter((char *)(LPCTSTR)printer, &pHandle, NULL)){
                  msg.Format("OpenPrinter FAILED!! (%d)", GetLastError());
                  AfxMessageBox(msg, MB_OK);
                  return false;
            }

            buffer = new BYTE[1];
            GetPrinter(pHandle, 2, buffer, 1, &numbytes);
            delete [] buffer;

            buffer = new BYTE[numbytes];
            if(!GetPrinter(pHandle, 2, buffer, numbytes, &numbytes)){
                  msg.Format("GetPrinter FAILED!! (%d)", GetLastError());
                  AfxMessageBox(msg, MB_OK);
                  msg.Format("%d : %d", sizeof(PRINTER_INFO_2), numbytes);
                  AfxMessageBox(msg, MB_OK);
                  delete [] buffer;
                  return false;
            }

            ((PRINTER_INFO_2 *)buffer)->Attributes |= PRINTER_ATTRIBUTE_DEFAULT;
            ((PRINTER_INFO_2 *)buffer)->pSecurityDescriptor = NULL;

            if(!SetPrinter(pHandle, 2, buffer, 0)){
                  msg.Format("SetPrinter FAILED!! (%d)", GetLastError());
                  AfxMessageBox(msg, MB_OK);
                  delete [] buffer;
                  return false;
            }

            ClosePrinter(pHandle);

            delete [] buffer;
      }
      else{ // Windows NT uses Get/WriteProfileString
            CString temp, driver, port;
            TCHAR mybuff[1024];
            DWORD nSize = 1024;
            
            ::GetProfileString("PrinterPorts", (LPCTSTR)printer, "", mybuff, nSize);
            temp = mybuff;
            driver = temp.Left(temp.Find(','));
            temp = temp.Right(temp.GetLength() - temp.Find(',') -1);
            //AfxMessageBox(driver + " " + temp, MB_OK);

            port = temp.Left(temp.Find(','));

            temp = printer + "," + driver + "," + port;

            ::WriteProfileString("windows", "device", (LPCTSTR)temp);
            //AfxMessageBox(temp, MB_OK);
      
      }

      return true;
}

CString & PrinterMgr::GetDefaultPrinter()
{
      TCHAR lpDefault[1024];

      ::GetProfileString("windows", "device", "", lpDefault, 1024);
      if(strlen(lpDefault) > 0){
            csDefaultPrinter = lpDefault;
            csDefaultPrinter = csDefaultPrinter.Left(csDefaultPrinter.Find(","));
            return csDefaultPrinter;
      }
      return CString("");
}

PRINTER_INFO_2 * PrinterMgr::GetPrinterInfo(DWORD i)
{
      if(i >= dwPrinterCount){
            return NULL;
      }

      return pPrinterInfo2 + i;
}

PrinterMgr::~PrinterMgr()
{
      delete [] pPrinterInfo2;
}

0
 
LVL 1

Author Comment

by:dragonlord
ID: 1761750
this is great, if I were looking to actually write a program.
However, not being a diehard windows programmer, I'm more looking
for a program that already exists that I can use via a dos
box.  Something where I can say;
program_name set-default "DotMatrix_Green"   or
program_name delete-printer "DotMatrix_Blue" or
program_name add_printer printer_name printer_type
Thanks alot.  The previous answer may still prove useful.  TTFN!
0
 
LVL 32

Accepted Solution

by:
jhance earned 200 total points
ID: 1761751
This code will give you the names of all printers installed and setup one of them as default.  It cannot install a new one.

If you want, I'll compile this into a command line application and post the URL here.

Will that work for you?
0
Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

 
LVL 1

Author Comment

by:dragonlord
ID: 1761752
Yes, that would be very helpful.  It would be part of the way
down the road that I need to go.  THANKS!
0
 
LVL 1

Author Comment

by:dragonlord
ID: 1761753
Also, if it can't install one, how hard is it to delete one?
0
 
LVL 32

Expert Comment

by:jhance
ID: 1761754
Adding or deleting printers can be done with the API as well.

I'll cook up what I've given you so far and you can try it out.

Regards....
0
 
LVL 32

Expert Comment

by:jhance
ID: 1761755
Can you provide an EMAIL address where I can send you the EXE file?  I don't have a convenient place to put this up for download at the moment.

Thanks.....
0
 
LVL 1

Author Comment

by:dragonlord
ID: 1761756
bubba@msu.edu  Thanks!
0
 
LVL 1

Author Comment

by:dragonlord
ID: 1761757
bubba@msu.edu  Thanks!
0
 
LVL 1

Author Comment

by:dragonlord
ID: 1761758
This little program does exactly what I need it to do.  thank
you ever so much.
0

Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
virus question? 6 81
Location of files for Quick Access Toolbar (QAT) settings in Office 2010 3 52
FTP server windows 2008 5 40
Window 10 upgrade crashes ssd again 44 86
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question