Too Cool for Internet Explorer

VB: InputBox-Hack für Passwort-Abfragen

Heute gibt es mal eine kleine Ergänzung zu vb-voting.com ;-) .

Es taucht immer wieder die Frage auf, ob man in Visual Basic die bekannte InputBox auch für Passwort-Abfragen nutzen kann. D.h. die Zeichendarstellung sollte durch ein ‘*’, oder was auch immer, substituiert werden können.

Generell geht das nicht :-( . Aber dank folgendem VB-Hack ist es dennoch möglich ;-) :

'Code fuer die Integration ins Modul beispiel.bas
'
Option Explicit

Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32" _
Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Public Declare Function SetTimer& Lib "user32" _
(ByVal hwnd&, ByVal nIDEvent&, ByVal uElapse&, _
ByVal lpTimerFunc&)

Private Declare Function KillTimer& Lib "user32" _
(ByVal hwnd&, ByVal nIDEvent&)

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long

Const EM_SETPASSWORDCHAR = &HCC
Public Const NV_INPUTBOX As Long = &H5000&

Public Sub TimerProc(ByVal hwnd&, ByVal uMsg&, _
ByVal idEvent&, ByVal dwTime&)

Dim EditHwnd As Long

EditHwnd = FindWindowEx(_
FindWindow("#32770", App.Title), 0, "Edit", "")

Call SendMessage(EditHwnd, _
EM_SETPASSWORDCHAR, Asc("*"), 0)
KillTimer hwnd, idEvent
End Sub

'Der Code fuer die Einbindung
'lautet dann so:
'
Private Sub Command1_Click()
Dim ret As String
SetTimer hwnd, NV_INPUTBOX, 10, _
AddressOf TimerProc
ret = InputBox("Passwort eingeben:")
End Sub

Leave a Reply