0

My friend and I are working on a project.

He gave me a .dll file which can activate web camera and stream the view.

He also gave me a small example project written in VS2013 MFC that demos the function.

typedef int(*ExtportFuncA)(void);
typedef int(*Open)(void);

void Ctrydll2Dlg::OnBnClickedOk()
{
    //CDialogEx::OnOK();
    ExtportFuncA  ga = NULL;
    HINSTANCE hLib = LoadLibrary(_T("ShowImg.dll"));
    if (hLib)
    {
        ga = (ExtportFuncA)GetProcAddress(hLib, "ExtportFuncA");
        ga();
    }
    system("pause");
}

void Ctrydll2Dlg::OnBnClickedButton1()
{
    Open  gb = NULL;
    HINSTANCE hLib = LoadLibrary(_T("ShowImg.dll"));
    if (hLib)
    {
        gb = (ExtportFuncA)GetProcAddress(hLib, "Open");
        int system = gb();
        if (system == -1)
        {
            printf("not get device");
        }
    }
}

However, I failed to load that .dll file in QT project.

#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>
typedef int (*ExportFuncA)(void);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QLibrary lib("ShowImg.dll");
    lib.load();
    if(!lib.load())
    {
        qDebug() << "Cannot Open Library";
        return 0;
    }
    ExportFuncA CLI = (ExportFuncA)lib.resolve("ExportFuncA");
    if(!CLI)
    {
        qDebug() << "Cannot Load Function";
        return 0;
    }
    printf("Hello World");
    return a.exec();
}

The error always shows "Cannot Open Library", that means the .dll cannot be loaded at the beginning,

but my friend's demo project runs fine on my computer, I can see the camera's image.

I suspect the problem is related to 32bit and 64bit format,

but since I am not familiar with .dll file, any suggestion will be grateful.

APU
  • 239
  • 2
  • 17
  • 2
    Probably `ShowImg.dll` has some dependent libraries that miss on your computer. – vahancho Jun 22 '15 at 07:24
  • but how to explain that it runs fine on my friends demo project, and fails on a QT project? – APU Jun 22 '15 at 07:28
  • You have to pay attention on the location of `ShowImg.dll` library. `QLibrary lib("ShowImg.dll");` assumes, that the dll is in the current working directory. – vahancho Jun 22 '15 at 07:41
  • I found the root cause...it was two stupid typos, the .dll name is "ShowImage.dll" and the function name is "ExtportFuncA" after I used Dependency Walker checked it. – APU Jun 22 '15 at 08:20
  • What does `QLibrary::errorString()` return? – RobbieE Jun 22 '15 at 11:09
  • 1
    Why do you run `lib.load()` twice? – RobbieE Jun 22 '15 at 11:11
  • yeah...it is not necessary to lib.load() twice, I keep the one in the condition and remove another – APU Jun 23 '15 at 01:28
  • I haven't use errorstring yet, but according to the official documentation, it "returns a text string with the description of the last error that occurred. Currently, errorString will only be set if load(), unload() or resolve() for some reason fails." – APU Jun 23 '15 at 01:31

0 Answers0