Как я могу вызвать dll из другой dll, написанной на cpp, без сбоев?

Я пытаюсь вызвать dll из dll, который я создаю в cpp, проблема в том, что он вылетает, когда вызывает другую dll. Я вызываю ту самую первую DLL из программы autoit, которую я работаю. Это первая dll, написанная на cpp:

    #include <iostream>
#include <Windows.h> // this is where MessageBox function resides(Autoit also uses this function)
#include <windows.h>
#include "basicdll.h" // We include our header file which contains function declarations and other instructions

extern "C" // Again this extern just solves c/c++ compatability [b]Make sure to use it because Autoit wont open dll for usage without it![/b]
{
//typedef int (__cdecl *MYPROC)(int x1, int y1, int right, int bottom, char findImage, void* HBMP );
typedef int* (__stdcall *f_funci)(int x1, int y1, int right, int bottom, char findImage, void* HBMP );

DECLDIR int Add( int a, int b )
{
return( a + b ); // Finally we define our basic function that just is a sum of 2 ints and hence returns int(c/c++ type)
}

DECLDIR void Function( void )
{
std::cout << "DLL Called! Hello AutoIt!" << std::endl; // This silly function will just output in our Autoit script "Dll Called!"MessageBox(0, TEXT("DLL from Autoit"), TEXT("Simple Dll..."),0);  // Use good old Message Box inside Windows Api Functions but this time with your own dll }
}

DECLDIR int Recognize(  int x1, int y1, int right, int bottom, char findImage, void* HBMP ){
std::cout << "DLL Called! Hello AutoIt!" << std::endl; // This silly function will just output in our Autoit script "Dll Called!"MessageBox(0, TEXT("DLL from Autoit"), TEXT("Simple Dl22l..."),0);
// Use

HINSTANCE hinstLib;
//MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

// Get a handle to the DLL module.

hinstLib = LoadLibrary(TEXT("D:\\Downloads\\SmartGuy\\SmartGuy\\fol\\imgsearch.dll"));

// If the handle is valid, try to get the function address.

if (hinstLib != NULL)
{
std::cout << "DLL Called! Hello AutoIt!" << std::endl; // This silly function will just output in our Autoit script "Dll Called!"MessageBox(0, TEXT("hinstLib != NULL"), TEXT("Simple Dll..."),0);  //

//MYPROC  ProcAdd = (MYPROC) GetProcAddress(hinstLib, "ImageSearchEx");
f_funci ImageSearchEx = (f_funci)GetProcAddress(hinstLib, "ImageSearchEx");

// If the function address is valid, call the function.

if (NULL != ImageSearchEx)
{

std::cout << "DLL Called! Hello AutoIt!" << std::endl; // This silly function will just output in our Autoit script "Dll Called!"MessageBox(0, TEXT("NULL != ProcAdd"), TEXT("Simple Dll..."),0);  //

fRunTimeLinkSuccess = TRUE;
int result=0;
ImageSearchEx (x1,y1,right,bottom,findImage, HBMP);

int index1 = 1;
std::string test1 = std::to_string(static_cast<long long>(result));
MessageBoxA(NULL, test1.c_str(), "testx", MB_OK);

MessageBox(0, TEXT("DLL from Autoittt"), TEXT("ok"),0);

}
// Free the DLL module.

fFreeResult = FreeLibrary(hinstLib);
}

// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");

return 1;
}

}

вот заголовочный файл:

#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_  // This is basically to tell compiler that we want to include this code only once(incase duplication occurs), you include it in .cpp file(which we will soon do)
#include <iostream>       // Inlcude basic c++ standard library output header(this is used for output text on the screen in our code)

#define DECLDIR __declspec(dllexport)
#include <sstream>// We #define __declspec(dllexport) as DECLDIR macro so when compiler sees DECLDIR it will just take it as __declspec(dllexport)(say that DECLDIR is for readability)
// Note that there is also __declspec(dllimport) for internal linking with others programs but since we just need to export our functions so we can use them with Autoit, thats all we need!extern "C"  // this extern just wraps things up to make sure things work with C and not only C++
{

// Here we declare our functions(as we do with normal functions in c header files, our cpp file will define them(later). Remember that DECLDIR is just a shortcut for __declspec(dllexport)

DECLDIR int Add( int a, int b );
DECLDIR void Function( void );
DECLDIR int Recognize(  int x1, int y1, int right, int bottom, char findImage, void* HBMP  );

#endif // closing #ifndef _DLL_TUTORIAL_H_
}

теперь этот файл также был скомпилирован в dll, называемый basicdll.dll, и я вызываю его из программы autoit.au3:

$dllF1 = DllOpen("basicdll.dll")
DllCall($dllF1, "int:cdecl", "Recognize", "int", $x1, "int", $y1, "int", $right, "int", $bottom,"str", $findImage, "ptr", $HBMP)

DllCall($dllF1, "none", "Function")

все работает нормально, пока программа не достигнет этой строки в файле cpp, который я написал:

ImageSearchEx (x1,y1,right,bottom,findImage, HBMP);

после этого программа вылетает;
Теперь ImageSearchEx — это функция в imgsearch.dll, я сам этого не написал. Но я знаю, как его использовать, вот успешное использование функции в autoit:

$result = DllCall($LibDir & "\imgsearch.dll", "str", "ImageSearchEx", "int", $x1, "int", $y1, "int", $right, "int", $bottom, "str", $findImage, "ptr", $HBMP)

$ HBMP — это обработчик изображения для изображения $ findImage
Я знаю, что, вероятно, сделал что-то не так, как это исправить, кстати, я noob в cpp, так что будьте спокойны.

1

Решение

Задача ещё не решена.

Другие решения