OVH Cloud OVH Cloud

app.previnstance is not full proof

10 réponses
Avatar
ThunderMusic
Hi,
at the very beginning of my app I do a if app.previnstance then unload
me. It work if the exe stays in the same path, but I can start an instance,
then copy the exe and start another instance from the other path. Is there a
way to prevent this? I mean, is there something more efficient than
app.previnstance?

thanks

ThunderMusic

10 réponses

Avatar
François Picalausa
Bonjour/soir,


Tu peux essayer FindWindow pour identifier ton application par le titre.. si
tu trouves une fenêtre portant le titre, c'est que ton appli est déjà
chargé. Il faut alors que le titre ne change pas.

Sinon, les deux applications sont bien différentes puisqu'elles n'ont pas le
même path/même nom... il est donc normal que PrevInstance renvoie faux.

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ThunderMusic" a écrit dans le message de
news:%
Hi,
at the very beginning of my app I do a if app.previnstance then
unload me. It work if the exe stays in the same path, but I can start
an instance, then copy the exe and start another instance from the
other path. Is there a way to prevent this? I mean, is there
something more efficient than app.previnstance?

thanks

ThunderMusic


Avatar
ThunderMusic
ok, but how do I use it in VB? because I always return 0
I use it like this :
hResult = FindWindow(0, FormTitle)

is it correct? is there something better?

thanks

ThunderMusic

"François Picalausa" wrote in message
news:
Bonjour/soir,


Tu peux essayer FindWindow pour identifier ton application par le titre..


si
tu trouves une fenêtre portant le titre, c'est que ton appli est déjà
chargé. Il faut alors que le titre ne change pas.

Sinon, les deux applications sont bien différentes puisqu'elles n'ont pas


le
même path/même nom... il est donc normal que PrevInstance renvoie faux.

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ThunderMusic" a écrit dans le message de
news:%
> Hi,
> at the very beginning of my app I do a if app.previnstance then
> unload me. It work if the exe stays in the same path, but I can start
> an instance, then copy the exe and start another instance from the
> other path. Is there a way to prevent this? I mean, is there
> something more efficient than app.previnstance?
>
> thanks
>
> ThunderMusic




Avatar
François Picalausa
Bonjour/soir,

Il faut passer en premier argument un vbNullString (sauf si l'argument a été
déclaré en tant que byval Long auquel cas, 0 est adapté):

'If the function fails, the return value is NULL
If FindWindow(vbNullString, Text1.Text) = 0 Then
MsgBox "Pas de fenêtre correspondante"
Else
MsgBox "La fenêtre a été trouvée"
End If

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ThunderMusic" a écrit dans le message de
news:e7kGB$
ok, but how do I use it in VB? because I always return 0
I use it like this :
hResult = FindWindow(0, FormTitle)

is it correct? is there something better?

thanks

ThunderMusic

"François Picalausa" wrote in message
news:
Bonjour/soir,


Tu peux essayer FindWindow pour identifier ton application par le
titre.. si tu trouves une fenêtre portant le titre, c'est que ton
appli est déjà chargé. Il faut alors que le titre ne change pas.

Sinon, les deux applications sont bien différentes puisqu'elles
n'ont pas le même path/même nom... il est donc normal que
PrevInstance renvoie faux.

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ThunderMusic" a écrit dans le message
de news:%
Hi,
at the very beginning of my app I do a if app.previnstance then
unload me. It work if the exe stays in the same path, but I can
start an instance, then copy the exe and start another instance
from the other path. Is there a way to prevent this? I mean, is
there
something more efficient than app.previnstance?

thanks

ThunderMusic






Avatar
ThunderMusic
avec vbnullstring ca fonctionne. merci beaucoup.

"François Picalausa" wrote in message
news:%
Bonjour/soir,

Il faut passer en premier argument un vbNullString (sauf si l'argument a


été
déclaré en tant que byval Long auquel cas, 0 est adapté):

