VB3 Changing WIN.INI Printer Settings By Using Windows APIID: Q105839
|
From Visual Basic, you can call Windows API routines to change the
default printer settings stored in the Windows WIN.INI file. You can
also broadcast a message to all applications currently loaded in
Windows to try to force them to use this WIN.INI change. However, most
Windows version 3.0 and 3.1 applications are not designed to act on this
broadcast message.
Applications that are started after you change WIN.INI will reflect
your WIN.INI changes, as will applications that are currently loaded, if
the user did not change Printer Setup in the application. But if
the user changed Printer Setup in a currently loaded application, the
application will ignore any changes to WIN.INI during that application's
session.
There are only two ways to ensure that an application will take the changes
made to the printer settings stored in WIN.INI. Either method will work:
The following steps change the printer settings in the WIN.INI file, and
then broadcast a message to all programs currently loaded in Windows to
make the change take effect:
[windows]
device=HP LaserJet IIISi PostScript,pscript,LPT1:
[devices]
Generic / Text Only=TTY,FILE:
HP LaserJet IIISi PostScript=pscript,LPT1:
For a detailed article that discusses how to change WIN.INI, search
for the following words in the Microsoft Knowledge Base:
CMDialog1.PrinterDefault = True
CMDialog1.Flags = PD_PRINTSETUP ' PD_PRINTSETUP = &H40&
CMDialog1.Action = 5 ' Displays Printer Dialog for Printer Setup
Option Explicit
' Enter each Declare statement on one, single line:
Declare Function GetProfileString Lib "Kernel"
(ByVal lpAppName As String, ByVal lpKeyName As Any,
ByVal lpDefault As String, ByVal lpReturnedString As String,
ByVal nSize As Integer) As Integer
Declare Function WriteProfileString Lib "Kernel"
(ByVal lpApplicationName As String, ByVal lpKeyName As Any,
ByVal lpString As Any) As Integer
Declare Function SendMessage Lib "User" (ByVal hWnd As Integer,
ByVal wMsg As Integer, ByVal wParam As Integer,
lParam As Any) As Long
Const WM_WININICHANGE = &H1A
Const HWND_BROADCAST = &HFFFF
' Enter the following two lines as one, single line:
Sub GetDriverAndPort (ByVal Buffer As String, DriverName As String,
PrinterPort As String)
Dim r As Integer
Dim iDriver As Integer
Dim iPort As Integer
DriverName = ""
PrinterPort = ""
'The driver name is first in the string terminated by a comma
iDriver = InStr(Buffer, ",")
If iDriver > 0 Then
'Strip out the driver name
DriverName = Left(Buffer, iDriver - 1)
'The port name is the second entry after the driver name
'separated by commas.
iPort = InStr(iDriver + 1, Buffer, ",")
If iPort > 0 Then
'Strip out the port name
PrinterPort = Mid(Buffer, iDriver + 1, iPort - iDriver - 1)
End If
End If
End Sub
Sub ParseList (lstCtl As Control, ByVal Buffer As String)
Dim i As Integer
Do
i = InStr(Buffer, Chr(0))
If i > 0 Then
lstCtl.AddItem Left(Buffer, i - 1)
Buffer = Mid(Buffer, i + 1)
Else
lstCtl.AddItem Buffer
Buffer = ""
End If
Loop While i > 0
End Sub
' Enter the following two lines as one, single line:
Sub SetDefaultPrinter (ByVal PrinterName As String,
ByVal DriverName As String, ByVal PrinterPort As String)
Dim DeviceLine As String
Dim r As Integer
Dim l As Long
DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort
' Store the new printer information in the [WINDOWS] section of
' the WIN.INI file for the DEVICE= item
r = WriteProfileString("windows", "Device", DeviceLine)
' Cause all applications to reload the INI file:
l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal "windows")
End Sub
Dim r As Integer
Dim Buffer As String
Dim DeviceName As String
Dim DriverName As String
Dim PrinterPort As String
Dim PrinterName As String
If List1.ListIndex > -1 Then
'Get the printer information for the currently selected printer
'in the list. The information is taken from the WIN.INI file.
Buffer = Space(1024)
PrinterName = List1.Text
r=GetProfileString("PrinterPorts",PrinterName,"",Buffer,Len(Buffer))
'Parse the driver name and port name out of the buffer
GetDriverAndPort Buffer, DriverName, PrinterPort
If DriverName <> "" And PrinterPort <> "" Then
SetDefaultPrinter List1.Text, DriverName, PrinterPort
End If
End If
Dim r As Integer
Dim Buffer As String
'Get the list of available printers from WIN.INI
Buffer = Space(8192)
r = GetProfileString("PrinterPorts",ByVal 0&,"",Buffer,Len(Buffer))
'Display the list of printer in the list box List1
ParseList List1, Buffer
"Microsoft Windows Programmer's Reference," Chapters 4 and 6, Microsoft Press, 1990.
Additional query words: 2.00 3.00
Keywords : kbcode kbPrinting
Version : 2.00 3.00
Platform : WINDOWS
Issue type :
Last Reviewed: June 14, 1999