WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Printing Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Changing Windows 95/NT Default Printers

Keywords: default printer control panel 

Question:

I need to find a way to change the default printer. Anybody know of a way?

Answer:

Download the brand new Printer Manager extender from the website.

It does require at least WinBatch 98a or newer to work.

Sample code for changing the Default Printer in Windows 95/NT

In the code below, get the "newdevice=" information from the "DEVICE=" line in your WIN.INI file.

The trick is to manually set the printer to the desired configuration, then examine the appropriate registry entries and the device= line in the WIN.INI file. Construct your code to duplicate both the registry entries and the WIN.INI setting. Then do the DMC=27 stuff below.

The DMC stuff broadcasts a message to Windows and Windows applications that basically says: "Heads up. Printer settings have changed. Look at new settings and adjust."


;;32 bit Devmode.wbt
;;WM_DEVMODECHANGE (DMC) message:

DMC=27
                      
DaDll=Strcat(DirWindows(1),"USER32.DLL")
DllCall( DaDll, long:"SendMessageA", long:-1, long:DMC, long:0, lpstr:newdevice)
Message(0,1)

This example reads and changes the default printer in Windows 95/NT, and accomplishes this successfully *without* having to reboot your system.

hpdriver=RegQueryValue(@regmachine, "System\CurrentControlSet\control\Print\Environments\Windows 4.0\Drivers\HP LaserJet III[Configuration File]")
hpdriver=strupper(hpdriver)
hpdriver=strreplace(hpdriver,".DRV","")

serverloc=RegQueryValue(@regmachine, "System\CurrentControlSet\control\Print\Printers\HP LaserJet III[Port]")

message("Drivers and Server Location=", "Driver=%hpdriver%%@crlf%Server=%serverloc%")

newdevice="HP LaserJet III,%hpdriver%,%serverloc%"     ;Needs a device name here 

newprt = itemextract(1,newdevice,",")
regkey = RegOpenKey(@REGCLASSES+5, "System\CurrentControlSet\Control\Print\Printers")

;NB:  There is (Default) which is [] and then there is Default which is [Default].
;In the following code, you should be using [Default]: 

defprt = RegQueryValue(regkey, "[Default]")	 ;query the default value

RegSetValue(regkey, "[Default]", newprt)	 ;set the default value

defprt = RegQueryValue(regkey, "[Default]")	 ;query the default value

Message("New default printer", defprt)
RegCloseKey(regkey)
iniwrite("windows","device",newdevice)

;;32 bit Devmode.wbt
;;WM_DEVMODECHANGE (DMC) message:

DMC=27
		      
DaDll=Strcat(DirWindows(1),"USER32.DLL")
DllCall( DaDll, long:"SendMessageA", long:-1, long:DMC, long:0, lpstr:newdevice)
Message(0,1)

Once you've changed the device, you can broadcast a WM_WININICHANGE message to all interested parties in the system to make the printer changes in the registry take effect, so that applications (such as Program Manager, Task Manager, Control Panel, and so forth) can perform an update. An WININICHANGE message (=26) is required for some cases, so it probably would not hurt to do both of them (the WININICHANGE and the DEVMODECHANGE).

The WININICHG is for telling running applications that WIN.INI has changed. Newly run applications will pick up the environment automatically, done as follows:


;; this is the WM_WININICHANGE part

WININICHG=26
DaDll=Strcat(DirWindows(1),"USER32.DLL")
DllCall( DaDll, long:"SendMessageA", long:-1, long:WININICHG, long:0, lpstr:newdevice) 
In addition, if that does not work, examine all your registry entries BEFORE and AFTER the first print job. Windows will fix errors (sometimes) when you print. Compare registry and ini file settings - very carefully.

Example: for NT 3.51 (NT 4.0 is probably same as Windows95:

This would be done similarly, except that you need to figure out what the "newdevice" is from the registry.

Example of reading and changing the default printer in Windows NT 3.51:


hpdriver=RegQueryValue(@regmachine, "System\CurrentControlSet\control\Print\Environments\Windows 4.0\Drivers\HP LaserJet III[Configuration File]")
hpdriver=strupper(hpdriver)
hpdriver=strreplace(hpdriver,".DRV","")

serverloc=RegQueryValue(@regmachine, "System\CurrentControlSet\control\Print\Printers\HP LaserJet III[Port]")

message("Drivers and Server Location=", "Driver=%hpdriver%%@crlf%Server=%serverloc%")


newdevice="HP LaserJet III,%hpdriver%,%serverloc%" ;Needs a device name

newprt = "LJ3,winspool,LPT1:"  ; the printer you want to assign as the default

regkey = RegOpenKey(@REGCURRENT, "Software\Microsoft\Windows NT\CurrentVersion\Windows")
defprt = RegQueryValue(regkey, "[Device]")

Message("Default printer", defprt)

RegSetValue(regkey, "[Device]", newprt)
defprt = RegQueryValue(regkey, "[Device]")

Message("Default printer", defprt)

RegCloseKey(regkey)
iniwrite("windows","device",newdevice) 

;;32 bit Devmode.wbt
; you need to figure out what the "newdevice" is here by looking in the registry
; in the INI Mapping section, where the INI files are mapped to the registry

DMC=27
		       ;  device="HP DeskJet 500,HPDSKJET,LPT1:"
		  
DaDll=Strcat(DirWindows(1),"USER32.DLL")
DllCall( DaDll, long:"SendMessageA", long:-1, long:DMC, long:0, lpstr:newdevice)
Message(0,1)

;; this is the WM_WININICHANGE part

WININICHG=26
DaDll=Strcat(DirWindows(1),"USER32.DLL")
DllCall( DaDll, long:"SendMessageA", long:-1, long:WININICHG, long:0, lpstr:newdevice) 
 

Article ID:   W13685
Filename:   Changing Windows 95 and NT Default Printer.txt
File Created: 2002:02:12:15:59:50
Last Updated: 2002:02:12:15:59:50