CustomHostClr Sample doesn't work with returning values
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 35/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
調査の方向性
core/hosting/src、特に190行目付近のnativehost.cppとLib.csから始め、CustomHostClrサンプルを実行して、返されるバッファーの動作を再現します。issueで説明されている戻り値の例を追加し、その後、ネイティブ出力にマネージドな戻り値のテキストが含まれていることを確認し、charとwchar_tのマーシャリング結果を明確にします。
索引モデルが 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分
- マージ済み PR(30日)
- 2
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 日以内に返信