CustomHostClr Sample doesn't work with returning values
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 35/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
调研方向
从 core/hosting/src 开始,重点查看第 190 行附近的 nativehost.cpp 和 Lib.cs,并运行 CustomHostClr 示例以复现返回缓冲区的行为。添加 issue 中描述的返回值示例,然后验证 native 输出包含 managed 返回文本,并明确 char 与 wchar_t 的 marshaling 结果。
由索引模型根据 Issue 内容生成。
描述
[CustomHostClr Sample doesn't work with returning values]
I worked through this sample core/hosting/src. There is a missing sample for invocation when some value retuned from the managed side via pre-defined buffer.
I tried to add this sample myself but it doesn't seem to work.
On the native side (in nativehost.cpp line ~190) added:
// Function pointer to managed delegate with non-default signature
typedef void (CORECLR_DELEGATE_CALLTYPE *custom_entry_point_returning_fn)(wchar_t const* someptr, wchar_t* out_res);
custom_entry_point_returning_fn custom_returning = nullptr;
// Custom delegate type
rc = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnet_type,
STR("CustomEntryPointReturning") /*method_name*/,
STR("DotNetLib.Lib+CustomEntryPointReturningDelegate, DotNetLib") /*delegate_type_name*/,
nullptr,
(void**)&custom_returning);
assert(rc == 0 && custom_returning != nullptr && "Failure: load_assembly_and_get_function_pointer()");
wchar_t const* someptr = STR("some data");
wchar_t buffer[128]{STR("some init data")};
custom_returning(someptr, buffer);
std::wcout << STR("returned buffer: ") << buffer << std::endl;
In the Lib.cs I added:
public delegate void CustomEntryPointReturningDelegate(IntPtr someptr, StringBuilder res);
public static void CustomEntryPointReturning(IntPtr someptr, StringBuilder res)
{
string message = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? Marshal.PtrToStringUni(someptr)
: Marshal.PtrToStringUTF8(someptr);
Console.WriteLine($"Hello, world! from {nameof(CustomEntryPointReturning)} in {nameof(Lib)}");
Console.WriteLine($"Passed-in string: {message}");
Console.WriteLine($"the buffer on the managed side: {res.ToString()}");
res.Clear();
res.Append("some returned text");
Console.WriteLine($"the buffer on the managed side: {res.ToString()}");
}
It outputs:
Hello, world! from CustomEntryPointReturning in Lib
Passed-in string: some data
the buffer on the managed side: s
the buffer on the managed side: some returned text
returned buffer:
Please, add this kind of example I sketch above (with some fixes required) into the sample.
UPDATE:
To fix reported behaviour
On the native side (in nativehost.cpp line ~190) added:
// Function pointer to managed delegate with non-default signature
typedef void (CORECLR_DELEGATE_CALLTYPE *custom_entry_point_returning_fn)(wchar_t const* someptr, char* out_res);
custom_entry_point_returning_fn custom_returning = nullptr;
// Custom delegate type
rc = load_assembly_and_get_function_pointer(
dotnetlib_path.c_str(),
dotnet_type,
STR("CustomEntryPointReturning") /*method_name*/,
STR("DotNetLib.Lib+CustomEntryPointReturningDelegate, DotNetLib") /*delegate_type_name*/,
nullptr,
(void**)&custom_returning);
assert(rc == 0 && custom_returning != nullptr && "Failure: load_assembly_and_get_function_pointer()");
wchar_t const* someptr = STR("some data");
char buffer[128]{"some init data"};
custom_returning(someptr, buffer);
std::wcout << STR("returned buffer: ") << buffer << std::endl;
Note the change of type for out_res pointer from wchat_t to char. And then it works (the output is below):
Hello, world! from CustomEntryPointReturning in Lib
Passed-in string: some data
the buffer on the managed side: some init data
the buffer on the managed side: some returned text
returned buffer: some returned text
It's strange that marshaled back string is 8-bit instead of 16-bit. If you let me know I'll create issue in more relevant repo about this possible CLR marshaling bug.
Issue metadata
- Issue type: sample-update
- 主要语言
- C#
- 星标
- 3.7k
- 派生
- 5.1k
- 平均合并
- 19 小时 47 分钟
- 30 天内合并 PR
- 2
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/samples 的其他 Issue
-
dotnet-target-version
难度 2/5 1-3 小时 新手友好度 77/100
-
dotnet-target-version
难度 2/5 1-3 小时 新手友好度 76/100
-
dotnet-target-version
难度 2/5 1-3 小时 新手友好度 78/100
-
Race condition in orleans/ShoppingCart/Grains/InventoryGrain.cs#L48可能已有人在做 @TimonPasslick 于 160 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
-
dotnet-target-version
难度 2/5 1-3 小时 新手友好度 72/100
相似的 Issue
-
Audit by ear: boss warnings, other not-enough messages, ailments and barriers in a chapter 1 battle未关闭enhancement needs-game ready-for-agent
难度 2/5 1-3 小时 新手友好度 72/100
Sour-Dev-Home/metaphor-accessibility#1273 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
joinrpg/joinrpg-net#6336 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
muxinc/mux-csharp#88 ·
-
In-car displays show 0 km/h and P while the AV drives可能已有人在做 @bazilinskyy 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 66/100
bazilinskyy/coupled-sim#366 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 3 天内回复