Skip to content

Commit 8003b88

Browse files
authored
Fix delegate return type: byte* instead of string for const char* (#336)
Fix delegate return type: byte* instead of string for const char*
2 parents 41921f1 + 6f289a2 commit 8003b88

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

FFmpeg.AutoGen.Abstractions/generated/Delegates.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public unsafe struct AVClass_get_category_func
106106
}
107107

108108
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
109-
public unsafe delegate string AVClass_item_name (void* @ctx);
109+
public unsafe delegate byte* AVClass_item_name (void* @ctx);
110110
public unsafe struct AVClass_item_name_func
111111
{
112112
public IntPtr Pointer;

FFmpeg.AutoGen.CppSharpUnsafeGenerator/Processing/FunctionProcessor.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,29 @@ internal TypeDefinition GetDelegateType(FunctionType functionType, string name)
8282
{
8383
Name = $"{name}_func",
8484
FunctionName = name,
85-
ReturnType = GetReturnType(functionType.ReturnType.Type, name),
85+
ReturnType = GetDelegateReturnType(functionType.ReturnType.Type, name),
8686
Parameters = functionType.Parameters.Select(GetParameter).ToArray()
8787
};
8888
_context.AddDefinition(@delegate);
8989

9090
return @delegate;
9191
}
9292

93+
private TypeDefinition GetDelegateReturnType(Type type, string name)
94+
{
95+
// For delegates (function pointers), const char* must be byte* not string,
96+
// because string marshaling does not work with unmanaged function pointers.
97+
if (type is PointerType pointerType &&
98+
pointerType.QualifiedPointee.Qualifiers.IsConst &&
99+
pointerType.Pointee is BuiltinType builtinType &&
100+
builtinType.Type == PrimitiveType.Char)
101+
{
102+
return new TypeDefinition { Name = "byte*" };
103+
}
104+
105+
return GetReturnType(type, name);
106+
}
107+
93108
private FunctionParameter GetParameter(Parameter parameter, int position)
94109
{
95110
var name = string.IsNullOrEmpty(parameter.Name) ? $"p{position}" : parameter.Name;

FFmpeg.AutoGen/generated/Delegates.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public unsafe struct AVClass_get_category_func
106106
}
107107

108108
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
109-
public unsafe delegate string AVClass_item_name (void* @ctx);
109+
public unsafe delegate byte* AVClass_item_name (void* @ctx);
110110
public unsafe struct AVClass_item_name_func
111111
{
112112
public IntPtr Pointer;

0 commit comments

Comments
 (0)