Jump to content

1 Screenshot

About This File

ImageSearchExDLL & UDF - The Complete Guide

Author: Dao Van Trong - TRONG.PRO

Last Updated: 2025-09-15

1. Introduction

ImageSearchDLL is a high-performance image recognition solution for Windows, designed to be both powerful for modern systems and compatible with legacy environments. The project consists of three distinct C++ DLL versions and a smart AutoIt User-Defined Function (UDF) that automatically selects the best DLL for the job.

This architecture ensures that your scripts get the best possible performance on modern systems (like Windows 10/11) while maintaining full functionality on older systems like Windows 7 and even Windows XP.

New is the Special Feature is the function of searching for pictures in pictures!

2. How it Works: The Smart UDF Loader

The core of this project is the AutoIt UDF (ImageSearchEx_UDF.au3). You don’t need to worry about which DLL to use; the _ImageSearchEx_Startup() function handles it all automatically.

Here is the loading logic:

  1. On Modern OS (Windows 8, 10, 11+):
    • It first looks for the Modern DLL (ImageSearchEx_x64.dll or ImageSearchEx_x86.dll).
    • If not found, it falls back to the Windows 7 DLL.
    • If neither is found, it deploys the Embedded XP DLL.
  2. On Windows 7:
    • It prioritizes the Windows 7 DLL (ImageSearchEx_Win7_x64.dll or ImageSearchEx_Win7_x86.dll).
    • If not found, it deploys the Embedded XP DLL.
  3. On Windows XP:
    • It exclusively uses the Embedded XP DLL, which is extracted from a HEX string inside the UDF.

This ensures maximum performance where possible and maximum compatibility where needed.

3. The DLL Versions Explained

There are three distinct DLLs, each compiled for a specific purpose.

Feature Modern (Win10+) Windows 7 Legacy (XP)
Target OS Windows 8, 10, 11+ Windows 7 SP1+ Windows XP SP3+
Filename ImageSearchEx_x64.dll
ImageSearchEx_x86.dll
ImageSearchEx_Win7_x64.dll
ImageSearchEx_Win7_x86.dll
Embedded in UDF
Compiler VS 2022 (C++23) VS 2017+ (C++14) VS 2010 (C++03)
Performance Excellent Very Good Good
AVX2 Support Yes (auto-detected) Yes (auto-detected) No
Thread-Safety Yes (thread_local) Yes (thread_local) No (static buffer)
Best Use Case High-performance automation on modern PCs. Scripts that need to run reliably on both modern systems and Windows 7 machines. Maximum compatibility for legacy systems or when no external DLLs are provided.

4. Getting Started (For AutoIt Users)

Using the library is simple. Just make sure your files are organized correctly.

File Structure

For the best experience, place the DLL files in the same directory as your script and the UDF.

/YourScriptFolder/
|
|-- MyScript.au3
|-- ImageSearch_UDF.au3
|-- ImageSearchEx_x64.dll (Modern DLL for 64-bit)
|-- ImageSearchEx_x86.dll (Modern DLL for 32-bit)
|-- ImageSearchEx_Win7_x64.dll (Win7 DLL for 64-bit)
|-- ImageSearchEx_Win7_x86.dll (Win7 DLL for 32-bit)
|
/-- images/
|-- button.png

Quick Start Example

Here is a basic AutoIt script to get you started.

#include "ImageSearchEx_UDF.au3"

; 1. Initialize the library. The UDF will automatically load the best DLL.  
_ImageSearchEx_Startup()  
If @error Then  
    MsgBox(16, "Error", "ImageSearchEx DLL could not be initialized. Exiting.")  
    Exit  
EndIf

; You can check which version was loaded  
ConsoleWrite(">> Loaded DLL Version: " & _ImageSearchEx_GetVersion() & @CRLF)  
ConsoleWrite(">> System Info: " & _ImageSearchEx_GetSysInfo() & @CRLF)

; 2. Perform a search for an image on the entire screen.  
Local $sImagePath = @ScriptDir & "\images\button.png"  
Local $aResult = _ImageSearchEx($sImagePath)

; 3. Process the results. The result is ALWAYS a 2D array.  
If $aResult[0][0] > 0 Then  
    ConsoleWrite("Found " & $aResult[0][0] & " match(es)!" & @CRLF)  
    ; Loop through each match  
    For $i = 1 To $aResult[0][0]  
        Local $iX = $aResult[$i][1] ; X coordinate  
        Local $iY = $aResult[$i][2] ; Y coordinate  
        ConsoleWrite("Match #" & $i & " found at: " & $iX & ", " & $iY & @CRLF)  
        MouseMove($iX, $iY, 20)  
        Sleep(1000)  
    Next  
Else  
    ConsoleWrite("Image not found." & @CRLF)  
EndIf

; 4. Shutdown is handled automatically when the script exits. No need to call _ImageSearchEx_Shutdown().

 

5. Full API Reference

Main Functions

  • _ImageSearchEx_Area(…): The main function with all available options.
  • _ImageSearchEx(…): A simplified wrapper for searching the entire screen.
  • _ImageInImageSearchEx_Area(…): Searches for an image within another image file.

Common Parameters

Parameter Description Default
$sImageFile Path to the image(s). Use | to search for multiple images.  
$iLeft, $iTop, $iRight, $iBottom The coordinates of the search area. Entire Screen
$iTolerance Color tolerance (0-255). Higher values allow for more variation. 10
$iTransparent A color in 0xRRGGBB format to be ignored during the search. -1 (disabled)
$iMultiResults The maximum number of results to return. 1
$iCenterPos 1 returns the center coordinates; 0 returns the top-left. 1
$fMinScale, $fMaxScale Minimum and maximum scaling factor (e.g., 0.8 for 80%). 1.0
$fScaleStep The increment between scales (e.g., 0.1 for 10% steps). 0.1
$iFindAllOccurrences 1 finds all matches; 0 stops after the first. 0
$iUseCache 1 enables the file-based location cache; 0 disables it. 1
$iDisableAVX2 1 disables AVX2 optimization (for debugging). 0

Return Value

