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

Even Rouault even.rouault at mines-paris.org
Tue Feb 22 17:43:43 EST 2011


Le mardi 22 février 2011 23:14:50, Mikhail Tchernychev a écrit :
>    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:

I think you are going to inflict you a lot of pain with LoadLibrary() and C++ 
API at the same time !

If you really need to dynamicly load gdal, use the GDAL C API instead...

> 
> #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@@SAPA
> V1 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
> 
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list