Preventing multiple copies of software with VB.NET
I have an application that is being used on PPC based hand held scanners but the software they use to lock the scanners allows them to move focus from the scanner software to the lockdown program. Because the lockdown software doesn't allow access to menus they return to the scanner software by running the application again.
Because the original copy has control of the scanner hardware the second copy now doesn't operate correctly.
I believe that I should be able to use the FindWindow routine in the initial startup code to check whether the software is already running and if it is I should be able to use the SetForegroundWindow routine to surface the original copy then exit the second copy.
Is this a senisble way of handling this sequence of events? If so how do I do this (code would be good).
Steve Sharkey
Re: Preventing multiple copies of software with VB.NET
Ya it is the right way to do it. When you try to launch the second instance of the applicaiton it will check for a previous existance and if one exists it will bring it to the foreground
here is the code:
VB Code:
Declare Function FindWindow Lib "coredll.dll" (ByVal className As Char(), ByVal WindowsName As Char()) As Integer
Declare Function SetForegroundWindow Lib "coredll.dll" (ByVal hwnd As Integer) As Boolean
Public Shared Sub Main()
Dim hWnd As Integer
hWnd = FindWindow(Nothing, "Form1 Caption".ToCharArray)
If hWnd <> 0 Then
SetForegroundWindow(hWnd)
Else
Application.Run(New Form1)
End If
End Sub
Form1 Caption is the text on the top of the form in the title bar
Rgds
Barry