HOWTO: Retrieve Printer Name from Windows 95/98 Registry in VBID: Q143274
|
The Registry is used by Windows 95 and Windows 98 to determine what application programs and hardware items are installed in the computer system. This article explains how to retrieve the name of the default printer from the Registry from within a Visual Basic application program.
System
Current Control Set
Control
Print
Printers
Default
All of the above items are keys and subkeys. We are interested in the
Printers subkey.
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal dwReserved As Long, ByVal samDesired As Long, phkResult _
As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName$, ByVal _
lpdwReserved As Long, lpdwType As Long, lpData As Any, lpcbData As _
Long) As Long
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As _
Long) As Long
Const HKEY_CURRENT_CONFIG As Long = &H80000005
Private Sub Command1_Click()
Dim PName As String
PName = GetCurrPrinter()
Text1.Text = PName
End Sub
Function GetCurrPrinter() As String
GetCurrPrinter = RegGetString$(HKEY_CURRENT_CONFIG, _
"System\CurrentControlSet\Control\Print\Printers", "Default")
End Function
Function RegGetString$(hInKey As Long, ByVal subkey$, ByVal valname$)
Dim RetVal$, hSubKey As Long, dwType As Long, SZ As Long
Dim R As Long
RetVal$ = ""
Const KEY_ALL_ACCESS As Long = &HF0063
Const ERROR_SUCCESS As Long = 0
Const REG_SZ As Long = 1
R = RegOpenKeyEx(hInKey, subkey$, 0, KEY_ALL_ACCESS, hSubKey)
If R <> ERROR_SUCCESS Then GoTo Quit_Now
SZ = 256: v$ = String$(SZ, 0)
R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ)
If R = ERROR_SUCCESS And dwType = REG_SZ Then
RetVal$ = Left$(v$, SZ)
Else
RetVal$ = "--Not String--"
End If
If hInKey = 0 Then R = RegCloseKey(hSubKey)
Quit_Now:
RegGetString$ = RetVal$
End Function
Technical Articles, Windows Articles, User Interface Articles. How
to Use the Windows NT Registry in Your Application.
Product Documentation, SDKs, Win32 SDK, Win32, Reference, Functions.
RegOpenKeyEx QuickInfo Group Overview.
Product Documentation, SDKs, Win32 SDK, Win32, Reference, Functions.
RegQueryValueEx QuickInfo Group Overview.
Product Documentation, SDKs, Win32 SDK, Win32, Overviews, System
Services, Registry. Retrieving Data from the Registry.
Product Documentation, SDKs, Win32 SDK, Win32, Reference, Functions.
RegCloseKey QuickInfo Group Overview.
Additional query words:
kbVBp500 kbVBp400 kbAPI kbRegistry kbDSupport kbdsd Kbprint kbVBp600
KBPRINTING KBWIN32SDI
Keywords :
Version : WINDOWS:4.0,5.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 13, 1999