1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
// 'renvoie la liste des icones présentes dans le tray
// 'comme les infos se trouvent dans Explorer.exe et non dans
// notre processus
// 'on doit allouée de la mémoire dans Explorer.exe pour
// pouvoir fournir un pointeur valide vers un TBBUTTON à
// SendMessage
function GetTrayIcons : TTBButtons; // 'cette fonction ne fonctionne que sous NT/2000/XP (car
// VirtualAllocEx n'est pas présent sous 9x)
Var hProc : THandle ; // Public Function GetTrayIcons() As Tray()
hTray : THandle ; // Dim hProc As Long 'handle de Explorer.exe
pid : Integer ; // Dim hTray As Long 'hWnd du tray
ptrVM : Integer ; // Dim pid As Long 'identifiant de Explorer.exe
ret : Cardinal ; // Dim ptrVM As Long 'pointeur vers la zone mémoire pour
// TBBUTTON
ptrTip : Integer; // Dim ret As Long 'valeur de retour
tbut : TTBButton; // Dim ptrTip As Long 'pointeur vers une chaîne Unicode
// (Tip ou Path)
TChar : Integer ; // Dim tbut As TBBUTTON 'les infos sur le bouton
x : Integer ; // Dim TChar As Integer 'contient un caractère unicode
nIcons : Integer ; // Dim x As Long 'compteur
Icons : TTBButtons ; // Dim nIcons As Long 'nombre d'icones dans le tray
O : Cardinal; // ajouté & à revoir // Dim Icons() As Tray 'tableau temporaire des icones dans
// le tray
begin
try
//on demande le hWnd du tray
hTray := GethWndTray; // hTray = GethWndTray
//si erreur
If hTray = 0 Then Exit ; // If hTray = 0 Then Exit Function
//on demande l'identifiant de Explorer.exe
GetWindowThreadProcessId(hTray, @pid) ; // GetWindowThreadProcessId hTray, pid
//on demande un handle de Explorer.exe
hProc := OpenProcess(PROCESS_VM_READ, False, pid) ; // hProc = OpenProcess(PROCESS_VM, 0, pid)
// ou PROCESS_VM_OPERATION : PROCESS_VM n'existe pas
//si erreur
If hProc = -1 Then // If hProc = -1 Then Err.Raise &H80070000
begin // & Err.LastDllError
Showmessage('Impossible d''obtenir Explorer.exe.') ;
Exit ;
End ;
//on alloue de la mémoire pour TBBUTTON
//ptrVM := VirtualAllocEx(hProc, @O, SizeOf(tbut), // ptrVM = VirtualAllocEx(hProc, ByVal 0&,
// MEM_COMMIT, PAGE_READWRITE); // Len(tbut), MEM_COMMIT, PAGE_READWRITE)
//si erreur
If ptrVM = 0 Then // If ptrVM = 0 Then Err.Raise &H80070000
begin // & Err.LastDllError
Showmessage('Impossible d''allouer de la mémoire');
Exit ;
End ;
//on demande le nombre de boutons dans le tray
nIcons := SendMessage(hTray, TB_BUTTONCOUNT, 0, 0) ; // nIcons = SendMessage(hTray, TB_BUTTONCOUNT,
// ByVal 0&, ByVal 0&)
//on redimensionne le tableau au nombre d'icones
SetLength(Icons, nIcons) ; // ReDim Icons(nIcons - 1)
//pour chaque icone
For x := 0 To nIcons -1 do // For x = 0 To nIcons - 1
begin
//on demande les infos sur le bouton
SendMessage(hTray, TB_GETBUTTON, x, ptrVM) ; // SendMessage hTray, TB_GETBUTTON, x, ByVal ptrVM
//on lit les infos sur le bouton
ReadProcessMemory(hProc, @ptrVM, @tbut, // ReadProcessMemory hProc, ByVal ptrVM, tbut,
Sizeof(tbut), ret) ; // Len(tbut), ret
//on extrait les données utiles
Icons[x].dwData := tbut.dwData; // Icons(x).dwData = tbut.dwData
Icons[x].fsState := tbut.fsState; // Icons(x).fsState = tbut.fsState
Icons[x].fsStyle := tbut.fsStyle; // Icons(x).fsStyle = tbut.fsStyle
Icons[x].iBitmap := tbut.iBitmap; // Icons(x).iBitmap = tbut.iBitmap
Icons[x].idCommand := tbut.idCommand; // Icons(x).idCommand = tbut.idCommand
Icons[x].iString := tbut.iString; // Icons(x).iString = tbut.iString
//le Tip
ptrTip := tbut.iString; // ptrTip = tbut.iString
repeat // Do
//on lit chaque caractère unicode
ReadProcessMemory(hProc, @ptrTip, @TChar, 2, ret);// ReadProcessMemory hProc, ByVal ptrTip,
// TChar, 2&, ret
//Icons[x].sTip := Icons[x].sTip & ChrW$(TChar) // Icons(x).sTip = Icons(x).sTip & ChrW$(TChar)
// pas de sTip dans la structure TBBUTTON :
{ typedef struct _TBBUTTON \\ tbb
int iBitmap;
int idCommand;
BYTE fsState;
BYTE fsStyle;
DWORD dwData;
int iString;
TBBUTTON, NEAR* PTBBUTTON, FAR* LPTBBUTTON;
typedef const TBBUTTON FAR* LPCTBBUTTON; }
ptrTip := ptrTip + 2; // ptrTip = ptrTip + 2
//jusqu'au NULL
until TChar = 0; // Loop While Tchar
//on lit les données sur l'icone (Path...)
ReadProcessMemory(hProc, @tbut.dwData, @Icons[x], 36, ret); // ReadProcessMemory hProc,
// ByVal tbut.dwData, Icons(x).hwnd,
// 36&, ret
//on demande le PID du processus
GetWindowThreadProcessId(pid, @Icons[x]); // GetWindowThreadProcessId Icons(x).hwnd, pid
//on lit le chemin et le nom de l'application propriétaire
GetProcessNameFromPID_NT(pid, Icons(x).uniPath // GetProcessNameFromPID_NT pid,
// Icons(x).uniPath
end ; // Next
// 'on renvoie le tout
finally
Result := Icons; // GetTrayIcons = Icons
//Cleanup: // ??? // Cleanup:
//on libère la mémoire allouée
VirtualFreeEx(hProc, @ptrVM, 0, MEM_RELEASE); // VirtualFreeEx hProc, ByVal ptrVM, 0&, MEM_RELEASE
//on ferme le handle de Explorer.exe
CloseHandle(hProc); // CloseHandle hProc
end;
end ; |
Partager