dotnet / dotnet/docs

How to custom marshal parameter in callback function pointer with source generation?

Open
#39,902 0 comments 0 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-fundamentals/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

### Type of issue

Missing information

### Description

My C API takes a function pointer as callback parameter, and the callback takes a string that needs custom marshalling. However, the document doesn't mention how to apply custom marshaller on delegate.

I did some test and it looks MarshalUsing doesn't work with function pointer delegate.

```
internal delegate void ACallback([MarshalUsing(typeof(Utf32StringMarshaller))] string word);

[LibraryImport("./mylib.so", EntryPoint ="TestFunc")]
internal static partial int TestFunc(ACallback callback);
```

The custom marshaller is not called when ACallback is invoked.

### Page URL

https://learn.microsoft.com/en-us/dotnet/standard/native-interop/custom-marshalling-source-generation

### Content source URL

https://github.com/dotnet/docs/blob/main/docs/standard/native-interop/custom-marshalling-source-generation.md

### Document Version Independent Id

92017668-936b-57b2-4e84-8f545ffcc600

### Article author

@jkoritzinsky

### Metadata

* ID: ff5eeaf7-0696-7d3b-3220-c502aa4ec7da
* Service: **dotnet-fundamentals**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.