我试图在cpp中调用一个dll形成一个dll,问题是它在调用另一个dll时崩溃了。我用自动程序打电话给那个拳头dll。这是用cpp编写的第一个dll:
#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的图像处理程序 我知道我可能做错了什么,如何解决这个问题,顺便说一句,我是cpp中的菜鸟,所以放轻松。