Exemples d’utilisation de fonctions API Windows
API:exemples d’utilisation1. Récupération du répertoire Windows
Déclaration de la fonction API :
' Cette fonction API renvoie le répertoire Windows
Private Declare Function GetWindowsDirectory _
Lib "kernel32" Alias "GetWindowsDirectoryA" _
(ByVal lpWindowsDir As String, _
ByVal lpWindowsHeight As Long) _
As Long
Appel de la fonction API :
Function GetWinPath() As String
' Cette fonction VBA renvoie le répertoire Windows
Dim StrResult As String
Dim StrProfile As String
StrResult = String(255, " ")
StrProfile = GetWindowsDirectory(StrResult, 255)
' Tronque la chaîne au premier caractère nul
If StrProfile <> "" Then
StrResult = Trim(StrResult)
GetWinPath = Left(StrResult, InStr(1, StrResult, vbNullChar) - 1)
Else
GetWinPath = ""
End If
End Function
2. Lancement de la calculatrice Windows
Cet exemple permet de tester si la calculatrice Windows est déjà active, et de lancer son exécution sinon.
Deux fonctions API sont ici utilisées : la première, FindWindow, permet de rechercher la fenêtre "Calculatrice" ;...