Solved

Programatic printer configuration...

Posted on 1999-01-23
10
342 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now