Hey, could you try this code and let me know if it works for you? Here's the optimized version. Thanks!"
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <WinAPIIcons.au3>
#include <WinAPIShellEx.au3>
; Constants for SetBalloonInfo
;Global Const $NIIF_NONE = 0x00000000, $NIIF_USER = 0x00000004, $NIIF_NOSOUND = 0x00000010, $NIIF_RESPECT_QUIET_TIME = 0x00000080
; Function to create and display the notification
Func CreateSystemNotification($sTitle, $sMessage, $iTimeout = 2000, $iIconIndex = 13, $sSound = "")
Local $hIcon = 0, $oNotif = 0, $bSuccess = False
Local Const $sCLSID_IUserNotification = "{0010890E-8789-413C-ADBC-48F5B511B3AF}"
Local Const $sIID_IUserNotification = "{BA9711BA-5893-4787-A7E1-41277151550B}"
Local Const $tagIUserNotification = "SetBalloonInfo long(wstr;wstr;dword);" & _
"SetBalloonRetry long(dword;dword;uint);" & _
"SetIconInfo long(ptr;wstr);" & _
"Show long(ptr;dword);" & _
"PlaySound long(wstr);"
; Create COM instance
$oNotif = ObjCreateInterface($sCLSID_IUserNotification, $sIID_IUserNotification, $tagIUserNotification)
If Not IsObj($oNotif) Then
ConsoleWrite("Error: Failed to create COM object." & @CRLF)
Return False
EndIf
; Load icon
Local $dwFlags = $NIIF_USER
If Not IsInt($iIconIndex) Or $iIconIndex < 0 Then
$iIconIndex = 13 ; Default icon
Else
Local $iTotal = _WinAPI_ExtractIconEx(@SystemDir & '\shell32.dll', -1, 0, 0, 0)
If $iIconIndex >= $iTotal Then
$iIconIndex = 13 ; Fallback to default if index is out of range
EndIf
EndIf
$hIcon = _WinAPI_ShellExtractIcon(@SystemDir & '\shell32.dll', $iIconIndex, 32, 32)
If Not $hIcon Then
ConsoleWrite("Error: Failed to load icon. Switching to NIIF_NONE." & @CRLF)
$dwFlags = $NIIF_NONE
EndIf
; Configure notification
If $sSound Then $oNotif.PlaySound($sSound)
$oNotif.SetBalloonInfo($sTitle, $sMessage, BitOR($NIIF_RESPECT_QUIET_TIME, $dwFlags))
$oNotif.SetBalloonRetry(0, 0, 0)
$oNotif.SetIconInfo($hIcon, $sTitle)
$oNotif.Show(0, $iTimeout)
$bSuccess = True ; Mark success if we reach this point
; Cleanup
If $hIcon Then _WinAPI_DestroyIcon($hIcon)
$oNotif = 0 ; AutoIt handles COM cleanup automatically
Return $bSuccess
EndFunc ;==>CreateSystemNotification
; Example usage
If CreateSystemNotification("Notification Title", "This is an AutoIt message", 2000) Then
ConsoleWrite("Notification displayed successfully." & @CRLF)
Else
ConsoleWrite("Failed to display notification." & @CRLF)
EndIf