Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gdal.SetConfigOption exception encountered for x64 #160

Open
ylimemily opened this issue Sep 17, 2020 · 5 comments
Open

Gdal.SetConfigOption exception encountered for x64 #160

ylimemily opened this issue Sep 17, 2020 · 5 comments

Comments

@ylimemily
Copy link

Hi,

I downloaded gdal 2.4.4 and gdal.native 2.4.4 through nuget.org in a C# project and I am encountering exceptions in Gdal.SetConfigOption calls in GdalConfiguration.cs. The exceptions only occurs if the nativePath variable is set to x64 , it works fine when set to x86.

I think this has been raised before in this link but the latest version available in nuget is 2.4.4 and I'm still encountering the issue. So i'm not sure if this has been fixed or is there another version that I can grab from nuget? Thanks.

Below is the trace output:
--- End of inner exception stack trace ---
at OSGeo.GDAL.GdalPINVOKE.SetConfigOption(String jarg1, String jarg2)
at OSGeo.GDAL.Gdal.SetConfigOption(String pszKey, String pszValue)
at TestGdal.GdalConfiguration..cctor() in D:\Visual Studio 2015\Projects\CheckGdal\TestGdal\TestGdal\GdalConfiguration.cs:line 96
error: Executing directory: D:\Visual Studio 2015\Projects\CheckGdal\TestGdal\TestGdal\bin\Debug
error: gdal directory: D:\Visual Studio 2015\Projects\CheckGdal\TestGdal\TestGdal\bin\Debug\gdal
error: native directory: D:\Visual Studio 2015\Projects\CheckGdal\TestGdal\TestGdal\bin\Debug\gdal\x64

@ylimemily
Copy link
Author

More stack trace info:
Exception thrown: 'System.TypeInitializationException' in gdal_csharp.dll
error: System.TypeInitializationException: The type initializer for 'OSGeo.GDAL.GdalPINVOKE' threw an exception. ---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
at OSGeo.GDAL.GdalPINVOKE.SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_Gdal(ExceptionDelegate applicationDelegate, ExceptionDelegate arithmeticDelegate, ExceptionDelegate divideByZeroDelegate, ExceptionDelegate indexOutOfRangeDelegate, ExceptionDelegate invalidCastDelegate, ExceptionDelegate invalidOperationDelegate, ExceptionDelegate ioDelegate, ExceptionDelegate nullReferenceDelegate, ExceptionDelegate outOfMemoryDelegate, ExceptionDelegate overflowDelegate, ExceptionDelegate systemExceptionDelegate)
at OSGeo.GDAL.GdalPINVOKE.SWIGExceptionHelper..cctor()
--- End of inner exception stack trace ---
at OSGeo.GDAL.GdalPINVOKE.SWIGExceptionHelper..ctor()
at OSGeo.GDAL.GdalPINVOKE..cctor()
--- End of inner exception stack trace ---

@SimonA84
Copy link

Is for this issue a workaround available? I have the same issue with version 3.6.3 downloaded via nuget.

@Costigan
Copy link

Costigan commented Aug 31, 2023

I think I'm seeing the same or a closely-related issue with Gdal and Gdal.Native 3.7.1, also downloaded via nuget.

The inner exception thrown when calling Gdal.SetConfigOption("GDAL_DATA", gdalData) is Unable to load DLL 'gdal_wrap': The specified procedure could not be found. (Exception from HRESULT: 0x8007007F)

I've compiled for x64. When I compile for Any CPU, this SetConfigOption succeeds, although it runs into problems later.
It does get far enough to print the drivers (Gdal.GetDriverCount() returns 202).

gdal_wrap.dll does existin the nativePath directory. This code doesn't throw an exception.

            if (!File.Exists(Path.Combine(nativePath, "gdal_wrap.dll")))
                throw new FileNotFoundException(
                    $"GDAL native wrapper file not found at '{Path.Combine(nativePath, "gdal_wrap.dll")}'");

            AddDllDirectory(nativePath);

returns without a problem.

The gdal_wrap.dll appears to be a 64 bit dll:

mshirley@costigan MINGW64 /c/git/github/TraverseDesigner/timeliner/bin/x64/Debug/gdal (VIPERAPS-549-update-gdal)
$ file x64/gdal_wrap.dll
x64/gdal_wrap.dll: PE32+ executable (DLL) (console) x86-64, for MS Windows

I don't know whether gdal_wrap.dll calls another DLL in turn that can't load.

The 32-bit version appears to be in the right place also:

mshirley@costigan MINGW64 /c/git/github/TraverseDesigner/timeliner/bin/x64/Debug/gdal (VIPERAPS-549-update-gdal)
$ file x86/gdal_wrap.dll
x86/gdal_wrap.dll: PE32 executable (DLL) (console) Intel 80386, for MS Windows

When compiled for Any CPU, it gets through Gdal initialization, and I have an error flagged in my data later that's probably related to the upgrade (something got stricter).

@syboldwerff
Copy link

3.7.2 has the same issue, any workaround or fix available.

@jonasnordlund
Copy link

jonasnordlund commented Nov 18, 2024

I've been hit by this, precisely as in #160 (comment) in 3.9.1.

Strangely enough, I'm using 3.9.1 in another project with the same files in the gdal subdirectory and *_csharp.dll's in the application directory, and it seems to be working there. Both projects reference GDAL 3.9.1 and GDAL.Native 3.9.1 via NuGet. I have a hunch it's somehow about auxiliary files that get involved here.

I used Dependency Walker on gdal_wrap along with the disassembled .NET code and this entry point was found in gdal_wrap.dll so I'm 99% sure it's talking of some other entry point / version conflict.

[DllImport("gdal_wrap", EntryPoint = "CSharp_OSGeofGDAL_SetConfigOption___")]
public static extern void SetConfigOption(string jarg1, string jarg2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants