-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Description
What is the bug?
The swig-generated bindings for Gdal.SetConfigOption() and Gdal.GetConfigOption() are incorrectly marshalling values as ANSI strings instead of UTF-8 strings, which appears to be what GDAL is expecting. All non-ansi strings passed to Gdal.SetConfigOption() are converted to ansi. This is the cause of #1647, but it also affects any other config options which are file paths. e.g. GEOTIFF_CSV, GDAL_DATA, and GDAL_CURL_CA_BUNDLE.
For .NET to marshal UTF-8 strings, the marshaller must be explicitly told that strings are UTF-8 using the [MarshalAs(UnmanagedType.LPUTF8Str)] attribute. Here's what those P/Invoke signatures should look like.
[DllImport("gdal_wrap", EntryPoint = "CSharp_OSGeofGDAL_GetConfigOption___")]
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
static extern string GetConfigOption(string pszKey, string pszDefault);
[DllImport("gdal_wrap", EntryPoint = "CSharp_OSGeofGDAL_SetConfigOption___")]
static extern void SetConfigOption(string pszKey, [MarshalAs(UnmanagedType.LPUTF8Str)] string pszValue);Steps to reproduce the issue
- Create a new dotnet test project with the MaxRev Gdal package
dotnet new console --name TestGdalConfigOption
cd TestGdalConfigOption
dotnet add package MaxRev.Gdal.Universal- Edit the Program.cs file
to the below:
Console.OutputEncoding = System.Text.Encoding.UTF8;
OSGeo.GDAL.Gdal.SetConfigOption("UNICODE_STRING", "Ĥĕļĺō Ŵŏŕľď");
Console.WriteLine($"UNICODE_STRING value is '{OSGeo.GDAL.Gdal.GetConfigOption("UNICODE_STRING", "")}'");- Run the program
dotnet runYou'll see the following output:
UNICODE_STRING value is 'Hello World'Versions and provenance
Windows 11
Gdal version 3.12.1.440 MaxRev Universal Nuget Package
Additional context
No response