C++ C++;如何从dll函数调用中获得正确的返回
我有这个密码:C++ C++;如何从dll函数调用中获得正确的返回,c++,dll,C++,Dll,我有这个密码: #include <iostream> #include <string> #include <cstddef> #include <iomanip> #include <cstdlib> #include <stdio.h> #include <windows.h> using namespace std; int main() { //Define ScreenResolitio
#include <iostream>
#include <string>
#include <cstddef>
#include <iomanip>
#include <cstdlib>
#include <stdio.h>
#include <windows.h>
using namespace std;
int main()
{
//Define ScreenResolition
int DesktopResolution[] = { GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) };
cout << "DesktopResolution: <" << DesktopResolution[0] << ", " << DesktopResolution[1] << ">\n";
//Load DLL
HINSTANCE IShndl = NULL;
IShndl = LoadLibrary("C:\\Users\\Bilbao\\Desktop\\C++\\Hello World\\Project1\\Project1\\DLLS\\ImageSearch.dll");
if (IShndl != NULL){
cout << "ISHandle: " << IShndl << "\n";
//DEFINE
cout << "Define... \n";
typedef int(__stdcall * FuncImageSearch)(int aLeft, int aTop, int aRight, int aBottom, string aImageFile);
typedef int(__stdcall * FuncDLLTest)(int a);
//Set ImageSearch
cout << "Set ImageSearch... \n";
FuncImageSearch ImageSearch;
ImageSearch = NULL;
//Set Test Function
cout << "Set Test Function... \n";
FuncDLLTest ISTest;
ISTest = NULL;
//Get ImageSearch
cout << "GetProcAddress 'ImageSearch'... \n";
ImageSearch = (FuncImageSearch)GetProcAddress(IShndl, "ImageSearch");
if (ImageSearch != NULL){
cout << "ImageSearch: " << ImageSearch << "\n";
int answer = ImageSearch(0, 0, DesktopResolution[0], DesktopResolution[1], "C:\\Users\\Bilbao\\Desktop\\C++\\Hello World\\Project1\\Project1\\DLLS\\test.bmp");
cout << "ImageSearch CALL return: Size " << sizeof(answer) << "\n";
cout << "ImageSearch CALL: " << answer << "\n";
if (answer == 1){
cout << "Found Image: \n";
}
else{
cout << "No ImageFound. \n";
}
}
//Get Test Function
cout << "GetProcAddress 'ImageTest'... \n";
//ISTest = (FuncDLLTest)GetProcAddress(IShndl, "ImageTest");
ISTest = (FuncDLLTest)GetProcAddress(IShndl, "ImageTest");
if (ISTest != NULL){
cout << "ISTest: " << ISTest << "\n";
int TestValue = 100; //1
while(TestValue < 10){
cout << "test: " << TestValue << "\n";
int ISTestRestult = ISTest(TestValue);
cout << "ISTEST CALL return: " << ISTestRestult << "\n";
TestValue++;
}
}
}
system("PAUSE");
return 0;
}
现在我不知道如何得到一个可用的格式。如果我使用int,我已经获得了正确的第一个参数(1=找到图像,0=没有找到图像),但我从未获得有效的x/y坐标。
(dll工作正常,我在autoit中使用了它。)
有人有主意吗?我从几个小时开始就在尝试,在我(谷歌)知识的尽头
问题是答案是本地数据和字符数组。因为它是一个数组,所以不能通过值返回。只返回指向答案的指针。但由于它是本地数据,返回后会立即被销毁。所以试图接近它是一种未定义的行为 我建议你采取以下办法: 1) 定义dll和应用程序之间共享的结构:
struct MyData {
int x,y,width,height;
};
2) 更改dll函数的签名以按值返回此结构:
MyData ImageSearch(....) {
MyData d;
...
//sprintf(answer,"1|%d|%d|%d|%d",locx,locy,image_width,image_height);
d.x=locx; d.y=locy; ...
return d;
}
或者,您也可以使用referefence传递的预期结果的参数来调用该函数。然后,您的函数可以通过直接写入main传递的变量来返回结果 让我猜猜:
answer
是dll函数的局部变量吗?是的,但通过return它进入主调用(我也可以在那里调用它result或其他任何东西)返回工作,但类型似乎是错误的/a array或so idk:/如果它是局部变量,则在返回后会立即删除它。因此,在main()中,容器不再被认为是安全的!泰,我试试看。我对C++很陌生,而且仍然在读很多书。下面是dll代码,以防您观看它(它是公共的,并且在autoit中工作良好,因为它主要是由什么制作的;()发现,这有助于解决所有问题:cout
MyData ImageSearch(....) {
MyData d;
...
//sprintf(answer,"1|%d|%d|%d|%d",locx,locy,image_width,image_height);
d.x=locx; d.y=locy; ...
return d;
}