Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Programatic printer configuration...

Posted on 1999-01-23
10
Medium Priority
?
354 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
It is a real story and is one of my scariest tech experiences. Most users think that IT experts like us know how to fix all computer problems. However, if there is a time constraint and you MUST not fail the task or you will lose your job, a simple …
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

886 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