SlideShare a Scribd company logo
C++ via C#
Egor Bogatov
@EgorBo
Calling C++ from C#. For what?
โ€ข Lovely IDE + R#
โ€ข Syntactic sugar
โ€ข nuget
Calling C++ from C#. How?
โ€ข C++/CLI
โ€ข COM
โ€ข [DllImport], [MethodImpl]
โ€ข IPC, Sockets,โ€ฆ
Calling C++ from C#. How?
โ€ข C++/CLI
โ€ข COM
โ€ข [DllImport], [MethodImpl]
โ€ข IPC, Sockets,โ€ฆ
UrhoSharp
โ€ข ~ 200 classes
โ€ข ~ 5000 methods
โ€ข Same FPS
โ€ข +10% memory
โ€ข C# for:
Windows
Mac OS
Android
iOS
tvOS
Urho3D API
class Node : public Animatable
{
public:
Node(Context* context);
void SetName(const String& name);
const String& GetName();
void SetPosition(const Vector3& position);
const Vector3& GetPosition();
void AddChild(Node* node, unsigned index = M_MAX_UNSIGNED);
void RemoveChild(Node* node);
};
public class Node : Animatable
{
public Node(Context context) { }
public string Name { get; set; }
public Vector3 Position { get; set; }
void AddChild(Node node, uint index = uint.MaxValue) { }
void RemoveChild(Node node) { }
}
Node.cpp
C++
C#
Urho3D API
class Node : public Animatable
{
public:
Node(Context* context);
void SetName(const String& name);
};
glue.cpp
Urho3D API
Node.cpp
C#
Node node = new Node(context);
node.SetName("foo");
Node* node = new Node(context_);
node->SetName("foo");
Urho3D APIvoid SetName(const String& name);
extern "C"
DllExport void Node_SetName (Node *target, const char * name){
target->SetName (Urho3D::String(name));
}
[DllImport ("mono-urho", โ€ฆ)]
static extern void Node_SetName (IntPtr handle, string name);
public void SetName (string name) { Node_SetName (handle, name);
Node.cppNode.h
binding.cpp
Node.cs
original API
generated
generated
Urho3D API
class Node : public Animatable
{
public:
Node(Context* context);
void SetName(const String& name);
};
DllExport void* Node_Node (Context * context) {
return new Node(context);
}
DllExport void Node_SetName (Node *target, const char * name) {
target->SetName (Urho3D::String(name));
}
public partial class Node : Animatable
{
private IntPtr handle;
[DllImport ("mono-urho")]
static extern IntPtr Node_Node (IntPtr context);
public Node (Context context) { handle = Node_Node (context.Handle); }
[DllImport ("mono-urho")]
static extern void Node_SetName (IntPtr handle, string name);
public void SetName (string name) { Node_SetName (handle, name);
Node.cppNode.h
binding.cpp
Node.cs
original API
generated
generated
Binding flow
Urho3D sources
(*.cpp, *.h)
Urho.pch
Generator (Binder)
binding.cpp *.cs
UrhoSharp.dll
Win: mono-urho.dll
Droid: libmono-urho.so
OSX: libmono-urho.dylib
iOS: urho.framework
nuget package
all-urho.cpp via clang
roslyn
โ€ข clang
โ€ข gcc
โ€ข msvc
clang.dll
Nrefactory.dll
PCL, Profile7
Object lifecycle
RefCounted:
โ€ข AddRef
โ€ข ReleaseRef
โ€ข RefsCount
โ€ข ~dctor
Node node = new Node(context)
1) Allocate a native object (Node* node = new Node)
scene.AddNode(node)
2) AddRef is called, callback to managed code
3) Managed code receives the callback, adds hard reference to node
scene.RemoveAll();
5) Managed code receives callback from native ~dctor,
removes reference. GC is now able to collect empty wrapper.
6) GC collects managed wrapper
Egor Bogatov
@EgorBo
github:
โ€ข UrhoSharp
โ€ข CppSharp

More Related Content

What's hot (20)

PPTX
C++11
Andrey Dankevich
ย 
PPTX
Summary of C++17 features
Bartlomiej Filipek
ย 
PDF
Why my Go program is slow?
Inada Naoki
ย 
PDF
2018 cosup-delete unused python code safely - english
Jen Yee Hong
ย 
PDF
Basic c++ 11/14 for python programmers
Jen Yee Hong
ย 
PPTX
C++ 11 Features
Jan Rรผegg
ย 
PDF
Permute
Russell Childs
ย 
PDF
Modern c++ (C++ 11/14)
Geeks Anonymes
ย 
PPTX
C++11
Sasha Goldshtein
ย 
PDF
C++11
ppd1961
ย 
PDF
C++20 the small things - Timur Doumler
corehard_by
ย 
PDF
C++11 & C++14
CyberPlusIndia
ย 
PPTX
Fun with Lambdas: C++14 Style (part 2)
Sumant Tambe
ย 
PDF
C++ Programming - 11th Study
Chris Ohk
ย 
PDF
High performance web programming with C++14
Matthieu Garrigues
ย 
PPTX
Lambda Expressions in C++
Patrick Viafore
ย 
PDF
C++17 introduction - Meetup @EtixLabs
Stephane Gleizes
ย 
PPTX
Basic c++ programs
harman kaur
ย 
PDF
C++11 concurrency
xu liwei
ย 
Summary of C++17 features
Bartlomiej Filipek
ย 
Why my Go program is slow?
Inada Naoki
ย 
2018 cosup-delete unused python code safely - english
Jen Yee Hong
ย 
Basic c++ 11/14 for python programmers
Jen Yee Hong
ย 
C++ 11 Features
Jan Rรผegg
ย 
Permute
Russell Childs
ย 
Modern c++ (C++ 11/14)
Geeks Anonymes
ย 
C++11
ppd1961
ย 
C++20 the small things - Timur Doumler
corehard_by
ย 
C++11 & C++14
CyberPlusIndia
ย 
Fun with Lambdas: C++14 Style (part 2)
Sumant Tambe
ย 
C++ Programming - 11th Study
Chris Ohk
ย 
High performance web programming with C++14
Matthieu Garrigues
ย 
Lambda Expressions in C++
Patrick Viafore
ย 
C++17 introduction - Meetup @EtixLabs
Stephane Gleizes
ย 
Basic c++ programs
harman kaur
ย 
C++11 concurrency
xu liwei
ย 

