<?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>
  </categories>
  <description>IDispatch関係のP/Invokeでハマった点です。 問題∶構造体内部に構造体のある型でおかしな値やobjectのVARIANTマーシャリングエラーが起きた。 原因∶C#側で構造体内部の構造体をクラス(class)として実装していた。クラスはポインタに変換して配置されるため、構造体の中身が「構造体のバイト数−ポインタのバイト数」だけずれた。 対応∶関連する構造体を構造体(struct)に直した。構造体へのポインタは都度Marshal.PtrToStructureとした。 C#はクラス推しなので無意識にクラスで実装してしまいますが、上の問題からP/Invoke時は構造体を優先した方が良いか…</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%2F03%2F221049&quot; title=&quot;C# P/Invoke ネイティブ構造体の構造体かクラスかはネイティブで構造体が非ポインタとして使われるかも考慮すべき - 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-03 22:10:49</published>
  <title>C# P/Invoke ネイティブ構造体の構造体かクラスかはネイティブで構造体が非ポインタとして使われるかも考慮すべき</title>
  <type>rich</type>
  <url>https://potisan-programming-memo.hatenablog.jp/entry/2025/05/03/221049</url>
  <version>1.0</version>
  <width>100%</width>
</oembed>
