CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Running only 1 instance

    Good Morning !

    I need to run only 1 instance of my application.

    I'm betting I need to use app.PrevInstance. But it seems that I can't make it work... I guess I need to know which application is running..

    Any help would be appreciated thanks

    Nicolas

    Nicolas Bohemier

  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Running only 1 instance

    You can use App.PrevInstance. But from my experience sometimes it is not very stable. For example when you kill the second instance of the app and check the task list you might still have your app running there.
    I prefer to use API. Here is an example

    'module declarations
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long


    'Constants used by the API functions
    Public Const WM_CLOSE = &H10

    'in the form

    Private Sub Form_Load()
    '========================================================================
    If App.PrevInstance Then KillSecondApp
    End Sub

    Private Sub KillSecondApp()
    '=============================================================================
    Dim hWindow As Long
    Dim lngReturnValue As Long


    hWindow = FindWindow(vbNullString, "Your Window Caption")
    lngReturnValue = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
    End Sub

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Running only 1 instance

    Here is a simple prigram that enters into a TextBox all available runnning programs.
    Start a new project. Add a textbox to the form.
    Add a module
    paste this code into the appropriate spot.

    ' In the form
    '
    option Explicit

    'Add this code to a form
    private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    'set the form's graphics mode to persistent
    me.AutoRedraw = true
    'call the Enumwindows-function
    Dim x
    EnumWindows AddressOf EnumWindowsProc, byval 0&
    End Sub
    '
    '
    ' In the module
    '
    option Explicit
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]

    'Add this code to a module
    Declare Function EnumWindows Lib "user32" (byval lpEnumFunc as Long, byval lParam as Long) as Boolean
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (byval hwnd as Long, byval lpString as string, byval cch as Long) as Long
    Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (byval hwnd as Long) as Long
    public Function EnumWindowsProc(byval hwnd as Long, byval lParam as Long) as string
    Dim sSave as string, Ret as Long
    Ret = GetWindowTextLength(hwnd)
    sSave = Space(Ret)
    GetWindowText hwnd, sSave, Ret + 1
    If Trim(sSave) <> "" then _
    frmIsMeRunning.Text1 = frmIsMeRunning.Text1 & sSave & vbCrLf
    'continue enumeration
    EnumWindowsProc = true
    End Function




    '
    Run the program. Use its output to compare against your app name.

    John G

  4. #4
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Running only 1 instance

    If I do this it is working but I'm getting a run-time error so It might be this.. I'm not sure

    I'm using this

    if App.PrevInstance then
    end
    end if

    But I'm getting the run-time error only on Winnt with 1 instance started

    It says that

    The instruction at 0x70223EFF referenced memory at 0x70223EFF. The memory could not be "Read".

    And the worse is that my application is working great till you try to close it, it says that once in a while.. Need to find it GRR.

    Sigh

    Nicolas


    Nicolas Bohemier

  5. #5
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: Running only 1 instance

    For the App.PrevInstance property to be true the same application should be invoked from either the application Full Path or shortcuts. Invoking a copy of the application will not fire the App PrevInstance. The code below should do what you asked for.
    private Sub Form_Load()
    If App.PrevInstance then
    'Delete the following line after testing
    MsgBox "Found Instance"
    Unload me
    End
    End If
    End Sub





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured