Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Changing Default Printer with

dbiddbid Posts: 1Member
I've seen a few code examples on how to change the default printer using VB6 but none with Does anyone have any code to do this or can you help be modify the VB6 code below?


Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const WM_WININICHANGE As Long = &H1A

Private Declare Function GetProfileString Lib "kernel32" _
Alias "GetProfileStringA" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long) As Long

Private Declare Function WriteProfileString Lib "kernel32" _
Alias "WriteProfileStringA" _
(ByVal lpszSection As String, _
ByVal lpszKeyName As String, _
ByVal lpszString As String) As Long

Private Declare Function SendNotifyMessage Lib "user32" _
Alias "SendNotifyMessageA" _
(ByVal hwnd As Long, _
ByVal msg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Private Sub Form_Load()

ProfileLoadWinIniList List1, "PrinterPorts"
Command1.Enabled = False

End Sub

Private Sub Command1_Click()

Call SetDefaultPrinterWinNT

End Sub

Private Sub List1_Click()

Command1.Enabled = List1.ListIndex > -1

End Sub

Private Sub SetDefPrinter(ByVal PrinterName As String, _
ByVal DriverName As String, _
ByVal PrinterPort As String)
Dim DeviceLine As String

'rebuild a valid device line string
DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort

'Store the new printer information in the
'[WINDOWS] section of the WIN.INI file for
'the DEVICE= item
Call WriteProfileString("windows", "Device", DeviceLine)

'Cause all applications to reload the INI file
Call SendNotifyMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal "windows")

End Sub

Private Sub GetDriverAndPort(ByVal Buffer As String, _
DriverName As String, _
PrinterPort As String)

Dim posDriver As Long
Dim posPort As Long
DriverName = ""
PrinterPort = ""

'The driver name is first in the string
'terminated by a comma
posDriver = InStr(Buffer, ",")

If posDriver > 0 Then

'Strip out the driver name
DriverName = Left(Buffer, posDriver - 1)

'The port name is the second entry after
'the driver name separated by commas.
posPort = InStr(posDriver + 1, Buffer, ",")

If posPort > 0 Then

'Strip out the port name
PrinterPort = Mid(Buffer, posDriver + 1, posPort - posDriver - 1)

End If
End If

End Sub

Private Function ProfileLoadWinIniList(lst As ListBox, _
lpSectionName As String) As Long

'Load the listbox data from win.ini.

Dim success As Long
Dim nSize As Long
Dim lpKeyName As String
Dim ret As String

'call the API passing null as the parameter
'for the lpKeyName parameter. This causes
'the API to return a list of all keys under
'that section. Pad the passed string large
'enough to hold the data. Adjust to suit.
ret = Space$(8102)
nSize = Len(ret)
success = GetProfileString(lpSectionName, _
vbNullString, _
"", _
ret, _

'The returned string is a null-separated
'list terminated by a pair of null characters.
If success Then

'trim terminating null and trailing spaces
ret = Left$(ret, success)

'with the resulting string,
'extract each element
Do Until ret = ""

'strip off an item and
'add the item to the listbox
lpKeyName = StripNulls(ret)
lst.AddItem lpKeyName


End If

'return the number of items as an
'indicator of success
ProfileLoadWinIniList = lst.ListCount

End Function

Private Function StripNulls(startstr As String) As String

'Take a string separated by chr$(0)
'and split off 1 item, shortening the
'string so next item is ready for removal.
Dim pos As Long

pos = InStr(startstr$, Chr$(0))

If pos Then

StripNulls = Mid$(startstr, 1, pos - 1)
startstr = Mid$(startstr, pos + 1, Len(startstr))

End If

End Function

Private Sub SetDefaultPrinterWinNT()

Dim Buffer As String
Dim DeviceName As String
Dim DriverName As String
Dim PrinterPort As String
Dim PrinterName As String
Dim r As Long

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.list(list1.ListIndex)

Call GetProfileString("PrinterPorts", _
PrinterName, "", _
Buffer, Len(Buffer))

'Parse the driver name and port name out of the buffer
GetDriverAndPort Buffer, DriverName, PrinterPort

If (Len(DriverName) > 0) And (Len(PrinterPort) > 0) Then
SetDefPrinter PrinterName, DriverName, PrinterPort
End If

End If

End Sub

Sign In or Register to comment.