Adding Port, Driver, Printer in Windows

Just starting out? Need help? Post your questions and find answers here.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Adding Port, Driver, Printer in Windows

Post by coder14 »

I have been trying to programmatically setup a virtual printer with some API functions but have been very very unsuccessful. This is what I've got so far although the first part doesn't work. The last part for adding the printer works only if the driver is already (manually) installed:

Code: Select all

Define mon.MONITOR_INFO_2, drv.DRIVER_INFO_3, prn.PRINTER_INFO_2

With mon
  \pEnvironment = @""
  \pName = @"newMonitor"
  ;not sure what DLL should be used here
  \pDLLName = @"?.dll"
EndWith

;also tried with the full path - C:\Windows\System32\DriverStore\FileRepository
With drv
  \cVersion = 3
  \pDriverPath = @"\driverPath\PSCRIPT5.DLL"
  \pConfigFile = @"\driverPath\PS5UI.DLL"
  \pHelpFile = @"\driverPath\PSCRIPT.HLP"
  \pDataFile = @"\driverPath\MSXPSINC.PPD"
  \pDependentFiles = @"\driverPath\PSCRIPT.NTF"
  \pEnvironment = @"Windows NT x86"
  \pName = @"Miscrosoft PS Class Driver"
EndWith

With prn
  \pServerName = @""
  \pPrinterName = @"myPrinter"
  \pDriverName = @"Microsoft PS Class Driver"
  \pPortName = @"e:\printJobs\file.ps"
  \pPrintProcessor = @"WinPrint"
  \Priority = 1
  \DefaultPriority = 1
  \pDatatype = @"RAW"
  \pShareName = @"myPrinter"
  \pComment = @"myPrinter File Capture"
  \pLocation = @"local computer"
  \Attributes = #PRINTER_ATTRIBUTE_LOCAL | #PRINTER_ATTRIBUTE_SHARED
EndWith

;need these 3 functions to install the printer driver
AddMonitor_(#Null$, 2, @mon)
AddPort_(#Null, #Null, @"newMonitor")
AddPrinterDriver_(#Null, 3, @drv)

;this works if the printer driver is already installed
AddPrinter_(#Null, 2, @prn)
Is anyone familiar with the AddMonitor, AddPort and AddPrinterDriver parts? :?

Any help is appreciated. Thank you.
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: Adding Port, Driver, Printer in Windows

Post by JHPJHP »

Hi coder14,

See Services, Stuff, and Shellhook:
- Stuff\PrinterStuff\
-- PrinterStuff.pbi, AddMonitor.pb, DeleteMonitor.pb, AddPort.pb, DeletePort.pb, AddDriver.pb, UploadDriver.pb, InstallDriver.pb, DeleteDriver.pb
-- AddPrinter.pb, DeletePrinter.pb, SetSecurity.pb
-- MS_PS_CD_V3\... (required by AddDriver.pb)
-- MS_PS_CD_V4\... (required by UploadDriver.pb)

*** Tested in Windows 10 ***
- included driver files were taken from the Windows 10 OS

Instructions (order of execution is important):
- Setup
-- 1. AddMonitor.pb
-- 2. AddPort.pb
-- 3. AddDriver.pb: only needed if manually adding driver files
-- 3. UploadDriver.pb: only needed if driver is not in the Driver Repository
-- 3. InstallDriver.pb: only needed if driver is not already installed (not to be confused with an installed printer)
-- 4. AddPrinter.pb
-- 5. SetSecurity.pb: only needed if you want to restrict access, default security is ok for standard printing
- Cleanup
-- 1. DeletePrinter.pb
-- 2. DeleteDriver.pb: removes the installed driver (registry settings) not the actual files
-- 3. DeletePort.pb
-- 4. DeleteMonitor.pb

NOTE:
- Driver Repository: C:\WINDOWS\System32\DriverStore\FileRepository\...
- Printer Driver Directory: C:\Windows\System32\spool\drivers\x64\
-- identified using the Procedure GetDriverDirectory
- Important Registry Settings:
-- HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\...
-- HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\...

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Adding Port, Driver, Printer in Windows

Post by coder14 »

JHPJHP wrote:Hi coder14,

See Services, Stuff, and Shellhook:
- Stuff\PrinterStuff\
-- PrinterStuff.pbi, AddMonitor.pb, DeleteMonitor.pb, AddPort.pb, DeletePort.pb, AddDriver.pb, UploadDriver.pb, InstallDriver.pb, DeleteDriver.pb
-- AddPrinter.pb, DeletePrinter.pb, SetSecurity.pb
-- MS_PS_CD_V3\... (required by AddDriver.pb)
-- MS_PS_CD_V4\... (required by UploadDriver.pb)

*** Tested in Windows 10 ***
- included driver files were taken from the Windows 10 OS

Instructions (order of execution is important):
- Setup
-- 1. AddMonitor.pb
-- 2. AddPort.pb
-- 3. AddDriver.pb: only needed if manually adding driver files
-- 3. UploadDriver.pb: only needed if driver is not in the Driver Repository
-- 3. InstallDriver.pb: only needed if driver is not already installed (not to be confused with an installed printer)
-- 4. AddPrinter.pb
-- 5. SetSecurity.pb: only needed if you want to restrict access, default security is ok for standard printing
- Cleanup
-- 1. DeletePrinter.pb
-- 2. DeleteDriver.pb: removes the installed driver (registry settings) not the actual files
-- 3. DeletePort.pb
-- 4. DeleteMonitor.pb

NOTE:
- Driver Repository: C:\WINDOWS\System32\DriverStore\FileRepository\...
- Printer Driver Directory: C:\Windows\System32\spool\drivers\x64\
-- identified using the Procedure GetDriverDirectory
- Important Registry Settings:
-- HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\...
-- HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\...
Hi JHPJHP. I just tried them out and they work! Thank you very much for your help. :D :D :D
Post Reply