Savoir si nouvelle fenètre est ouverte

ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 - 10 juin 2005 à 14:43
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 - 11 juin 2005 à 16:18
Encore bonjour, c'est encore moi !

(Je crois que je deviens accro à cppfrance ... faut que vous fassiez attention les nouveaux inscits, ça va très vite....)

J'aimerai pouvoir savoir quand une nouvelle fenètre s'ouvre !!!! (Et ensuite récupérer sa classe...)

Je suppose qu'il faut faire un hook global...

Une indication ?

Merci à tous ceux qui pourront me répondre...

26 réponses

vecchio56 Messages postés 6535 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 22 août 2010 14
10 juin 2005 à 15:04
Exact, il faut faire un hook, de type WH_CBT:



LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)

{

if(nCode == HCBT_CREATEWND)

{

// Création d'une nouvelle fenêtre

}

return CallNextHookEx(hhk, nCode, wParam, lParam);

}
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
10 juin 2005 à 15:16
Merci et quelle est la fonction qui appelle celle-ci ?

(hhk je suppose que c'est le hook ?)
0
vecchio56 Messages postés 6535 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 22 août 2010 14
10 juin 2005 à 15:18
C'est le système qui l'appelle bien entendu, sinon aucun intérêt
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
10 juin 2005 à 15:23
Je fais ça :

SetWindowsHookEx( WH_CBT, (HOOKPROC) CBTProc, hExe, NULL);

Mais rien...

Où sont mes erreurs ?
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
vecchio56 Messages postés 6535 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 22 août 2010 14
10 juin 2005 à 15:52
hExe -> tu dois mettre l'instance de la DLL (ce code figure bien dans la dll?)
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
10 juin 2005 à 16:11
Non j'ai fait un hook global sans DLL.....
Tout à refaire ou il y a une solution ?
0
vecchio56 Messages postés 6535 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 22 août 2010 14
10 juin 2005 à 16:16
Autre solution, selon racpp. Jamais essayé, a toi de voir si ca marche:

http://www.cppfrance.com/code.aspx?ID=27169
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
10 juin 2005 à 16:32
C'est ce que j'ai fait mais ça marche pas....

La fonction n'est jamais appelée...

Par quoi puis-je remplacer hExe pour que cela fonctionne ?

Merci de ton aide
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
10 juin 2005 à 17:15
Voilà j'ai créé la DLL comme ça :

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>



HHOOK hhk;
HINSTANCE hThisMod;


LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam);


__declspec (dllexport) int InitHook (void);
__declspec (dllexport) int EndHook (void);



LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode = = HCBT_CREATEWND)
{
ShellExecute(NULL, "open", "mon_programme.exe", 0, NULL, SW_SHOWNORMAL);
// Création d'une nouvelle fenêtre
}
return CallNextHookEx(hhk, nCode, wParam, lParam);
}


__declspec (dllexport) int InitHook ()
{
if (!hhk) hhk = SetWindowsHookEx (WH_CBT, (HOOKPROC)CBTProc, hThisMod, 0);
return 1;



}



__declspec (dllexport) int EndHook ()
{


if (hhk) {
UnhookWindowsHookEx (hhk);
return 1;
}
return 0;
}

Mais ça marche pas !!!!!!!!

Why ???
0
vecchio56 Messages postés 6535 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 22 août 2010 14
10 juin 2005 à 17:16
Tu as regardé l'exemple que je t'ai donné.

Moi je fais avec une DLL, donc a la place de l'exe, on met l'instance de la DLL
0
vecchio56 Messages postés 6535 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 22 août 2010 14
10 juin 2005 à 17:18
Je vois pas de DllMain...

Tu dois initiliser hThisMod dans DllMain:



BOOL WINAPI DllMain(HINSTANCE hDll, DWORD dwReason, LPVOID Reserved)

{

if(dwReason == DLL_PROCESS_ATTACH)

{

hThisMod = hDll;

DisableThreadLibraryCalls(hDll);

}

return 1;

}
0
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
10 juin 2005 à 17:53
exemple complet ici:
http://www.cppfrance.com/code.aspx?id=17387

ciao...
BruNews, MVP VC++
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
10 juin 2005 à 19:37
Oui voilà ce que j'avais oublié de montrer :