'If the function fails, the return value is NULL
If FindWindow(vbNullString, Text1.Text) = 0 Then
MsgBox "Pas de fenêtre correspondante"
Else
MsgBox "La fenêtre a été trouvée"
End If

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ThunderMusic" a écrit dans le message de
news:e7kGB$
> ok, but how do I use it in VB? because I always return 0
> I use it like this :
> hResult = FindWindow(0, FormTitle)
>
> is it correct? is there something better?
>
> thanks
>
> ThunderMusic
>
> "François Picalausa" wrote in message
> news:
>> Bonjour/soir,
>>
>>
>> Tu peux essayer FindWindow pour identifier ton application par le
>> titre.. si tu trouves une fenêtre portant le titre, c'est que ton
>> appli est déjà chargé. Il faut alors que le titre ne change pas.
>>
>> Sinon, les deux applications sont bien différentes puisqu'elles
>> n'ont pas le même path/même nom... il est donc normal que
>> PrevInstance renvoie faux.
>>
>> --
>> François Picalausa (MVP VB)
>> FAQ VB : http://faq.vb.free.fr
>> MSDN : http://msdn.microsoft.com
>>
>>
>> "ThunderMusic" a écrit dans le message
>> de news:%
>>> Hi,
>>> at the very beginning of my app I do a if app.previnstance then
>>> unload me. It work if the exe stays in the same path, but I can
>>> start an instance, then copy the exe and start another instance
>>> from the other path. Is there a way to prevent this? I mean, is
>>> there
>>> something more efficient than app.previnstance?
>>>
>>> thanks
>>>
>>> ThunderMusic




Avatar
François Picalausa
Bonjour/soir,

Vérifie que la feuille de l'instance courrante ne doit pas être chargée lors
de l'appel à FindWindow.
Le mieux est de mpasser par un sub main de démarrage où tu fais

Sub Main()
If FindWindow(vbNullString, frmMaFeuillePrincipale.Caption) = 0 Then
frmMaFeuillePrincipale.Show
End If
End Sub

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ThunderMusic" a écrit dans le message de
news:
finalement, ca ne fonctionne pas. la fonction ne retourne jamais 0.
Est-ce normal?
j'y vais de cette facon :
hResult = FindWindow(vbNullString, FormTitle)

et le hResult est toujours <> 0, résultat, je ne peux jamais entrer
dans mon application

Est-ce que je fais quelque chose incorrectement?

merci

ThunderMusic


"François Picalausa" wrote in message
news:%
Bonjour/soir,

Il faut passer en premier argument un vbNullString (sauf si
l'argument a été déclaré en tant que byval Long auquel cas, 0 est
adapté):

'If the function fails, the return value is NULL
If FindWindow(vbNullString, Text1.Text) = 0 Then
MsgBox "Pas de fenêtre correspondante"
Else
MsgBox "La fenêtre a été trouvée"
End If

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ThunderMusic" a écrit dans le message
de news:e7kGB$
ok, but how do I use it in VB? because I always return 0
I use it like this :
hResult = FindWindow(0, FormTitle)

is it correct? is there something better?

thanks

ThunderMusic

"François Picalausa" wrote in message
news:
Bonjour/soir,


Tu peux essayer FindWindow pour identifier ton application par le
titre.. si tu trouves une fenêtre portant le titre, c'est que ton
appli est déjà chargé. Il faut alors que le titre ne change pas.

Sinon, les deux applications sont bien différentes puisqu'elles
n'ont pas le même path/même nom... il est donc normal que
PrevInstance renvoie faux.

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ThunderMusic" a écrit dans le
message de news:%
Hi,
at the very beginning of my app I do a if app.previnstance
then unload me. It work if the exe stays in the same path, but I
can
start an instance, then copy the exe and start another instance
from the other path. Is there a way to prevent this? I mean, is
there
something more efficient than app.previnstance?

thanks

ThunderMusic