Recently uploaded (20)

PDF
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
ย 
PPTX
For my supp to finally picking supp that work
necas19388
ย 
PPTX
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
ย 
PPTX
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
ย 
PPT
Information Communication Technology Concepts
LOIDAALMAZAN3
ย 
PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
ย 
PDF
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
ย 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
PPTX
B2C EXTRANET | EXTRANET WEBSITE | EXTRANET INTEGRATION
philipnathen82
ย 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
PDF
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
ย 
PDF
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
ย 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
ย 
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
ย 
PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
ย 
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
ย 
For my supp to finally picking supp that work
necas19388
ย 
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
ย 
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
ย 
Information Communication Technology Concepts
LOIDAALMAZAN3
ย 
>Wondershare Filmora Crack Free Download 2025
utfefguu
ย 
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
ย 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
B2C EXTRANET | EXTRANET WEBSITE | EXTRANET INTEGRATION
philipnathen82
ย 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
ย 
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
ย 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
ย 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
ย 
Building scalbale cloud native apps with .NET 8
GillesMathieu10
ย 
Ad

C++ via C#

  • 1. C++ via C# Egor Bogatov @EgorBo
  • 2. Calling C++ from C#. For what? โ€ข Lovely IDE + R# โ€ข Syntactic sugar โ€ข nuget
  • 3. Calling C++ from C#. How? โ€ข C++/CLI โ€ข COM โ€ข [DllImport], [MethodImpl] โ€ข IPC, Sockets,โ€ฆ
  • 4. Calling C++ from C#. How? โ€ข C++/CLI โ€ข COM โ€ข [DllImport], [MethodImpl] โ€ข IPC, Sockets,โ€ฆ
  • 5. UrhoSharp โ€ข ~ 200 classes โ€ข ~ 5000 methods โ€ข Same FPS โ€ข +10% memory โ€ข C# for: Windows Mac OS Android iOS tvOS
  • 6. Urho3D API class Node : public Animatable { public: Node(Context* context); void SetName(const String& name); const String& GetName(); void SetPosition(const Vector3& position); const Vector3& GetPosition(); void AddChild(Node* node, unsigned index = M_MAX_UNSIGNED); void RemoveChild(Node* node); }; public class Node : Animatable { public Node(Context context) { } public string Name { get; set; } public Vector3 Position { get; set; } void AddChild(Node node, uint index = uint.MaxValue) { } void RemoveChild(Node node) { } } Node.cpp C++ C#
  • 7. Urho3D API class Node : public Animatable { public: Node(Context* context); void SetName(const String& name); }; glue.cpp
  • 8. Urho3D API Node.cpp C# Node node = new Node(context); node.SetName("foo"); Node* node = new Node(context_); node->SetName("foo");
  • 9. Urho3D APIvoid SetName(const String& name); extern "C" DllExport void Node_SetName (Node *target, const char * name){ target->SetName (Urho3D::String(name)); } [DllImport ("mono-urho", โ€ฆ)] static extern void Node_SetName (IntPtr handle, string name); public void SetName (string name) { Node_SetName (handle, name); Node.cppNode.h binding.cpp Node.cs original API generated generated
  • 10. Urho3D API class Node : public Animatable { public: Node(Context* context); void SetName(const String& name); }; DllExport void* Node_Node (Context * context) { return new Node(context); } DllExport void Node_SetName (Node *target, const char * name) { target->SetName (Urho3D::String(name)); } public partial class Node : Animatable { private IntPtr handle; [DllImport ("mono-urho")] static extern IntPtr Node_Node (IntPtr context); public Node (Context context) { handle = Node_Node (context.Handle); } [DllImport ("mono-urho")] static extern void Node_SetName (IntPtr handle, string name); public void SetName (string name) { Node_SetName (handle, name); Node.cppNode.h binding.cpp Node.cs original API generated generated
  • 11. Binding flow Urho3D sources (*.cpp, *.h) Urho.pch Generator (Binder) binding.cpp *.cs UrhoSharp.dll Win: mono-urho.dll Droid: libmono-urho.so OSX: libmono-urho.dylib iOS: urho.framework nuget package all-urho.cpp via clang roslyn โ€ข clang โ€ข gcc โ€ข msvc clang.dll Nrefactory.dll PCL, Profile7
  • 12. Object lifecycle RefCounted: โ€ข AddRef โ€ข ReleaseRef โ€ข RefsCount โ€ข ~dctor Node node = new Node(context) 1) Allocate a native object (Node* node = new Node) scene.AddNode(node) 2) AddRef is called, callback to managed code 3) Managed code receives the callback, adds hard reference to node scene.RemoveAll(); 5) Managed code receives callback from native ~dctor, removes reference. GC is now able to collect empty wrapper. 6) GC collects managed wrapper

Editor's Notes

  • #12: Add 2/3 circles aligned in a row on a separate slide.