0

I have installed GDAL and GDAL.Native v3.9.1 packages. Console applications work well, but when I use it in AutoCAD Class Library project, it always raises the exception:

The type initializer for 'OSGeo.GDAL.GdalPINVOKE' threw an exception.

I am using Visual Studio 2019. The application is being compiled using .NET Framework 4.8.1.

The default GdalConfiguration.cs file is set with execution path of output directory where gdal folder is installed.

// Add directories
AddDllDirectory(nativePath);
AddDllDirectory(Path.Combine(nativePath, "plugins"));

// Set the additional GDAL environment variables.
string gdalData = Path.Combine(gdalPath, "data");
Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
Gdal.SetConfigOption("GDAL_DATA", gdalData); // <------------ Exception!

string driverPath = Path.Combine(nativePath, "plugins");
Environment.SetEnvironmentVariable("GDAL_DRIVER_PATH", driverPath);

Gdal.SetConfigOption("GDAL_DRIVER_PATH", driverPath); // <--- Exception!

The lines with comments generate the exception.

Any idea to counter this issue?

1

1 Answer 1

0

I think you need to set the Environment Variable path where gdal_wrap.dll is located:

private static void InitializeNativeDllPath(string subfolder)
{
    var nativeDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, pathWhereDllIs);
    var path = Environment.GetEnvironmentVariable("PATH");
    Environment.SetEnvironmentVariable("PATH", $"{nativeDllPath};{path}");
}

Then register the Gdal first:

OSGeo.GDAL.Gdal.AllRegister();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.