Skip to content

Commit cdc7fbf

Browse files
committed
Merge branch 'fix/utf8-settings-stringlist'
2 parents 4751bbb + 0dcd5d7 commit cdc7fbf

3 files changed

Lines changed: 38 additions & 21 deletions

File tree

Function/BNSettingsSetStringList.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ internal static extern bool BNSettingsSetStringList(
3333
// const char* key
3434
[MarshalAs(UnmanagedType.LPUTF8Str)] string key ,
3535

36-
// const char** _value
37-
string[] _value ,
38-
36+
// const char** _value (UTF-8 block built by the caller; string[] cannot be
37+
// marshaled as UTF-8 by attribute, so it is passed as a raw pointer)
38+
IntPtr _value ,
39+
3940
// uint64_t size
40-
ulong size
41+
ulong size
4142
);
4243
}
4344
}

Function/BNSettingsUpdateStringListProperty.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ internal static extern bool BNSettingsUpdateStringListProperty(
2727
// const char* property
2828
[MarshalAs(UnmanagedType.LPUTF8Str)] string property ,
2929

30-
// const char** _value
31-
string[] _value ,
32-
30+
// const char** _value (UTF-8 block built by the caller; string[] cannot be
31+
// marshaled as UTF-8 by attribute, so it is passed as a raw pointer)
32+
IntPtr _value ,
33+
3334
// uint64_t size
34-
ulong size
35-
35+
ulong size
36+
3637
);
3738
}
3839
}

Handle/BNSettings.cs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,23 @@ public bool SetStringList(
505505
SettingsScope scope = SettingsScope.SettingsAutoScope
506506
)
507507
{
508-
return NativeMethods.BNSettingsSetStringList(
509-
this.handle,
510-
null == view ? IntPtr.Zero : view.DangerousGetHandle() ,
511-
null == function ? IntPtr.Zero : function.DangerousGetHandle(),
512-
scope ,
513-
key,
514-
value,
515-
(ulong)value.Length
516-
);
508+
// BN expects the value list as a UTF-8 char** block. .NET cannot marshal a
509+
// string[] element as UTF-8 via [MarshalAs], so build the block by hand; the
510+
// core copies it, so freeing after the call (allocator dispose) is safe.
511+
using (ScopedAllocator allocator = new ScopedAllocator())
512+
{
513+
IntPtr valueBlock = allocator.AllocUtf8StringArray(value);
514+
515+
return NativeMethods.BNSettingsSetStringList(
516+
this.handle,
517+
null == view ? IntPtr.Zero : view.DangerousGetHandle() ,
518+
null == function ? IntPtr.Zero : function.DangerousGetHandle(),
519+
scope ,
520+
key,
521+
valueBlock,
522+
(ulong)value.Length
523+
);
524+
}
517525
}
518526

519527
public bool SetJson(
@@ -619,9 +627,16 @@ public bool UpdateUInt64Property(string key , string property , ulong value)
619627
/// </summary>
620628
public bool UpdateStringListProperty(string key , string property , string[] value)
621629
{
622-
return NativeMethods.BNSettingsUpdateStringListProperty(
623-
this.handle , key , property , value , (ulong)value.Length
624-
);
630+
// Build the value list as a UTF-8 char** block (string[] cannot be UTF-8
631+
// marshaled by attribute); the core copies it, so free after the call.
632+
using (ScopedAllocator allocator = new ScopedAllocator())
633+
{
634+
IntPtr valueBlock = allocator.AllocUtf8StringArray(value);
635+
636+
return NativeMethods.BNSettingsUpdateStringListProperty(
637+
this.handle , key , property , valueBlock , (ulong)value.Length
638+
);
639+
}
625640
}
626641

627642
/// <summary>

0 commit comments

Comments
 (0)