<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
  <author_name>potisan</author_name>
  <author_url>https://blog.hatena.ne.jp/potisan/</author_url>
  <blog_title>potisanのプログラミングメモ</blog_title>
  <blog_url>https://potisan-programming-memo.hatenablog.jp/</blog_url>
  <categories>
    <anon>C#</anon>
    <anon>Windows</anon>
  </categories>
  <description>P/InvokeでLPWSTRやLPCWSTRを無差別にSpan&lt;char&gt;/ReadOnlySpan&lt;char&gt;とref char/in charに置き換えて危険なコードを作ってしまったことの反省文です。 結論 P/Invokeで長さと一緒に渡さない文字列はstringのまま渡す（または内部でstringに変換するかスタック上に同等のデータを作成する）。Span&lt;T&gt;系は終端文字¥0を保証しない。 上に書いた通り、Span&lt;char&gt;とReadOnlySpan&lt;char&gt;は終端文字を保証しません1。string型から作成されたSpan&lt;char&gt;とReadOnlySpan&lt;char&gt;もLen…</description>
  <height>190</height>
  <html>&lt;iframe src=&quot;https://hatenablog-parts.com/embed?url=https%3A%2F%2Fpotisan-programming-memo.hatenablog.jp%2Fentry%2F2025%2F05%2F05%2F202516&quot; title=&quot;C# P/Invoke時のref charやin char使用の落とし穴（終端文字問題） - potisanのプログラミングメモ&quot; class=&quot;embed-card embed-blogcard&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;&quot;&gt;&lt;/iframe&gt;</html>
  <image_url></image_url>
  <provider_name>Hatena Blog</provider_name>
  <provider_url>https://hatena.blog</provider_url>
  <published>2025-05-05 20:25:16</published>
  <title>C# P/Invoke時のref charやin char使用の落とし穴（終端文字問題）</title>
  <type>rich</type>
  <url>https://potisan-programming-memo.hatenablog.jp/entry/2025/05/05/202516</url>
  <version>1.0</version>
  <width>100%</width>
</oembed>