Avatar
ThunderMusic
c'est en plein ca que je fais. Je ne peux pas le faire exactement parce que
le caption de ma feuille est chargé dynamiquement à partir d'une string
table, mais en gros, c'est pas mal ca. et je recois toujours la même valeur
et je ne sais pas c'est quoi cette valeur. Si je redemarre mon ordinateur,
la valeur change. Et j'avais pensé à l'éventualité du sub main, mais ca ne
change rien (dans ce cas-ci, ca n'a rien changé) le problème persiste.

autre solution?

merci

ThunderMusic

"François Picalausa" wrote in message
news:%
Bonjour/soir,

Vérifie que la feuille de l'instance courrante ne doit pas être chargée


lors
de l'appel à FindWindow.
Le mieux est de mpasser par un sub main de démarrage où tu fais

Sub Main()
If FindWindow(vbNullString, frmMaFeuillePrincipale.Caption) = 0 Then
frmMaFeuillePrincipale.Show
End If
End Sub

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ThunderMusic" a écrit dans le message de
news:
> finalement, ca ne fonctionne pas. la fonction ne retourne jamais 0.
> Est-ce normal?
> j'y vais de cette facon :
> hResult = FindWindow(vbNullString, FormTitle)
>
> et le hResult est toujours <> 0, résultat, je ne peux jamais entrer
> dans mon application
>
> Est-ce que je fais quelque chose incorrectement?
>
> merci
>
> ThunderMusic
>
>
> "François Picalausa" wrote in message
> news:%
>> Bonjour/soir,
>>
>> Il faut passer en premier argument un vbNullString (sauf si
>> l'argument a été déclaré en tant que byval Long auquel cas, 0 est
>> adapté):
>>
>> 'If the function fails, the return value is NULL
>> If FindWindow(vbNullString, Text1.Text) = 0 Then
>> MsgBox "Pas de fenêtre correspondante"
>> Else
>> MsgBox "La fenêtre a été trouvée"
>> End If
>>
>> --
>> François Picalausa (MVP VB)
>> FAQ VB : http://faq.vb.free.fr
>> MSDN : http://msdn.microsoft.com
>>
>>
>> "ThunderMusic" a écrit dans le message
>> de news:e7kGB$
>>> ok, but how do I use it in VB? because I always return 0
>>> I use it like this :
>>> hResult = FindWindow(0, FormTitle)
>>>
>>> is it correct? is there something better?
>>>
>>> thanks
>>>
>>> ThunderMusic
>>>
>>> "François Picalausa" wrote in message
>>> news:
>>>> Bonjour/soir,
>>>>
>>>>
>>>> Tu peux essayer FindWindow pour identifier ton application par le
>>>> titre.. si tu trouves une fenêtre portant le titre, c'est que ton
>>>> appli est déjà chargé. Il faut alors que le titre ne change pas.
>>>>
>>>> Sinon, les deux applications sont bien différentes puisqu'elles
>>>> n'ont pas le même path/même nom... il est donc normal que
>>>> PrevInstance renvoie faux.
>>>>
>>>> --
>>>> François Picalausa (MVP VB)
>>>> FAQ VB : http://faq.vb.free.fr
>>>> MSDN : http://msdn.microsoft.com
>>>>
>>>>
>>>> "ThunderMusic" a écrit dans le
>>>> message de news:%
>>>>> Hi,
>>>>> at the very beginning of my app I do a if app.previnstance
>>>>> then unload me. It work if the exe stays in the same path, but I
>>>>> can
>>>>> start an instance, then copy the exe and start another instance
>>>>> from the other path. Is there a way to prevent this? I mean, is
>>>>> there
>>>>> something more efficient than app.previnstance?
>>>>>
>>>>> thanks
>>>>>
>>>>> ThunderMusic




Avatar
Fabrice MALAINGRE
Bonjour ThunderMusic,

Autre solution?



Mettre en place en objet de synchronisation "noyau" de type Mutex ?

Cordialement

____________________________
Fabrice MALAINGRE
Architecte Logiciel - Chef de Projet
THEORIS - www.theoris.fr
Avatar
ThunderMusic
ca ne fonctionne toujours pas, et même avec le ShowWindow, il ne montre pas
de fenetre et la fait encore moins flasher. (aurais-je frappé un bug de
FindWindow)?

merci quand même...

ThunderMusic

"François Picalausa" wrote in message
news:%
Bonjour/soir,

Cette valeur est un hwnd correspondant à la feuille (fenêtre) trouvée.
Pour voir exactement quelle feuille est retrouvée, tu peux faire ceci:

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long,


ByVal
nCmdShow As Long) As Long
Private Const SW_SHOWMAXIMIZED = 3
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"


(ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long,


ByVal
bInvert As Long) As Long

Private Sub Command1_Click()
Dim lnghWnd As Long
lnghWnd = FindWindow(vbNullString, Text1.Text)
Text2.Text = lnghWnd

If lnghWnd Then
ShowWindow lnghWnd, SW_SHOWMAXIMIZED
FlashWindow lnghWnd, 1
End If
End Sub

FindWindow ne renvoie une valeur que lorsque la feuille est trouvée.

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ThunderMusic" a écrit dans le message de
news:%
> c'est en plein ca que je fais. Je ne peux pas le faire exactement
> parce que le caption de ma feuille est chargé dynamiquement à partir
> d'une string table, mais en gros, c'est pas mal ca. et je recois
> toujours la même valeur et je ne sais pas c'est quoi cette valeur. Si
> je redemarre mon ordinateur, la valeur change. Et j'avais pensé à
> l'éventualité du sub main, mais ca ne change rien (dans ce cas-ci, ca
> n'a rien changé) le problème persiste.
>
> autre solution?
>
> merci
>
> ThunderMusic
>
> "François Picalausa" wrote in message
> news:%
>> Bonjour/soir,
>>
>> Vérifie que la feuille de l'instance courrante ne doit pas être
>> chargée lors de l'appel à FindWindow.
>> Le mieux est de mpasser par un sub main de démarrage où tu fais
>>
>> Sub Main()
>> If FindWindow(vbNullString, frmMaFeuillePrincipale.Caption) = 0
>> Then frmMaFeuillePrincipale.Show
>> End If
>> End Sub
>>
>> --
>> François Picalausa (MVP VB)
>> FAQ VB : http://faq.vb.free.fr
>> MSDN : http://msdn.microsoft.com
>>
>>
>> "ThunderMusic" a écrit dans le message
>> de news:
>>> finalement, ca ne fonctionne pas. la fonction ne retourne jamais 0.
>>> Est-ce normal?
>>> j'y vais de cette facon :
>>> hResult = FindWindow(vbNullString, FormTitle)
>>>
>>> et le hResult est toujours <> 0, résultat, je ne peux jamais entrer
>>> dans mon application
>>>
>>> Est-ce que je fais quelque chose incorrectement?
>>>
>>> merci
>>>
>>> ThunderMusic
>>>
>>>
>>> "François Picalausa" wrote in message
>>> news:%
>>>> Bonjour/soir,
>>>>
>>>> Il faut passer en premier argument un vbNullString (sauf si
>>>> l'argument a été déclaré en tant que byval Long auquel cas, 0 est
>>>> adapté):
>>>>
>>>> 'If the function fails, the return value is NULL
>>>> If FindWindow(vbNullString, Text1.Text) = 0 Then
>>>> MsgBox "Pas de fenêtre correspondante"
>>>> Else
>>>> MsgBox "La fenêtre a été trouvée"
>>>> End If
>>>>
>>>> --
>>>> François Picalausa (MVP VB)
>>>> FAQ VB : http://faq.vb.free.fr
>>>> MSDN : http://msdn.microsoft.com
>>>>
>>>>
>>>> "ThunderMusic" a écrit dans le
>>>> message de news:e7kGB$
>>>>> ok, but how do I use it in VB? because I always return 0
>>>>> I use it like this :
>>>>> hResult = FindWindow(0, FormTitle)
>>>>>
>>>>> is it correct? is there something better?
>>>>>
>>>>> thanks
>>>>>
>>>>> ThunderMusic
>>>>>
>>>>> "François Picalausa" wrote in message
>>>>> news:
>>>>>> Bonjour/soir,
>>>>>>
>>>>>>
>>>>>> Tu peux essayer FindWindow pour identifier ton application par le
>>>>>> titre.. si tu trouves une fenêtre portant le titre, c'est que ton
>>>>>> appli est déjà chargé. Il faut alors que le titre ne change pas.
>>>>>>
>>>>>> Sinon, les deux applications sont bien différentes puisqu'elles
>>>>>> n'ont pas le même path/même nom... il est donc normal que
>>>>>> PrevInstance renvoie faux.
>>>>>>
>>>>>> --
>>>>>> François Picalausa (MVP VB)
>>>>>> FAQ VB : http://faq.vb.free.fr
>>>>>> MSDN : http://msdn.microsoft.com
>>>>>>
>>>>>>
>>>>>> "ThunderMusic" a écrit dans le
>>>>>> message de news:%
>>>>>>> Hi,
>>>>>>> at the very beginning of my app I do a if app.previnstance
>>>>>>> then unload me. It work if the exe stays in the same path, but I
>>>>>>> can
>>>>>>> start an instance, then copy the exe and start another instance
>>>>>>> from the other path. Is there a way to prevent this? I mean, is
>>>>>>> there
>>>>>>> something more efficient than app.previnstance?
>>>>>>>
>>>>>>> thanks
>>>>>>>
>>>>>>> ThunderMusic




