Skip to content

Swig C# SetConfigOption converts unicode characters to ASCII #14066

@Mbucari

Description

@Mbucari

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

  1. Create a new dotnet test project with the MaxRev Gdal package
dotnet new console --name TestGdalConfigOption
cd TestGdalConfigOption
dotnet add package MaxRev.Gdal.Universal
  1. 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", "")}'");
  1. Run the program
dotnet run

You'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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions