C++ 如何从 dll 函数调用中获取正确的 return
C++ How to get the right return from dll function call
我有这个代码:
#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;
}
现在的问题是,我没有从 ImageSearch
得到正确的 return。
DLL-Return 是这样的:
sprintf(answer,"1|%d|%d|%d|%d",locx,locy,image_width,image_height);
return answer;
现在我不知道如何以可用的格式获取它。如果我使用 int,我已经获得了第一个 arg 权利(1 = 找到图像,0 = 没有找到图像)但是我从来没有得到有效的 x/y 坐标。
(dll 有效,我在 autoit 中使用它。)
有人有想法吗?我从几个小时开始就在努力,在我的 (google-) 知识
结束时
真诚的:)
问题是答案是本地数据和一个字符数组。因为它是一个数组,所以不能按值 return 编辑。只有指向答案的指针才会被 returned。但由于它是本地数据,它会在 return 后立即被销毁。所以试图访问它是未定义的行为。
我建议你采用以下方法:
1) 定义一个在 dll 和您的应用程序之间共享的结构:
struct MyData {
int x,y,width,height;
};
2) 将您的 dll 函数的签名更改为 return 此结构的值:
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;
}
或者,您也可以调用该函数,对引用传递的预期结果使用参数。然后你的函数可能 return 通过直接写入 main 传递的变量来得到结果。
我有这个代码:
#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;
}
现在的问题是,我没有从 ImageSearch
得到正确的 return。
DLL-Return 是这样的:
sprintf(answer,"1|%d|%d|%d|%d",locx,locy,image_width,image_height);
return answer;
现在我不知道如何以可用的格式获取它。如果我使用 int,我已经获得了第一个 arg 权利(1 = 找到图像,0 = 没有找到图像)但是我从来没有得到有效的 x/y 坐标。 (dll 有效,我在 autoit 中使用它。)
有人有想法吗?我从几个小时开始就在努力,在我的 (google-) 知识
结束时真诚的:)
问题是答案是本地数据和一个字符数组。因为它是一个数组,所以不能按值 return 编辑。只有指向答案的指针才会被 returned。但由于它是本地数据,它会在 return 后立即被销毁。所以试图访问它是未定义的行为。
我建议你采用以下方法:
1) 定义一个在 dll 和您的应用程序之间共享的结构:
struct MyData {
int x,y,width,height;
};
2) 将您的 dll 函数的签名更改为 return 此结构的值:
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;
}
或者,您也可以调用该函数,对引用传递的预期结果使用参数。然后你的函数可能 return 通过直接写入 main 传递的变量来得到结果。