checking if program's opened

Hi,
In VB, how do I write the codings so that the program checks if it has been opened already? and stop further execution at that?
java_kevinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

supunrCommented:
How to Prevent Multiple Instances of a VB Application
http://support.microsoft.com/default.aspx?scid=KB;en-us;q142937
JonyvCommented:
Try this code, it will also activate the previously stared instance of your program before terminating.

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

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

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long


' Put this in Form_Load

Dim Title As String

' Check if an instance of this app is already running...
If App.PrevInstance Then
    '... if so, terminate this instance and activate the previous one
    Title = frmMain.Caption
    frmMain.Caption = "NOTITLE"       ' Change title of this window to avoid finding ourself witth 'FindWindow'
    hwnd = FindWindow(vbNullString, Title)      ' Find previous instance (by looking for a window with the same caption as this one)
    If hwnd <> 0 Then
        Call SetForegroundWindow(hwnd)         ' Bring previous instance to front
        Call ShowWindow(hwnd, SW_RESTORE)
        Call CloseHandle(hwnd)
    End If
    Unload Me        ' Kill this instance
    Exit Sub
End If

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

GivenRandyCommented:
To detect if a previous instance of the application is running:

---
If (App.PrevInstance) Then
    MsgBox "Another instance of this application is already running.", vbExclamation + vbOKOnly, "Error"
End If
---
GivenRandyCommented:
p.s., you could end it then:

To detect if a previous instance of the application is running (and End it):

---
If (App.PrevInstance) Then
    MsgBox "Another instance of this application is already running.", vbExclamation + vbOKOnly, "Error"
    End
End If
---
java_kevinAuthor Commented:
Jonyv  , how do i declare hwnd as?
supunrCommented:
hwnd is the window handle of the form.... dim newHWnd as long

newHWnd = form1.hwnd

if you read my second comment it should explain how to prevent the same instance of the program running.  Are you trying to prevent the anohter instance of your VB program or some other program?
java_kevinAuthor Commented:
supnr, sorry about it...

You've answered quite a number of my questions. As this is an easy one, I shall give it to jonyv.

I noted that FindWindow is a declared function. It  takes in a param of Long. But I ain't sure if hwnd should be of the same type. I do not why hwnd, being a window type could be Long.

Anyway, I do agree that supnr's second comment is much simplier.. I will have to test everything in win98SE, winXP/2k. if it works (provided I can install properly as supnr should understand), points will be awarded haha.. *do I sound odd??*
supunrCommented:
"I shall give it to jonyv", that is fine with me.  As long as I could help you out.   (Another option is you could split the points between us.  But I think it is not necessory.  I got plenty of points.  Give it to jonyv)
JonyvCommented:
A windowhandle (hWnd) is nothing more than a 32-bit number (= a Long), so there is no problem at all using Long as type for window handles.

Give your points to whoever provided the best solution (in your opinion) - I'm doing this mostly for fun (yeah, I'm crazy...)
supunrCommented:
"I'm doing this mostly for fun"

same here.  I like challenges and trying to find solutions for those.  this give me a chance to learn more new things every day.  Most of these questions, when I read for the first time, I don't have a clue.  But by reading and experimenting I learn a lot.  as Jonyv suggested give the points to the best solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.