0

this is my first post at stackoverflow and i`m starting with a problem.

Im writing a gui application with qt in c++. At the moment I'm stuck on loading and saving dlls which were passed to me. For loading and saving i decided to set functions for load- and save- tasks. But the main problem is the loading of different DLL files.

i have the following code at the moment for loading and saving, but getting errors when i'm switching from loading to saving. i know it have something to do with "FreeLibrary()" function. But in case FreeLibrary(hDLLimp) is executed i cannot repeat loading my "implib.dll". The returned hDLLimp is 0. In case i comment FreeLibrary out, the loading function can be re-executed several times. But saving function dont work. hDLLexp becomes "0" and the handle is gone.


    // Load function

HINSTANCE         hDLLimp = 0;
GETCOUNTS         fncgetcounts;
GETITEM           fncgetitem;
IMPEXPINTERFACE   fncimportinterface;
TProcLib          *proclib;
TNativeSurface    nativesurface;
char              filepath[_MAX_PATH];

hDLLimp = LoadLibrary(TEXT("implib.dll"));
DWORD Error = GetLastError();
if( hDLLimp == 0 )
{
    QMessageBox *msgBox = new QMessageBox;
    msgBox->setText("implib.dll konnte nicht geladen werden");
    msgBox->exec();
    return;
}

fncgetcounts = (GETCOUNTS) GetProcAddress(hDLLimp, "GetCounts");
fncgetitem = (GETITEM) GetProcAddress(hDLLimp, "GetItem");
fncimportinterface = (IMPEXPINTERFACE) GetProcAddress(hDLLimp, "ImportInterface");

for (long i=0; i<fncgetcounts()-1; i++) {
     if ((proclib = fncgetitem(i))==NULL) break;
     if (strcmp(proclib->ResName, "SDFIMPORT")==0) {
        strcpy(filepath, "test.sdf");
        fncimportinterface(filepath, _MAX_PATH, &(proclib->PrgId), 2, &nativesurface, 0, 0, NULL, NULL, 1); 
        break;
     }
  }
//FreeLibrary(hDLLimp);

    //Export/Save function

HINSTANCE         hDLLexp = 0;

GETCOUNTS         fncgetcounts;

GETITEM           fncgetitem;

IMPEXPINTERFACE   fncexportinterface;

TProcLib          *proclib;

TNativeSurface    nativesurface;

char              filepath[_MAX_PATH];

hDLLexp = LoadLibrary(TEXT("explib.dll"));
if( hDLLexp == 0 )
    {
        QMessageBox *msgBox = new QMessageBox;
        msgBox->setText("explib.dll konnte nicht geladen werden");
        msgBox->exec();
        return;
    }

    fncgetcounts = (GETCOUNTS) GetProcAddress(hDLLexp, "GetCounts");
    fncgetitem = (GETITEM) GetProcAddress(hDLLexp, "GetItem");
    fncexportinterface = (IMPEXPINTERFACE) GetProcAddress(hDLLexp, "ExportInterface");

    for (long i=0; i<fncgetcounts()-1; i++) {
         if ((proclib = fncgetitem(i))==NULL) break;
         if (strcmp(proclib->ResName, "SDFEXPORT")==0) {
            strcpy(filepath, "test.sdf");
            fncexportinterface(filepath, _MAX_PATH, &(proclib->PrgId), 2, &nativesurface, 0, 0, NULL, NULL, 1); 
            break;
         }
      }
    //FreeLibrary(hDLLexp);
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    After calling `LoadLibrary` you need to check for errors. If `LoadLibrary` fails the return value is `NULL`. At which point call `GetLastError` to find out why it failed. If I were you I would not use a relative path with `LoadLibrary`. Use the full absolute path. – David Heffernan Oct 08 '12 at 20:35
  • Thank you very much David. Absolute path was the answer, after getting ERROR_MOD_NOT_FOUND from `GetLastError` – Alexander Reinke Oct 08 '12 at 21:21

0 Answers0