Upgrade to Pro — share decks privately, control downloads, hide ads and more …

C# and C++ Interoperability - cho-dotnetnew

C# and C++ Interoperability - cho-dotnetnew

2026/5/29 超dotnetnew
「C++とC#の相互運用」遥佐保

Avatar for Akiko Kawai

Akiko Kawai

May 29, 2026

More Decks by Akiko Kawai

Other Decks in Programming

Transcript

  1. Type C# → C++(序章) COM系 COM Interop(RCW) CLI系 C++/CLIラッパー Export系

    P/Invoke (DLLImport,LibraryImport) https://learn.microsoft.com/ja-jp/dotnet/standard/native-interop/runtime-callable-wrapper RCW C# Client COM Object C++/CLI 自作Wrapper (/clr) C# Client Native Class/DLL C# Client Marshal (auto- generated) Native DLL CLRに組み込まれている DllImport 実行時 LibraryImportビルド時
  2. Type C++ → C# COM系 COM Interop(CCW) Windows only 超Legacy

    マーシャリング コスト大 CLI系 /clr 化 つまり /clr option Windows only Legacy C++/CLIブリッジDLL CCW C# Object COM Client Native Class/DLLのCLR化 (/clr) IJW(It Just Works) Thunk using VTableFixup C++/CLI 自作ブリッジ DLL C# Object Native Class/DLL C# Object
  3. Type C++ → C# Export 系 DllExport (~.NET Framework) Windows

    only 超Legacy NativeAOT (.NET 7+) +[UnmanagedCallersOnly] ホットリロード未対応, ランタイムは固定, 手動でC ABIを解決, ライブラリ抱き込み Cross- platform Build後にToolでC# DLLのILを書き換え パッチDLL(純粋なIL) C# Object using VTableFixup Native Class/DLL AOTコンパイラが C#コードをNativeDLLへ Native Class/DLL C# Native
  4. Type C++ → C# Hosting 系 CLR Hosting APIはCOM Interface,

    CCWなし Windows only Hostfxr (.NET Core 3.0+) +[UnmanagedCallersOnly] (.NET 5+) 直接C API使う Cross-platform C++ Process CLR Runtime C# Managed Heap Native Class/DLL C++ Process Core CLR Runtime C# Managed Heap Native Class/DLL
  5. Type C# → C++ C++ → C# COM系 COM Interop(RCW)

    COM Interop(CCW) CLI系 C++/CLIラッパー /clr, C++/CLIブリッジDLL Export系 P/Invoke (DLLImport,LibraryImport) DllExport, NativeAOT+[UnmanagedCallersOnly] Hosting系 なし (C++は既にNativeなので Runtime不要) CLR Hosting, hostfxr+[UnmanagedCallersOnly]