Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
251 views3 pages

Csharp - Intermittent - Osgeo - Gdal.Gdalpinvoke' Exception On First Call

The document describes an issue where an 'OSGeo.GDAL.GdalPINVOKE' exception occurs on the first call to any GDAL routine after setting the GDAL environment variables and paths. This intermittent error occurs on both 32-bit and 64-bit Windows machines when GDAL is installed in a custom location rather than the default installation path. The code works sometimes on some machines but fails other times, even when the GDAL DLLs are in the same location but the paths and variables are set to different folders.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
251 views3 pages

Csharp - Intermittent - Osgeo - Gdal.Gdalpinvoke' Exception On First Call

The document describes an issue where an 'OSGeo.GDAL.GdalPINVOKE' exception occurs on the first call to any GDAL routine after setting the GDAL environment variables and paths. This intermittent error occurs on both 32-bit and 64-bit Windows machines when GDAL is installed in a custom location rather than the default installation path. The code works sometimes on some machines but fails other times, even when the GDAL DLLs are in the same location but the paths and variables are set to different folders.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Csharp - Intermittent - OSGeo.GDAL.GdalPINVOKE' exception on first call.

Using GDAL>>> gdal-110-1600-core.msi


Same type of problem on a Windows 7 32 bit and 64 bit machine. The problem occurs at the first call to any
gdal routine after the paths and environment variables are set. In this case:

Here

>>

Environment.SetEnvironmentVariable("PROJ_LIB", projlib);
MessageBox.Show("Final PROJ_LIB= " + projlib);
OSGeo.GDAL.Gdal.SetConfigOption("PROJ_LIB", projlib);

This is the error message - "An unhandled exception of type 'System.TypeInitializationException' occurred in
gdal_csharp.dll Additional information: The type initializer for 'OSGeo.GDAL.GdalPINVOKE' threw an
exception.

This code works sometime on some machines and fails other times.
Right now it will work regardless of where the csharp dlls are, but will only work when the GDAL path and
Environmental variables are in: C:\Program Files (x86)\GDAL
and not in: C:\Users\Charlie\Desktop\RasterTest2\Libraries\GDAL

using
using
using
using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
OSGeo.GDAL;
GDALUtil.Python;
GDALUtil;
GDALUtil.Cpp;
System.Drawing.Imaging;
System.IO; //http://msdn.microsoft.com/en-us/library/system.io.path%28v=vs.110%29.aspx

namespace RasterTest1
{
public partial class GdalWinForm : Form
{
OSGeo.GDAL.Dataset dataSet;
//private string image = "";
private int width = -1;
private int height = -1;
private string loaded = "";
public GdalWinForm()
{
InitializeComponent();
#region GDAL
string show = "no";
string oldPath = Environment.GetEnvironmentVariable("PATH");
if (show != "no") MessageBox.Show("OLDPATH= " + oldPath);
string gdaldir = @"..\..\..\..\..\Libraries\GDAL";

// Does not WORK

// WORKS

//string gdaldir = @"C:\Program Files (x86)\GDAL";


if (show != "no") MessageBox.Show("Indirect Path to GDAL= " + gdaldir);
//http://msdn.microsoft.com/en-us/library/system.io.path%28v=vs.110%29.aspx
gdaldir = Path.GetFullPath(gdaldir);
if (show != "no") MessageBox.Show("Full Path to GDAL=" + gdaldir);
string path = gdaldir + ";" + oldPath;
Environment.SetEnvironmentVariable("PATH", path);
string newPath = Environment.GetEnvironmentVariable("PATH");
if (show != "no") MessageBox.Show("NEW path= " + newPath);
string gdal_data = Environment.GetEnvironmentVariable("GDAL_DATA");
if (show != "no") MessageBox.Show("Initial GDAL_DATA= " + gdal_data);
gdal_data = gdaldir + @"\gdal-data";
Environment.SetEnvironmentVariable("GDAL_DATA", gdal_data);
gdal_data = ""; gdal_data = Environment.GetEnvironmentVariable("GDAL_DATA");
if (show != "no") MessageBox.Show("Final GDAL_DATA= " + gdal_data);
/////OSGeo.GDAL.Gdal.SetConfigOption("GDAL_DATA", gdaldata);
string gdal_driver_path = Environment.GetEnvironmentVariable("GDAL_DRIVER_PATH");
if (show != "no") MessageBox.Show("Initial GDAL_DRIVER_PATH= " + gdal_driver_path);
gdal_driver_path = gdaldir + @"\gdalplugins";
Environment.SetEnvironmentVariable("GDAL_DRIVER_PATH", gdal_driver_path);
gdal_driver_path = Environment.GetEnvironmentVariable("GDAL_DRIVER_PATH");
if (show != "no") MessageBox.Show("Final GDAL_DRIVER_PATH= " + gdal_driver_path);
//OSGeo.GDAL.Gdal.SetConfigOption("GDAL_DRIVER_PATH", gdal_driver_path);
string projlib = gdaldir + @"\projlib";
if (show != "no") MessageBox.Show("value to be set= " + projlib);

// http://msdn.microsoft.com/en-us/library/z46c489x%28v=vs.110%29.aspx
string proj_lib = Environment.GetEnvironmentVariable("PROJ_LIB");
if (proj_lib != null) MessageBox.Show("initial PROJ_LIB+ " + proj_lib);
Environment.SetEnvironmentVariable("PROJ_LIB", projlib);
MessageBox.Show("Final PROJ_LIB= " + projlib);
OSGeo.GDAL.Gdal.SetConfigOption("PROJ_LIB", projlib);

// Fails Here

OSGeo.GDAL.Gdal.SetConfigOption("GDAL_CACHEMAX", "1000000");
OSGeo.GDAL.Gdal.SetConfigOption("CPL_TMPDIR", "c:\\");
OSGeo.OGR.Ogr.RegisterAll();
Gdal.AllRegister();
#endregion
}

(regardless the call)

You might also like