Solved

Programatic printer configuration...

Posted on 1999-01-23
10
344 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

806 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