Re: how to make a vb.net app single instance?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Rich (anonymous_at_discussions.microsoft.com)
Date: 03/10/04


Date: Wed, 10 Mar 2004 15:42:30 -0800

Thank you again for getting back to me (and for your
patience :). Unfortunately for me, my background with
processes ranges anywhere from weak to non-existent. My
problem is in getting the hwnd value required by

ShowWindow(ExecutingProcesses
(0).MainWindowHandle.ToInt32,SW_SHOWNORMAL)

and also same problem with SetForegroundWindow....

VB.net is complaining that 'ExecutingProcesses' isn't
declared. I placed the Dim and If Then statements in the
Form's Load Event. It works if I comment out
ShowWindow... and SetForegroundWindow... lines.

If I may bother you one more time (if not, don't worry -
I'll read up on thess APIs), but is there another way to
obtain the hwnd value than ExecutingProcesses? Can I get
it from the RunningProcesses var?

Thanks for your help,
Rich

>-----Original Message-----
>It's basically an array of all the processes with the
name of the current application. In the code I sent
previously, RunningProcesses was declared just before
the "If" statement as shown below.
>
>Dim RunningProcesses as Process() =
Process.GetProcessesByName(aProcName)
>
>
>Here's the code for the main routine again...
>
>
>Dim aModuleName As String =
>Diagnostics.Process.GetCurrentProcess.MainModule.ModuleNam
e
>Dim aProcName As String =
>System.IO.Path.GetFileNameWithoutExtension(aModuleName)
>Dim RunningProcesses as Process() =
Process.GetProcessesByName(aProcName)
>If RunningProcesses.Length > 1 Then
> ' Show previous instance
> ShowWindow(ExecutingProcesses
(0).MainWindowHandle.ToInt32,
>SW_SHOWNORMAL)
> SetForegroundWindow(ExecutingProcesses
(0).MainWindowHandle.ToInt32)
> ' Exit application
> Application.Exit()
>End If
>
>
>
>"Rich" <anonymous@discussions.microsoft.com> wrote in
message news:a89901c406f3$767f99b0$a501280a@phx.gbl...
>> Thanks for getting back to me on this. I am getting an
>> error though, on ShowWindow(ExecutingProcesses(0)...
says
>>
>> ExecutingProcesses not declared. Do I need to import
>> anything or inherit/implement something? I already
have
>>
>> Imports System.Runtime.InteropServices
>> ------------------------------------------
>> If RunningProcesses.Length > 1 Then
>> ' Show previous instance
>> ShowWindow(ExecutingProcesses
(0).MainWindowHandle.ToInt32,
>> SW_SHOWNORMAL)
>> SetForegroundWindow(ExecutingProcesses
>> (0).MainWindowHandle.ToInt32)
>> ' Exit application
>> Application.Exit()
>> End If
>> ------------------------------------------
>>
>>
>> >-----Original Message-----
>> >Add these declarations to your main form....
>> >
>> >
>> >' API Declarations
>> >Declare Function ShowWindow Lib "user32.dll" (ByVal
hwnd
>> As Int32, ByVal
>> >nCmdShow As Int32) As Int32
>> >Declare Function SetForegroundWindow Lib "user32.dll"
>> (ByVal hwnd As Int32)
>> >As Int32
>> >' Constant for ShowWindow function
>> >Const SW_SHOWNORMAL As Integer = 1
>> >
>> >
>> >And modify your code as shown below....
>> >
>> >
>> >Dim aModuleName As String =
>>
>Diagnostics.Process.GetCurrentProcess.MainModule.ModuleNam
>> e
>> >Dim aProcName As String =
>> >System.IO.Path.GetFileNameWithoutExtension(aModuleName)
>> >Dim RunningProcesses as Process() =
>> Process.GetProcessesByName(aProcName)
>> >If RunningProcesses.Length > 1 Then
>> > ' Show previous instance
>> > ShowWindow(ExecutingProcesses
>> (0).MainWindowHandle.ToInt32,
>> >SW_SHOWNORMAL)
>> > SetForegroundWindow(ExecutingProcesses
>> (0).MainWindowHandle.ToInt32)
>> > ' Exit application
>> > Application.Exit()
>> >End If
>> >
>> >
>> >
>> >"Rich" <anonymous@discussions.microsoft.com> wrote in
>> message
>> >news:a7f901c406e7$25865870$a501280a@phx.gbl...
>> >> Thank you all for your replies. Vielen Danke!
>> >>
>> >> Since I am still pretty new to VB.net I went with the
>> >> simplest solution, of those suggested here, which
works
>> >> except that it doesn't bring up/into focus the
already
>> >> running instance of the app like some of the more
>> >> sophisticated solutions mentioned (I was having
problems
>> >> with <...> the angle brackets - do I need to include
the
>> >> statements inside the brackets? Do I need to import
>> >> something else to use angle brackets?):
>> >> -----------------------------------------------------
-
>> >> Imports System.Diagnostics
>> >>
>> >> Dim aModuleName As String =
>> >>
>>
Diagnostics.Process.GetCurrentProcess.MainModule.ModuleName
>> >> Dim aProcName As String =
>> >> System.IO.Path.GetFileNameWithoutExtension
(aModuleName)
>> >> If Process.GetProcessesByName(aProcName).Length > 1
Then
>> >> Application.Exit()
>> >> End If
>> >> -----------------------------------------------------

--
>> >>
>> >> I tried adding the If Then code above to a
>> >>
>> >> Public Sub Main()
>> >>
>> >> (vb.net) and selected Sub Main as the startup module 
but
>> >> got an error message that there was no proper 
signature
>> >> for the sub Main.  So I put the If Then code into 
the 
>> main
>> >> form's Load event and made that my startup object.  
That
>> >> worked.  So is there a way, with this example code, 
to
>> >> make it so that the running app comes into focus if 
you
>> >> try to re-invoke? without making it too complex - 
maybe 
>> an
>> >> API or something?
>> >>
>> >> Thank you all again for your replies and help.
>> >>
>> >> Rich
>> >>
>> >> >-----Original Message-----
>> >> >yes, is there a way to prevent more than one 
instance 
>> of
>> >> a
>> >> >vb.net app from being invoked?  If this requires 
API 
>> code
>> >> >what API would I use?  If it doesn't require API 
code 
>> may
>> >> >I ask what property you set in the App or code to 
use?
>> >> >
>> >> >Thanks,
>> >> >Rich
>> >> >.
>> >> >
>> >
>> >
>> >.
>> >


Relevant Pages

  • Re: how to make a vb.net app single instance?
    ... "RunningProcesses" and then used it in the API calls under a different name. ... > obtain the hwnd value than ExecutingProcesses? ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Find path to outlook.exe
    ... an API to get it may be a slight bit more reliable. ... "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _ ... Public Function fGetOutlookEXE() As String ... Dim RetVal As Long ...
    (microsoft.public.access.modulesdaovba)
  • Re: Please help, IE window DOM (by focus) automation error
    ... the Internet Explorer_Server hWnd because when the Doc. ... Private Sub bUnhook_Click ... Dim LRet As Long ...
    (microsoft.public.vb.winapi)
  • Re: Find path to outlook.exe
    ... "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _ ... Public Function fGetOutlookEXE() As String ... Dim RetVal As Long ... this API apparently can take version specific arguments as ...
    (microsoft.public.access.modulesdaovba)
  • Re: Find path to outlook.exe
    ... Public Function fGetOutlookExe() As String ... Dim RetVal As Long ... Move the API module into the Form's module instead of its own. ...
    (microsoft.public.access.modulesdaovba)