C++如何从dll函数调用中获得正确的返回

C++ How to get the right return from dll function call

本文关键字:返回 dll 函数调用 C++      更新时间:2023-10-16

我有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传递的变量来返回结果。