Avatar
Fabrice MALAINGRE
> j'y avais pensé, mais ca existe un mutex en vb?



Pas directement, mais grâce au mot magique "declare", tout est possible :-)

Tu devrais pouvoir t'en sortir grâce aux déclarations suivantes :

Public Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA"
(lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long,
ByVal lpName As String) As Long

Public Declare Function OpenMutex Lib "kernel32" Alias "OpenMutexA" (ByVal
dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As
String) As Long

Public Declare Function ReleaseMutex Lib "kernel32" Alias "ReleaseMutex"
(ByVal hMutex As Long) As Long

Public Declare Function WaitForSingleObject Lib "kernel32" Alias
"WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long)
As Long

Cordialement
____________________________
Fabrice MALAINGRE
Architecte Logiciel - Chef de Projet
THEORIS - www.theoris.fr
Avatar
ThunderMusic
soit je ne sais pas comment créer un mutex, soit ca ne fonctionne pas. j'y
vais comme ca, et il crée toujours un mutex différent.

Public Function IsPrevInstance() As Boolean

Dim x As SECURITY_ATTRIBUTES

glMutex = CreateMutex(x, False, "MyForm Title")

'Return the right value
If glMutex = 0 Then
IsPrevInstance = True
Else
IsPrevInstance = False
End If
End Function

est-ce qu'il y a quelque chose qui n'est pas correct? il est a noter que si
je mets vbnull au lieu de x dans le CreateMutex, vb ne veut pas compiler.

merci

ThunderMusic


"Fabrice MALAINGRE" wrote in message
news:
> j'y avais pensé, mais ca existe un mutex en vb?

Pas directement, mais grâce au mot magique "declare", tout est possible


:-)

Tu devrais pouvoir t'en sortir grâce aux déclarations suivantes :

Public Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA"
(lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long,
ByVal lpName As String) As Long

Public Declare Function OpenMutex Lib "kernel32" Alias "OpenMutexA" (ByVal
dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As
String) As Long

Public Declare Function ReleaseMutex Lib "kernel32" Alias "ReleaseMutex"
(ByVal hMutex As Long) As Long

Public Declare Function WaitForSingleObject Lib "kernel32" Alias
"WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As


Long)
As Long

Cordialement
____________________________
Fabrice MALAINGRE
Architecte Logiciel - Chef de Projet
THEORIS - www.theoris.fr