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

时间:2015-02-22 13:36:51

标签: c++ dll

我有这个代码:

#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-Return是这样的:

sprintf(answer,"1|%d|%d|%d|%d",locx,locy,image_width,image_height);
return answer;

现在我不知道如何以可用的格式获取它。如果我使用int,我已经得到了第一个arg(1 =找到图像,0 =没有找到图像)但是我从来没有得到有效的x / y坐标。 (dll有效,我在autoit中使用它。)

有没有人有想法?我正在尝试从我的(谷歌 - )知识

的几个小时结束

真诚地:)

1 个答案:

答案 0 :(得分:0)

问题是答案是本地数据和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;
   } 

或者,您也可以使用referefence传递的预期结果的参数来调用函数。然后你的函数可以通过直接写入main传递的变量来返回结果。