by Thomas Greenwood (2 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 5.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (3 Votes)
This code will activate a previous instance of your program if detected. It uses subclassing of the main window.
Inputs
N/A
Assumes
May crash the ide, so save your work before execution (did not crash mine however, but subclassing can be unstable)
Code Returns
N/A
Side Effects
Had to use end to close the next instance (not the prev instance). Using Unload Obj in Form_Load produced an error, it can be used if On Error Resume Next or a Error Handler is produced.
API DeclarationsPrivate Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
' ^ Used to change window proc
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' ^ Used to call default window proc for window
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
' ^ Send Message to window (Proc)
Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
' ^ Used, funnily enough...to flash the window :D
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
' ^ Used to find the window if we have a prev instance
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
' ^ Check to see if we have a valid window.