All search functions return a 2D array.

  • $aResult[0][0]: Contains the number of matches found.
  • For each match $i (from 1 to $aResult[0][0]):
    • aResult[aResult[1]: X coordinate
  • aResult[aResult[2]: Y coordinate
aResult[aResult[3]: Width of the found image aResult[aResult[4]: Height of the found image

Utility Functions

_ImageSearchEx_GetVersion(): Returns the version string of the currently loaded DLL. _ImageSearchEx_GetSysInfo(): Returns system info from the DLL (AVX2 support, screen resolution). _ImageSearchEx_ClearCache(): Deletes all cache files from the temp directory.

 

The download file comes with the ImageSearch Automation Suite version so you can quickly test the UDF:

Screenshots.png

 

Support my work ? Click here to sponsor!

________________________________________________________

Thanks OhItsThatGuy for sponsoring!

Edited by Trong
New restructured version!


What's New in Version 2.0.0.0

Released

Complete basic functions!


User Feedback

Recommended Comments

OhItsThatGuy

Posted

Hi Trong, I love your work! Clean code, easy to read and use. I checked out your other projects as well!

Regarding your ImageSearch library, can I make a small request?

Having a scan function that can take a bitmap handle would be amazing. This would allow a finer control by eliminating the need to force a re-scan on each iteration of _ImageSearch(..)

_ImageSearch_ScanSnapshot($hImage = -1, $sImageFile, $iLeft = 0, $iTop = 0, $iRight = @DesktopWidth, $iBottom = @DesktopHeight, $iTolerance = 10, $iTransparent = -1, $iMultiResults = 1, $iCenterPos = 1, $iReturnDebug = 1, $fMinScale = 1.0, $fMaxScale = 1.0, $fScaleStep = 0.1, $iFindAllOccurrences = 0)

 

I realize we can use a delimiter | to search for multiple images but there are situations where a user would want to break images into categories and run different tolerance for each set, as well as focus on a particular area of the screen for each set.

We can take a snap shot using GDI+ through AutoIt as such:

Func _Img_TakeSnapshot($_iCanvasLeft=0,$_iCanvasTop=0,$_iCanvasWidth=@DesktopWidth,$_iCanvasHeight=@DesktopHeight)

    ; Clean up resources
    _GDIPlus_BitmapDispose($g_myImg_hImage)
    _WinAPI_DeleteObject($g_myImg_hMatWinCapture)

    ; Screen grab an area (should ideally be full screen)
    ; Use _Img_ScanSnapshotArea to scan

    ; Capture entire desktop or selected area
    $g_myImg_hMatWinCapture = _ScreenCapture_Capture("", $_iCanvasLeft, $_iCanvasTop, $_iCanvasWidth, $_iCanvasHeight, False)

    ; Create a Bitmap object from a bitmap handle (Using previously taken SnapShot)
    $g_myImg_hImage = _GDIPlus_BitmapCreateFromHBITMAP($g_myImg_hMatWinCapture)

    ; Save last scanned box dimensions
    $g_myImg_aAreaLastScan[0] = $_iCanvasLeft                           ; X Left Upper
    $g_myImg_aAreaLastScan[1] = $_iCanvasTop                            ; Y Left Upper
    $g_myImg_aAreaLastScan[2] = $_iCanvasLeft + $_iCanvasWidth          ; X Right Lower
    $g_myImg_aAreaLastScan[3] = $_iCanvasTop + $_iCanvasHeight          ; Y Right Lower
    $g_myImg_aAreaLastScan[4] = $_iCanvasWidth                          ; Box Width
    $g_myImg_aAreaLastScan[5] = $_iCanvasHeight                         ; Box Height

    ; Debug
    ;If $g_myImg_bDebugConsole = True Then ConsoleWrite("+ Snapshot:: (" & $_iCanvasLeft & ", " & $_iCanvasTop & ", " & $_iCanvasWidth & ", " & $_iCanvasHeight & ")" & @CRLF)

EndFunc

Or perhaps that can be handled by the dll. Those are just my thoughts, I'm not an expert a this.

Thank you for putting together this amazing UDF!

tubaba

Posted

First of all, thank you for providing a great user experience. However, I do have an issue to bring up. It seems that your DLL automatically corrects parts that exceed the screen, confining them within the frame of 0,0,@DesktopWidth,@DesktopHeight. But there are many application scenarios with multiple screens, which means that screen coordinates should not be limited to the range of the first screen. In earlier versions, I found that the coordinate correction was done in the UDF.

1.thumb.png.e0948e0023942a08c11910f8814928f1.png

tubaba

Posted

 

Get information of all screens

Global $__MonitorList[1][5], $__MonitorListBase[1][5]
$__MonitorListBase[0][0] = 0



_GetMonitors()



Func  _GetMonitors()
    $__MonitorList = $__MonitorListBase
    Local $handle = DllCallbackRegister("_MonitorEnumProc", "int", "hwnd;hwnd;ptr;lparam")
    DllCall("user32.dll", "int", "EnumDisplayMonitors", "hwnd", 0, "ptr", 0, "ptr", DllCallbackGetPtr($handle), "lparam", 0)
    DllCallbackFree($handle)
EndFunc   ;==>_GetMonitors

Func _MonitorEnumProc($hMonitor, $hDC, $lRect, $lParam)
    Local $Rect = DllStructCreate("int left;int top;int right;int bottom", $lRect)
    $__MonitorList[0][0] += 1
    ReDim $__MonitorList[$__MonitorList[0][0] + 1][5]
    $__MonitorList[$__MonitorList[0][0]][0] = $hMonitor
    $__MonitorList[$__MonitorList[0][0]][1] = DllStructGetData($Rect, "left")
    $__MonitorList[$__MonitorList[0][0]][2] = DllStructGetData($Rect, "top")
    $__MonitorList[$__MonitorList[0][0]][3] = DllStructGetData($Rect, "right")
    $__MonitorList[$__MonitorList[0][0]][4] = DllStructGetData($Rect, "bottom")
    Return 1 ; Return 1 to continue enumeration
EndFunc   ;==>_MonitorEnumProc

 

×
×
  • Create New...