Jump to content

I have problem using DllCall to call ImageRecognition.dll


Go to solution Solved by Danyfirex,

Recommended Posts

Hello everyone,


I found a very nice library: Image Recognition Library (https://www.3delite.hu/Object%20Pascal%20Developer%20Resources/download.html#irl)

Features:

Quote

Image Recognition Library is a component for use in Win32 and Win64 (Windows XP/Vista/7/8/10) software with a purpose to provide image recognition functionality, that is compare two pictures if they are the same or similar or find a smaller picture in a bigger picture with difference tolerance.
Requirements: any dev. environment that supports the stdcall calling convention.

- Exact, relative ARGB and AHSL, rotate and 'stretch'" search modes with alpha channel support
- Pre-process images with 6 different resamplers
- Multi-threaded processing
- Multiple match support
- OpenCL accelerated search option
- A fully multi-threaded loading queue functionality for batch loading of pictures
- A fully multi-threaded search queue functionality for batch processing with support of parallel CPU-OpenCL processing a the same time
- Full unicode support
- Platforms: Win32 and Win64
- Delphi and C++ API included


And I am writing code to call this library.
The simplest example of how to use this library consists of 3 steps:
1/ ImageRecognition_CreateObject
2/ ImageRecognition_Compare
3/ ImageRecognition_FreeObject

Step 1 and step 3 I have done, but in step 2, when running the program, the program encounters an error and crashes.
I have tried many ways but still can't fix it. Now I ask everyone to help me fix this error.

Here is the code I did:

#AutoIt3Wrapper_UseX64 = no
#include <Array.au3>
#include <WinAPISys.au3>
#include <WinAPIDiag.au3>

Global $__gh_DLL_IRL = -1

Enum _
        $IR_OK, _
        $IR_ERROR_UNKNOWN, _
        $IR_ERROR_NOT_ENOUGH_MEMORY, _
        $IR_ERROR_LOADING, _
        $IR_ERROR_OBJECT, _
        $IR_ERROR_DIMENSIONS, _
        $IR_ERROR_NOT_AVAILABLE, _
        $IR_ERROR_OCL_NOT_AVAILABLE, _
        $IR_ERROR_OCL_NOT_INITIALIZED, _
        $IR_ERROR_OCL_NO_SEARCH_IN_SET, _
        $IR_ERROR_OCL_NO_SEARCH_FOR_SET, _
        $IR_ERROR_NO_SEARCH_QUEUE_SET, _
        $IR_ERROR_OCL_SET_SEARCH_IN, _
        $IR_ERROR_OCL_SET_SEARCH_FOR, _
        $IR_ERROR_NO_LOAD_QUEUE_SET

#Status types
Enum $IR_STATUS_PROGRESS

#Compare type modes
Enum _
        $IR_COMPARE_TYPE_EXACT, _ ;Perform an exact search with an allowed pixel difference count. This requires that the search for bitmap is pixel identical with the search in bitmap, although you can set a "DifferenceTolerance" value to allow a concrete pixel count to differ. Exact mode is the fastest of the available modes.
        $IR_COMPARE_TYPE_RELATIVE_ARGB, _ ;Perform a search with an allowed pixel difference in Alpha-Red-Green-Blue color space. This mode is usefull when searching a similar picture in another picture. Setting a "DifferenceTolerance" value will allow a concrete pixel count to differ completly. This mode is slower than exact mode but faster than AHSL mode.
        $IR_COMPARE_TYPE_RELATIVE_AHSL, _ ;Perform a search with an allowed pixel difference in Alpha-Hue-Saturation-Lightness color space. AHSL component values range from 0 to 1. This mode is usefull when searching a similar picture in another picture but AHSL mode is required. Setting a "DifferenceTolerance" value will allow a concrete pixel count to differ completly.
        $IR_COMPARE_TYPE_RELATIVE_ARGB_ALPHA, _ ;Same as ARGB but the search for bitmap's pixels transparency value (alpha chanel) is considered when searching. Usefull when searching for a non-rectangular image, for example to find a logo put on a picture with transparency. Recommended that the search for bitmap is a PNG image with transparency. Slower then ARGB mode.
        $IR_COMPARE_TYPE_RELATIVE_AHSL_ALPHA ;Same as AHSL but the search for bitmap's pixels transparency value (alpha chanel) is considered when searching. Usefull when searching for a non-rectangular image, for example to find a logo put on a picture with transparency but AHSL mode is required. Recommended that the search for bitmap is a PNG image with transparency. This is the slowest of the available modes.

#Queue modes
Enum _
        $IR_SEARCH_MODE_CPU, _
        $IR_SEARCH_MODE_OPENCL, _
        $IR_SEARCH_MODE_ANY

#Queue threads
Enum _
        $IR_THREAD_PRIORITY_IDLE, _
        $IR_THREAD_PRIORITY_LOWEST, _
        $IR_THREAD_PRIORITY_LOWER, _
        $IR_THREAD_PRIORITY_NORMAL, _
        $IR_THREAD_PRIORITY_HIGHER, _
        $IR_THREAD_PRIORITY_HIGHEST, _
        $IR_THREAD_PRIORITY_TIMECRITICAL

#Pre-process types
Enum _
        $IR_PROCESS_TYPE_NONE, _
        $IR_PROCESS_TYPE_RESAMPLE

#Pre-process resample modes
Enum _
        $IR_RESAMPLER_BOX, _    ;   0 // Box, pulse, Fourier window, 1st order (constant) b-spline
        $IR_RESAMPLER_BICUBIC, _ ;  1 // Mitchell & Netravali's two-param cubic filter
        $IR_RESAMPLER_BILINEAR, _    ;2 // Bilinear filter
        $IR_RESAMPLER_BSPLINE, _ ;  3 // 4th order (cubic) b-spline
        $IR_RESAMPLER_CATMULLROM, _ ;   4 // Catmull-Rom spline, Overhauser spline
        $IR_RESAMPLER_LANCZOS3 ;    5 // Lanczos3 filter

#type LongBool

Global Const $sIRDimensions = _
        'long Width;' & _
        'long Height'

Global Const $sGUID = _
        'byte GUID[16]'

Global Const $sIRObject = _
        'long Status;' & _
        'ptr lpszFileName;' & _
        'ptr pImageObject;' & _
        'struct ID;' & $sGUID & ';endstruct;' & _
        'struct OriginalDimensions;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'struct ProcessedDimensions;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'ptr User'

Global Const $sIRCreateObjectParameters = _
        'long ProcessType;' & _
        'struct Dimensions;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'long Resampler'

Global Const $sIRProcessParameters = _
        'long CompareType;' & _
        'byte ARGBATolerance;' & _
        'byte ARGBRTolerance;' & _
        'byte ARGBGTolerance;' & _
        'byte ARGBBTolerance;' & _
        'double AHSLATolerance;' & _
        'double AHSLHTolerance;' & _
        'double AHSLSTolerance;' & _
        'double AHSLLTolerance;' & _
        'long DifferenceTolerance;' & _
        'long StretchCompare;' & _            ;//* Stretch compare is experimental and very slow!
        'struct MinimalStretchSize;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'struct MaximalStretchSize;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'long StretchResampler;' & _
        'long MultipleMatches;' & _
        'long MultiThreadedProcessing;' & _
        'long MultiThreadCount;' & _            ;//* 0 means all available CPU cores
        'ptr StatusCallback;' & _ ; TIRStatusCallback *StatusCallback;
        'long RotateSearch;' & _
        'float RotateSearchStartDegrees;' & _
        'float RotateSearchEndDegrees;' & _
        'float RotateSearchStepDegrees;' & _
        'long SearchSorroundingPixels'

Global Const $sIRPoint = _
        'dword X;' & _
        'dword Y'

Global Const $sIRResultMatches = _
        'struct Position;' & $sIRPoint & ';endstruct;' & _ ;TPoint
        'struct Dimensions;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'float Angle;' & _
        'long Difference;' & _
        'float MatchPercentage'

Global Const $sIRResult = _
        'long Success;' & _
        'long MatchCount;' & _
        'ptr pMatches'


;OpenCL accelerated search right now is supported with IR_COMPARE_TYPE_EXACT and IR_COMPARE_TYPE_RELATIVE_ARGB_ALPHA.
;Rotate search is supported with only IR_COMPARE_TYPE_RELATIVE_ARGB_ALPHA.





#Region 3 Steps

    ImageRecognition_StartUp()

    Local $TIRObject_SearchIn = ImageRecognition_CreateObject(@ScriptDir & '\SearchIn.bmp')
    ;_WinAPI_DisplayStruct($TIRObject_SearchIn, $sIRObject)
    Local $TIRObject_SearchFor = ImageRecognition_CreateObject(@ScriptDir & '\SearchFor.bmp')
    ;_WinAPI_DisplayStruct($TIRObject_SearchFor, $sIRObject)





    #Region ImageRecognition_Compare
        #typedef void (CALLBACK TIRStatusCallback)(double Progress, int StatusType, void *User);
        Local $sIRStatusCallback = 'double;long;ptr*'
        Local $hIRStatusCallback = DllCallbackRegister('IRStatusCallback', 'none', $sIRStatusCallback)
        Volatile Func IRStatusCallback($Progress, $StatusType, $User)
        EndFunc

        Local $TIRProcessParameters = DllStructCreate($sIRProcessParameters)
        $TIRProcessParameters.CompareType = $IR_COMPARE_TYPE_EXACT
        $TIRProcessParameters.MultipleMatches = True
        $TolerancePercent = 1 ; 0 - 99
        $TIRProcessParameters.DifferenceTolerance = Int($TIRObject_SearchFor.Width * $TIRObject_SearchFor.Height * ($TolerancePercent / 100))
        $TIRProcessParameters.MultiThreadedProcessing = 0
        ;$TIRProcessParameters.StatusCallback = DllCallbackGetPtr($hIRStatusCallback)
        ;_WinAPI_DisplayStruct($TIRProcessParameters, $sIRProcessParameters)

        Local $TIRResultMatches = DllStructCreate($sIRResultMatches)
        ;_WinAPI_DisplayStruct($TIRResultMatches, $sIRResultMatches)

        Local $TIRResult = DllStructCreate($sIRResult)
        $TIRResult.pMatches = DllStructGetPtr($TIRResultMatches)
        ;_WinAPI_DisplayStruct($TIRResult, $sIRResult)

        #typedef int(IRLIBCALL *t_ImageRecognition_Compare)(TIRObject IRObjectSearchIn, TIRObject IRObjectSearchFor, TIRProcessParameters Parameters, TIRResult *CompareResult, void *User);
        Local $User = 0
        $ret = DllCall($__gh_DLL_IRL, 'long', 'ImageRecognition_Compare', 'struct', $TIRObject_SearchIn, 'struct', $TIRObject_SearchFor, 'struct', $TIRProcessParameters, 'struct*', $TIRResult, 'ptr', $User)
        _ArrayDisplay($ret)
    #EndRegion





    ImageRecognition_FreeObject($TIRObject_SearchFor)
    ImageRecognition_FreeObject($TIRObject_SearchIn)
    
#EndRegion



















Func ImageRecognition_StartUp($sDllPath = 'ImageRecognition.dll')
    If Not FileExists($sDllPath) Then Exit MsgBox(16, 'Fatal Error', 'Not found ' & $sDllPath)
    $__gh_DLL_IRL = DllOpen($sDllPath)
    If @error Or $__gh_DLL_IRL = -1 Then Exit MsgBox(16, 'Fatal Error', 'Could not open ' & $sDllPath)
    OnAutoItExitRegister(ImageRecognition_Shutdown)
EndFunc
Func ImageRecognition_Shutdown()
    DllClose($__gh_DLL_IRL)
EndFunc


Func ImageRecognition_CreateObject($FilePath_or_Binary_or_hBitmap, $ProcessType = Default, $Resampler = Default, $Dimensions_Width = Default, $Dimensions_Height = Default)
    #Set Parameters
    Local $TIRCreateObjectParameters = DllStructCreate($sIRCreateObjectParameters)
    ;Pre-process the images: It can be usefull to resize the images when loading for faster processing and also usefull if you want to compare two images that differ in dimensions. When acquireing the picture object specify the "ProcessType" parameter of the "TIRCreateObjectParameters" structure to "IR_PROCESS_TYPE_RESAMPLE". And specify a resampler ("Resampler" variable). Resampling the images is performed by FreeImage.dll.
    If $ProcessType = Default Then $ProcessType = $IR_PROCESS_TYPE_NONE
    If $Resampler = Default Then $Resampler = $IR_RESAMPLER_BOX
    $TIRCreateObjectParameters.ProcessType = $ProcessType
    $TIRCreateObjectParameters.Resampler = $Resampler
    $TIRCreateObjectParameters.Width = $Dimensions_Width
    $TIRCreateObjectParameters.Height = $Dimensions_Height
    ;_WinAPI_DisplayStruct($TIRCreateObjectParameters, $sIRCreateObjectParameters)
    
    #Tạo Struct nhận Object
    Local $TIRObject = DllStructCreate($sIRObject)
    ;Local $tUser = DllStructCreate('byte[100]')
    ;$TIRObject.User = DllStructGetPtr($tUser)
    
    #Tạo Object
    If IsBinary($FilePath_or_Binary_or_hBitmap) Then
        Local $lImage = BinaryLen($FilePath_or_Binary_or_hBitmap)
        Local $tImage = DllStructCreate('byte[' & $lImage & ']')
        DllStructSetData($tImage, 1, $FilePath_or_Binary_or_hBitmap)
        ;typedef int(IRLIBCALL *t_ImageRecognition_CreateObjectFromMemory)(void *Address, QWORD DataSize, TIRObject *IRObject, TIRCreateObjectParameters Parameters);
        Local $ret = DllCall($__gh_DLL_IRL, 'long', 'ImageRecognition_CreateObjectFromMemory', 'ptr', DllStructGetPtr($tImage), 'uint64', $lImage, 'struct*', $TIRObject, 'struct', $TIRCreateObjectParameters)
    ElseIf IsPtr($FilePath_or_Binary_or_hBitmap) Then
        ;typedef int(IRLIBCALL *t_ImageRecognition_CreateObjectFromBitmapHandle)(HBITMAP BitmapHandle, TIRObject *IRObject, TIRCreateObjectParameters Parameters);
        Local $ret = DllCall($__gh_DLL_IRL, 'long', 'ImageRecognition_CreateObjectFromBitmapHandle', 'handle', $FilePath_or_Binary_or_hBitmap, 'struct*', $TIRObject, 'struct', $TIRCreateObjectParameters)
    Else
        ;typedef int(IRLIBCALL *t_ImageRecognition_CreateObject)(LPWSTR FileName, TIRObject *IRObject, TIRCreateObjectParameters Parameters);
        Local $ret = DllCall($__gh_DLL_IRL, 'long', 'ImageRecognition_CreateObject', 'wstr', $FilePath_or_Binary_or_hBitmap, 'struct*', $TIRObject, 'struct', $TIRCreateObjectParameters)
    EndIf
    Local $err = @error
    
    #Clear Params
    _WinAPI_ZeroMemory($TIRCreateObjectParameters, DllStructGetSize($TIRCreateObjectParameters))
    
    #Trả kết quả
    If $err Or $ret[0] <> $IR_OK Then Return SetError(1)
    If Not @Compiled Then
        ConsoleWrite(_WinAPI_GetString($TIRObject.lpszFileName) & ' / ImageObj=' & $TIRObject.pImageObject & ' / W=' & $TIRObject.Width & ' / H=' & $TIRObject.Height & @CRLF)
    EndIf
    Return $TIRObject
EndFunc


Func ImageRecognition_FreeObject(ByRef $TIRObject)
    If Not IsDllStruct($TIRObject) Then Return SetError(1)
    ;typedef int(IRLIBCALL *t_ImageRecognition_FreeObject)(TIRObject *IRObject);
    Local $ret = DllCall($__gh_DLL_IRL, 'long', 'ImageRecognition_FreeObject', 'struct*', $TIRObject)
    Local $err = @error
    _WinAPI_ZeroMemory($TIRObject, DllStructGetSize($TIRObject))
    If Not @Compiled Then
        ConsoleWrite('Free: ' & _WinAPI_GetString($TIRObject.lpszFileName) & ' / ImageObj=' & $TIRObject.pImageObject & ' / W=' & $TIRObject.Width & ' / H=' & $TIRObject.Height & @CRLF)
    EndIf
    If $err Or $ret[0] <> $IR_OK Then Return SetError(2)
EndFunc

 

I would like to thank everyone.😘

 

- Exe tutorials with source code of the Image Recognition Library: https://www.3delite.hu/Object%20Pascal%20Developer%20Resources/download.html#irl

- The attachment (my work) includes: my au3 script, dll, image

 

Test.zip

Edited by NewCommer
Link to comment
Share on other sites

  • Solution

Hello.  I've checked and it seems to be AutoIt DllCall is failing maybe It's a bug( or it was not designed to handle structure as C++ Compilers).

Here is a way to solve the issue. it supports x86 only.

#AutoIt3Wrapper_UseX64 = no
#include <Array.au3>
#include <WinAPISys.au3>
#include <WinAPIDiag.au3>
#include <Memory.au3>
#include <GDIPlus.au3>

Global $__gh_DLL_IRL = -1

Enum _
        $IR_OK, _
        $IR_ERROR_UNKNOWN, _
        $IR_ERROR_NOT_ENOUGH_MEMORY, _
        $IR_ERROR_LOADING, _
        $IR_ERROR_OBJECT, _
        $IR_ERROR_DIMENSIONS, _
        $IR_ERROR_NOT_AVAILABLE, _
        $IR_ERROR_OCL_NOT_AVAILABLE, _
        $IR_ERROR_OCL_NOT_INITIALIZED, _
        $IR_ERROR_OCL_NO_SEARCH_IN_SET, _
        $IR_ERROR_OCL_NO_SEARCH_FOR_SET, _
        $IR_ERROR_NO_SEARCH_QUEUE_SET, _
        $IR_ERROR_OCL_SET_SEARCH_IN, _
        $IR_ERROR_OCL_SET_SEARCH_FOR, _
        $IR_ERROR_NO_LOAD_QUEUE_SET

#Status types
Enum $IR_STATUS_PROGRESS

#Compare type modes
Enum _
        $IR_COMPARE_TYPE_EXACT, _ ;Perform an exact search with an allowed pixel difference count. This requires that the search for bitmap is pixel identical with the search in bitmap, although you can set a "DifferenceTolerance" value to allow a concrete pixel count to differ. Exact mode is the fastest of the available modes.
        $IR_COMPARE_TYPE_RELATIVE_ARGB, _ ;Perform a search with an allowed pixel difference in Alpha-Red-Green-Blue color space. This mode is usefull when searching a similar picture in another picture. Setting a "DifferenceTolerance" value will allow a concrete pixel count to differ completly. This mode is slower than exact mode but faster than AHSL mode.
        $IR_COMPARE_TYPE_RELATIVE_AHSL, _ ;Perform a search with an allowed pixel difference in Alpha-Hue-Saturation-Lightness color space. AHSL component values range from 0 to 1. This mode is usefull when searching a similar picture in another picture but AHSL mode is required. Setting a "DifferenceTolerance" value will allow a concrete pixel count to differ completly.
        $IR_COMPARE_TYPE_RELATIVE_ARGB_ALPHA, _ ;Same as ARGB but the search for bitmap's pixels transparency value (alpha chanel) is considered when searching. Usefull when searching for a non-rectangular image, for example to find a logo put on a picture with transparency. Recommended that the search for bitmap is a PNG image with transparency. Slower then ARGB mode.
        $IR_COMPARE_TYPE_RELATIVE_AHSL_ALPHA ;Same as AHSL but the search for bitmap's pixels transparency value (alpha chanel) is considered when searching. Usefull when searching for a non-rectangular image, for example to find a logo put on a picture with transparency but AHSL mode is required. Recommended that the search for bitmap is a PNG image with transparency. This is the slowest of the available modes.

#Queue modes
Enum _
        $IR_SEARCH_MODE_CPU, _
        $IR_SEARCH_MODE_OPENCL, _
        $IR_SEARCH_MODE_ANY

#Queue threads
Enum _
        $IR_THREAD_PRIORITY_IDLE, _
        $IR_THREAD_PRIORITY_LOWEST, _
        $IR_THREAD_PRIORITY_LOWER, _
        $IR_THREAD_PRIORITY_NORMAL, _
        $IR_THREAD_PRIORITY_HIGHER, _
        $IR_THREAD_PRIORITY_HIGHEST, _
        $IR_THREAD_PRIORITY_TIMECRITICAL

#Pre-process types
Enum _
        $IR_PROCESS_TYPE_NONE, _
        $IR_PROCESS_TYPE_RESAMPLE

#Pre-process resample modes
Enum _
        $IR_RESAMPLER_BOX, _    ;   0 // Box, pulse, Fourier window, 1st order (constant) b-spline
        $IR_RESAMPLER_BICUBIC, _ ;  1 // Mitchell & Netravali's two-param cubic filter
        $IR_RESAMPLER_BILINEAR, _    ;2 // Bilinear filter
        $IR_RESAMPLER_BSPLINE, _ ;  3 // 4th order (cubic) b-spline
        $IR_RESAMPLER_CATMULLROM, _ ;   4 // Catmull-Rom spline, Overhauser spline
        $IR_RESAMPLER_LANCZOS3 ;    5 // Lanczos3 filter

#type LongBool

Global Const $sIRDimensions = _
        'long Width;' & _
        'long Height'

Global Const $sGUID = _
        'byte GUID[16]'

Global Const $sIRObject = _
        'long Status;' & _
        'ptr lpszFileName;' & _
        'ptr pImageObject;' & _
        'struct ID;' & $sGUID & ';endstruct;' & _
        'struct OriginalDimensions;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'struct ProcessedDimensions;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'ptr User'

Global Const $sIRCreateObjectParameters = _
        'long ProcessType;' & _
        'struct Dimensions;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'long Resampler'

Global Const $sIRProcessParameters = _
        'long CompareType;' & _
        'byte ARGBATolerance;' & _
        'byte ARGBRTolerance;' & _
        'byte ARGBGTolerance;' & _
        'byte ARGBBTolerance;' & _
        'double AHSLATolerance;' & _
        'double AHSLHTolerance;' & _
        'double AHSLSTolerance;' & _
        'double AHSLLTolerance;' & _
        'long DifferenceTolerance;' & _
        'long StretchCompare;' & _            ;//* Stretch compare is experimental and very slow!
        'struct MinimalStretchSize;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'struct MaximalStretchSize;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'long StretchResampler;' & _
        'long MultipleMatches;' & _
        'long MultiThreadedProcessing;' & _
        'long MultiThreadCount;' & _            ;//* 0 means all available CPU cores
        'ptr StatusCallback;' & _ ; TIRStatusCallback *StatusCallback;
        'long RotateSearch;' & _
        'float RotateSearchStartDegrees;' & _
        'float RotateSearchEndDegrees;' & _
        'float RotateSearchStepDegrees;' & _
        'long SearchSorroundingPixels'

Global Const $sIRPoint = _
        'dword X;' & _
        'dword Y'

Global Const $sIRResultMatches = _
        'struct Position;' & $sIRPoint & ';endstruct;' & _ ;TPoint
        'struct Dimensions;' & $sIRDimensions & ';endstruct;' & _ ;TIRDimensions
        'float Angle;' & _
        'long Difference;' & _
        'float MatchPercentage'

Global Const $sIRResult = _
        'long Success;' & _
        'long MatchCount;' & _
        'ptr pMatches'


;OpenCL accelerated search right now is supported with IR_COMPARE_TYPE_EXACT and IR_COMPARE_TYPE_RELATIVE_ARGB_ALPHA.
;Rotate search is supported with only IR_COMPARE_TYPE_RELATIVE_ARGB_ALPHA.





#Region 3 Steps

ImageRecognition_StartUp()

Local $TIRObject_SearchIn = ImageRecognition_CreateObject(@ScriptDir & '\SearchIn.bmp')
;_WinAPI_DisplayStruct($TIRObject_SearchIn, $sIRObject)
Local $TIRObject_SearchFor = ImageRecognition_CreateObject(@ScriptDir & '\SearchFor.bmp')
;_WinAPI_DisplayStruct($TIRObject_SearchFor, $sIRObject)





#Region ImageRecognition_Compare
#typedef void (CALLBACK TIRStatusCallback)(double Progress, int StatusType, void *User);
Local $sIRStatusCallback = 'double;long;ptr*'
Local $hIRStatusCallback = DllCallbackRegister('IRStatusCallback', 'none', $sIRStatusCallback)
Volatile Func IRStatusCallback($Progress, $StatusType, $User)
    ConsoleWrite("IRStatusCallback Progress: " & $Progress & @CRLF)
EndFunc   ;==>IRStatusCallback

Local $TIRProcessParameters = DllStructCreate($sIRProcessParameters)
$TIRProcessParameters.CompareType = $IR_COMPARE_TYPE_EXACT
$TIRProcessParameters.MultipleMatches = True
$TolerancePercent = 1         ; 0 - 99
$TIRProcessParameters.DifferenceTolerance = Int($TIRObject_SearchFor.Width * $TIRObject_SearchFor.Height * ($TolerancePercent / 100))
$TIRProcessParameters.MultiThreadedProcessing = 0
;~ $TIRProcessParameters.StatusCallback = DllCallbackGetPtr($hIRStatusCallback)
;_WinAPI_DisplayStruct($TIRProcessParameters, $sIRProcessParameters)

;~ Local $TIRResultMatches = DllStructCreate($sIRResultMatches)
;_WinAPI_DisplayStruct($TIRResultMatches, $sIRResultMatches)

Local $TIRResult = DllStructCreate($sIRResult)
;~ $TIRResult.pMatches = DllStructGetPtr($TIRResultMatches)
;_WinAPI_DisplayStruct($TIRResult, $sIRResult)



#typedef int(IRLIBCALL *t_ImageRecognition_Compare)(TIRObject IRObjectSearchIn, TIRObject IRObjectSearchFor, TIRProcessParameters Parameters, TIRResult *CompareResult, void *User);
Local $User = 0
# Danyfirex - This should work but it seems to be AutoIt DllCall doesn't handle big structures O_o
;~ $ret = DllCall($__gh_DLL_IRL, 'long', 'ImageRecognition_Compare', 'struct', $TIRObject_SearchIn, 'struct', $TIRObject_SearchFor, 'struct', $TIRProcessParameters, 'struct*', $TIRResult, 'ptr', $User)
;~ _ArrayDisplay($ret)
#EndRegion ImageRecognition_Compare

#So here is a ugly fix. (Just for x86)
Local $hLibraryRec = _WinAPI_LoadLibrary("ImageRecognition.dll")
Local $pFunctionRec = _WinAPI_GetProcAddress($hLibraryRec, "ImageRecognition_Compare")

Local $sByteCode = "0x5589E56A00B8" & _SwapEndian(DllStructGetPtr($TIRResult)) & _
        "5083EC68B91A0000008D35" & _SwapEndian(DllStructGetPtr($TIRProcessParameters)) & _
        "89E7F3A583EC30B90C0000008D35" & _SwapEndian(DllStructGetPtr($TIRObject_SearchFor)) & _
        "89E7F3A583EC30B90C0000008D35" & _SwapEndian(DllStructGetPtr($TIRObject_SearchIn)) & _
        "89E7F3A5B8" & _SwapEndian($pFunctionRec) & _
        "FFD089EC5DC3"

Local $iRemoteCodeSize = BinaryLen($sByteCode)
Local $pRemoteCode = _MemVirtualAlloc(0, $iRemoteCodeSize, $MEM_COMMIT, $PAGE_EXECUTE_READWRITE)
Local $tCodeBuffer = DllStructCreate("byte[" & $iRemoteCodeSize & "]", $pRemoteCode)
DllStructSetData($tCodeBuffer, 1, $sByteCode)
DllCallAddress("int", $pRemoteCode)
_MemVirtualFree($pRemoteCode, $iRemoteCodeSize, $MEM_DECOMMIT)
Local $iMatchCount = $TIRResult.MatchCount
ConsoleWrite("$iMatchCount: " & $iMatchCount & @CRLF)


_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\SearchIn.bmp")
Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
Local $hBrush = _GDIPlus_BrushCreateSolid(0x40FFFF00)
Local $hPen = _GDIPlus_PenCreate(0xFFFF0000)

Local $TIRResultMatches = 0
Local $pFirstMatch = $TIRResult.pMatches
Local $pIncrement = DllStructGetSize(DllStructCreate($sIRResultMatches))
For $i = 0 To $iMatchCount - 1
    $TIRResultMatches = DllStructCreate($sIRResultMatches, $pFirstMatch + ($i * $pIncrement))
    ConsoleWrite("X: " & $TIRResultMatches.X & ", Y: " & $TIRResultMatches.Y & @TAB & _
            "Width: " & $TIRResultMatches.Width & ", Height: " & $TIRResultMatches.Height & @TAB & "MatchPercentage: " & $TIRResultMatches.MatchPercentage & @CRLF)
    If $i = 1 Then _GDIPlus_GraphicsFillRect($hGraphics, $TIRResultMatches.X, $TIRResultMatches.Y, $TIRResultMatches.Width, $TIRResultMatches.Height, $hBrush)
    _GDIPlus_GraphicsDrawRect($hGraphics, $TIRResultMatches.X, $TIRResultMatches.Y, $TIRResultMatches.Width, $TIRResultMatches.Height, $hPen)
    $TIRResultMatches = 0
Next

_GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\SearchIn-Result.bmp")
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_PenDispose($hPen)
_GDIPlus_Shutdown()
ShellExecute(@ScriptDir & "\SearchIn-Result.bmp")

ImageRecognition_FreeObject($TIRObject_SearchFor)
ImageRecognition_FreeObject($TIRObject_SearchIn)

#EndRegion 3 Steps

Func _SwapEndian($hex)
    Return Hex(Binary($hex))
EndFunc   ;==>_SwapEndian


Func ImageRecognition_StartUp($sDllPath = 'ImageRecognition.dll')
    If Not FileExists($sDllPath) Then Exit MsgBox(16, 'Fatal Error', 'Not found ' & $sDllPath)
    $__gh_DLL_IRL = DllOpen($sDllPath)
    If @error Or $__gh_DLL_IRL = -1 Then Exit MsgBox(16, 'Fatal Error', 'Could not open ' & $sDllPath)
    OnAutoItExitRegister(ImageRecognition_Shutdown)
EndFunc   ;==>ImageRecognition_StartUp
Func ImageRecognition_Shutdown()
    DllClose($__gh_DLL_IRL)
EndFunc   ;==>ImageRecognition_Shutdown


Func ImageRecognition_CreateObject($FilePath_or_Binary_or_hBitmap, $ProcessType = Default, $Resampler = Default, $Dimensions_Width = Default, $Dimensions_Height = Default)
    #Set Parameters
    Local $TIRCreateObjectParameters = DllStructCreate($sIRCreateObjectParameters)
    ;Pre-process the images: It can be usefull to resize the images when loading for faster processing and also usefull if you want to compare two images that differ in dimensions. When acquireing the picture object specify the "ProcessType" parameter of the "TIRCreateObjectParameters" structure to "IR_PROCESS_TYPE_RESAMPLE". And specify a resampler ("Resampler" variable). Resampling the images is performed by FreeImage.dll.
    If $ProcessType = Default Then $ProcessType = $IR_PROCESS_TYPE_NONE
    If $Resampler = Default Then $Resampler = $IR_RESAMPLER_BOX
    $TIRCreateObjectParameters.ProcessType = $ProcessType
    $TIRCreateObjectParameters.Resampler = $Resampler
    $TIRCreateObjectParameters.Width = $Dimensions_Width
    $TIRCreateObjectParameters.Height = $Dimensions_Height
    ;_WinAPI_DisplayStruct($TIRCreateObjectParameters, $sIRCreateObjectParameters)

    #T?o Struct nh?n Object
    Local $TIRObject = DllStructCreate($sIRObject)
    ;Local $tUser = DllStructCreate('byte[100]')
    ;$TIRObject.User = DllStructGetPtr($tUser)

    #T?o Object
    If IsBinary($FilePath_or_Binary_or_hBitmap) Then
        Local $lImage = BinaryLen($FilePath_or_Binary_or_hBitmap)
        Local $tImage = DllStructCreate('byte[' & $lImage & ']')
        DllStructSetData($tImage, 1, $FilePath_or_Binary_or_hBitmap)
        ;typedef int(IRLIBCALL *t_ImageRecognition_CreateObjectFromMemory)(void *Address, QWORD DataSize, TIRObject *IRObject, TIRCreateObjectParameters Parameters);
        Local $ret = DllCall($__gh_DLL_IRL, 'long', 'ImageRecognition_CreateObjectFromMemory', 'ptr', DllStructGetPtr($tImage), 'uint64', $lImage, 'struct*', $TIRObject, 'struct', $TIRCreateObjectParameters)
    ElseIf IsPtr($FilePath_or_Binary_or_hBitmap) Then
        ;typedef int(IRLIBCALL *t_ImageRecognition_CreateObjectFromBitmapHandle)(HBITMAP BitmapHandle, TIRObject *IRObject, TIRCreateObjectParameters Parameters);
        Local $ret = DllCall($__gh_DLL_IRL, 'long', 'ImageRecognition_CreateObjectFromBitmapHandle', 'handle', $FilePath_or_Binary_or_hBitmap, 'struct*', $TIRObject, 'struct', $TIRCreateObjectParameters)
    Else
        ;typedef int(IRLIBCALL *t_ImageRecognition_CreateObject)(LPWSTR FileName, TIRObject *IRObject, TIRCreateObjectParameters Parameters);
        Local $ret = DllCall($__gh_DLL_IRL, 'long', 'ImageRecognition_CreateObject', 'wstr', $FilePath_or_Binary_or_hBitmap, 'struct*', $TIRObject, 'struct', $TIRCreateObjectParameters)
    EndIf
    Local $err = @error

    #Clear Params
    _WinAPI_ZeroMemory($TIRCreateObjectParameters, DllStructGetSize($TIRCreateObjectParameters))

    #Tr? k?t qu?
    If $err Or $ret[0] <> $IR_OK Then Return SetError(1)
    If Not @Compiled Then
        ConsoleWrite(_WinAPI_GetString($TIRObject.lpszFileName) & ' / ImageObj=' & $TIRObject.pImageObject & ' / W=' & $TIRObject.Width & ' / H=' & $TIRObject.Height & @CRLF)
    EndIf
    Return $TIRObject
EndFunc   ;==>ImageRecognition_CreateObject

Func ImageRecognition_FreeObject(ByRef $TIRObject)
    If Not IsDllStruct($TIRObject) Then Return SetError(1)
    ;typedef int(IRLIBCALL *t_ImageRecognition_FreeObject)(TIRObject *IRObject);
    Local $ret = DllCall($__gh_DLL_IRL, 'long', 'ImageRecognition_FreeObject', 'struct*', $TIRObject)
    Local $err = @error
    _WinAPI_ZeroMemory($TIRObject, DllStructGetSize($TIRObject))
    If Not @Compiled Then
        ConsoleWrite('Free: ' & _WinAPI_GetString($TIRObject.lpszFileName) & ' / ImageObj=' & $TIRObject.pImageObject & ' / W=' & $TIRObject.Width & ' / H=' & $TIRObject.Height & @CRLF)
    EndIf
    If $err Or $ret[0] <> $IR_OK Then Return SetError(2)
EndFunc   ;==>ImageRecognition_FreeObject

 

PD: try to use correct parameters in structure tag You're using long where you should have bool etc. 

Saludos

 

 

 

 

Link to comment
Share on other sites

I'm happy to be able to help you.  for x64 it will work correctly using struct* in all structure parameters.

 

 

Saludos

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...