[gdal-dev] How to use GDAL C++ dynamically?

Mikhail Tchernychev misha at mail.geometrics.com
Tue Feb 22 17:14:50 EST 2011


   Hello List,

I wonder if someone could point me into the right direction. I would 
like to use
GDAL as loaded DLL at run time,  with LoadLibray()  call under windows 
and corresponding
calls under Linux. I see that most of the functions are virtual, so it 
seems it is made
with this in mind.  I am trying to compile simple program:

#include "stdafx.h"
#include <windows.h>

#define CPL_DLL

#include "ogrsf_frmts.h"

typedef void (* pOGRRegisterAll)();
typedef OGRSFDriverRegistrar * (*pGetRegistar)();

int _tmain(int argc, _TCHAR* argv[])
{

     const char *pszDriverName = "ESRI Shapefile";
     HMODULE hDll = LoadLibrary("gdal18.dll");
     if(!hDll) {
             fprintf(stderr,"Cannot load library\n");
             exit(0);
     }


     pOGRRegisterAll pRegFunc = 
(pOGRRegisterAll)GetProcAddress(hDll,"OGRRegisterAll");

     (*pRegFunc)();

     OGRSFDriver *poDriver;
     OGRSFDriverRegistrar * poRegistrar;

     pGetRegistar pRegistrarFunc = 
(pGetRegistar)GetProcAddress(hDll,"?GetRegistrar at OGRSFDriverRegistrar@@SAPAV1 at XZ");

     if(pRegistrarFunc == NULL )
     {
         printf( "Cannot get registrar\n");
         exit( 1 );
     }

     poRegistrar = (*pRegistrarFunc)();

     if( poRegistrar == NULL )
     {
         printf( "Cannot get poRegistrar\n");
         exit( 1 );
     }

     // link error here:
     poDriver = poRegistrar->GetDriverByName(pszDriverName);

     if( poDriver == NULL )
     {
         printf( "%s driver not available.\n", pszDriverName );
         exit( 1 );
     }


     return 0;
}

This is just part of the GDAL API tutorial.

I see two problems here:

1. I have to use C++ mangled names, which is not very nice. Is there 
.DEF file for GDAL
      with meaningful names?

2. I got a link error, which apparently clear why, because 
GetDriverByName() is not virtual.
Now then could I create new files if GDAL is loaded dynamically?

I am using latest GDAL source with VC++ 2010

I tried to search mail list but did not seem find anything.

Thank you very much,

Mikhail



More information about the gdal-dev mailing list