BOOL APIENTRY DllMain (HINSTANCE hInst ,
DWORD reason ,
LPVOID reserved )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
hThisMod = hInst;
DisableThreadLibraryCalls(hInst);
break;


case DLL_PROCESS_DETACH:
break;


case DLL_THREAD_ATTACH:
break;


case DLL_THREAD_DETACH:
break;
}



return TRUE;
}

And it doesn't work ...

Il faut rajouter des options au compilateur ?
J'utilise Dev Cpp 4.9.9.2

Merci de ton aide en tt cas
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
10 juin 2005 à 19:39
Ok je vais voir Brunew merci
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
10 juin 2005 à 20:31
Dev me fais plein d'erreur...

Comment réussir à partir de mon code ?
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
11 juin 2005 à 12:24
Ca marche mais à moitié :

Au début j'ai mis : hhk = SetWindowsHookEx (WH_SHELL, (HOOKPROC)CBTProc, hThisMod, 0);

LRESULT CALLBACK CBTProc( int nCode, WPARAM wParam, LPARAM lParam)
{
//Si un fenètre s'ouvre, ici, tout s'exécute !


if(nCode == HSHELL_WINDOWCREATED) {
//Ici rien ne se passe !!!
}

return CallNextHookEx(hhk, nCode, wParam, lParam);
}

Comment est-ce possible ?
0
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
11 juin 2005 à 13:13
hhk = SetWindowsHookEx(...)

hhk est bien != NULL ???

ciao...
BruNews, MVP VC++
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
11 juin 2005 à 13:43
Oui j'ai vérifié il me renvoie 1 :

__declspec (dllexport) int InitHook ()
{
if (!hhk){

if ( (hhk = SetWindowsHookEx (WH_SHELL, (HOOKPROC)CBTProc, hThisMod, 0))==1 ) return 1;

}

return 0;
}
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
11 juin 2005 à 14:07
Bref, par portions de code on y arrivera jamais :
Voilà mes 2 codes :

Celui de la DLL :

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>


HHOOK hhk;
HINSTANCE hThisMod;


LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam);


__declspec (dllexport) int InitHook (void);
__declspec (dllexport) int EndHook (void);

<HR>



LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{

printf("***** Evenement *****\n");
if(nCode = = HSHELL_WINDOWCREATED)
{
printf("***** Fenetre ouverte *****\n");
}
return CallNextHookEx(hhk, nCode, wParam, lParam);
}

<HR>



__declspec (dllexport) int InitHook ()
{
printf( "***** Inithook *****\n" );
if (!hhk){
if ( (hhk = SetWindowsHookEx (WH_SHELL, (HOOKPROC)CBTProc, hThisMod, 0))= =1 ) return 1;
}
return 0;

}



<HR>

__declspec (dllexport) int EndHook ()
{
printf( "***** Endhook *****\n" );
if (hhk) {
UnhookWindowsHookEx (hhk);
return 1;
}
return 0;
}



<HR>

BOOL APIENTRY DllMain (HINSTANCE hInst ,
DWORD reason ,
LPVOID reserved )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
hThisMod = hInst;
DisableThreadLibraryCalls(hInst);
break;


case DLL_PROCESS_DETACH:
break;


case DLL_THREAD_ATTACH:
break;


case DLL_THREAD_DETACH:
break;
}


return TRUE;
}

<HR>

<HR>




Et celui de mon programme lanceur :

#include <stdio.h>
#include <stdlib.h>


int rep;
int InitHook (void);
int EndHook (void);


int main(int argc, char *argv[])
{
while (1) {
printf("1- Hook\n2- Endhook\n\n= => ");
scanf("%d", &rep);
if (rep 1) printf("Hook r\202pond> %d\n-----------------------------------\n", InitHook());

if (rep ==2) printf("Endhook r\202pond ==> %d\n-----------------------------------\n", EndHook());
}

return 0;
}


<HR>

Voilà voilà et les fonctions InitHook() et EndHook() me réponde aléatoirement 1 ou 0...
(Par exemple je fais InitHook() qui me renvoie 0 et ensuite EndHook() qui me renvoie 1...!!! )
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
11 juin 2005 à 14:14
Je viens de me rendre compte que hhk ne "disparait" jamais meme si je fais UnHookWindowsHookEx(hhk) ...?
0
Rejoignez-nous