C++如何从dll函数调用中获得正确的返回
C++ How to get the right return from dll function call
我有atm这个代码:
#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
得到正确的回报。DLL返回如下:
sprintf(answer,"1|%d|%d|%d|%d",locx,locy,image_width,image_height);
return answer;
现在我不知道如何获得可用的格式。如果我使用int,我已经得到了第一个正确的arg(1=找到图像,0=没有找到图像),但我再也没有得到有效的x/y坐标。(dll有效,我在autoit中使用了它。)
有人有主意吗?我从几个小时以来一直在尝试,在我(谷歌)知识的尽头
最近:)
问题是答案是本地数据和一个char数组。由于它是一个数组,因此不能按值返回。只返回一个指向答案的指针。但由于它是本地数据,返回后会立即被销毁。因此,试图接受它是一种未定义的行为。
我建议您采用以下方法:
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;
}
或者,您也可以调用该函数,使用reference传递的预期结果的参数。然后,函数可以通过直接写入main传递的变量来返回结果。
相关文章:
- 如何安全地从 DLL 调用返回对象
- 是否可以跨 dll 边界返回常量引用/指向 std::vectors?
- 从非托管 C# DLL 返回列表/数组
- 如何使函数返回数组?用于制作在VB.NET中使用的DLL
- C++ 安全数组在从 c# COM dll 返回时具有无效数据
- C# 中的 C++ DLL 返回 -858993460
- C ++ DLL导入:函数调用返回错误的结果
- Java 调用 dll 字符串返回类型方法
- WinAPI - GetLastError 在通过 COM ATL DLL 调用时始终返回 0
- winAPI FindResource 仅在 DLL 中返回 NULL
- WScript.CreateObject 在调用 C++ COM dll 函数时返回空
- LoadLibrary('user32.dll') 返回 14007
- DLL – 在 DLL 初始化时填充的静态向量,向客户端程序返回零大小
- 从 dll 返回多个字符串
- GetFullPathNameA 不返回 DLL 的路径
- 为什么 VS C++链接器返回具有混合 DLL 转发的"LNK2001: unresolved external symbol"?
- 如何使用返回矢量C++ DLL
- 如何返回包含来自com dll的多个空字符的BSTR
- 如何从C DLL返回的指针中获得VBA中的整个矩阵
- 如何使用Java代码中使用和返回.dll对象的函数