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

用户名

我有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;
}

现在的问题是,我没有从获得正确的回报ImageSearchDLL返回是这样的:

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

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

有人有主意吗?我几个小时以来一直在努力,直到(google-)知识结束

真诚的:)

克里斯多夫(Christophe)

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章