Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Programatic printer configuration...

Posted on 1999-01-23
10
Medium Priority
?
351 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

661 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