Implement password-protected TextBox that are really secure

As explained in another tip in this TipBank, users can peek at the contents of password-protected TextBox controls with a simple Spy-like program, or even with a VB program plus some API functions. The problem is that such TextBox controls react to the WM_GETTEXT message and the GetWindowText API function as if they were regular TextBox controls. This holds True under Windows 95, 98 and NT. (Windows 2000 has fixed this security issue.)

If you want to make sure that no one can steal passwords from your VB programs, you only have to subclass the WM_GETTEXT message and discard the call. The following code snippet relies on the MsgHook.Dll (that you can download from the FileBank section of this site).

' ASSUMES THAT YOU HAVE ADDED A REFERENCE TO THE MSGHOOK.DLL'Dim WithEvents TextHook As MsgHookPrivate Sub Form_Load()    Set TextHook = New MsgHook    ' Text1 is the password-protected control    TextHook.StartSubclass Text1End SubPrivate Sub TextHook_BeforeMessage(uMsg As Long, wParam As Long, lParam As Long, _    retValue As Long, Cancel As Boolean)    ' filter out the WM_GETTEXT message        If uMsg = WM_GETTEXT Then Cancel = TrueEnd Sub

If you discard the WM_GETTEXT message, no application will be able to read the contents of your control.

It is remarkable that this subclassing code doesn’t prevent the current application from reading the contents of the password-protected control. The reason is – evidently – that VB doesn’t rely on API calls to read the Text property of a TextBox.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS