Posts Tagged P/Invoke

C++ For C# Developers: Part 37 – Missing Language Features

Tags: , , , , , ,

We’ve covered all the features in the C++ language! Still, C# has some features that are missing from C++. Today we’ll look at those and explore some alternatives to fill these gaps.

Read the rest of this article »

4 Comments

C++ For C# Developers: Part 33 – Alignment, Assembly, and Language Linkage

Tags: , , ,

Today we’ll explore some of the lower-level concepts in C++. These are tools that get brought out of the toolchest when performance really matters and interoperability is paramount. Read on to learn about C++’s escape hatches and take fine-grained control over memory!

Read the rest of this article »

No Comments

P/Invoke in Burst: No Safety Net

Tags: , ,

Calling into native code like C++ from C# is a powerful interoperability tool in Unity. As we move more and more code out of Mono and IL2CPP and into Burst, will we still have this tool available? Today we’ll find out!

Read the rest of this article »

4 Comments

A Journey Through a P/Invoke Call

Tags: , ,

Today we’ll go through everything that happens when you make a P/Invoke call. We’ll see how native libraries are loaded and how marshaling works. We’ll touch on calling conventions and character sets. In the end, we’ll have a better understanding of what goes on when we call into native code.

Read the rest of this article »

3 Comments

How to Wrap a Real Native Library

Tags: , , ,

There are many posts that’ll tell you the “hello world” of calling native code like C++ from a C# Unity project. These tend to be simple examples though, so it’s hard to go from that to wrapping a real life useful native library. Today we’ll wrap SQLite, a popular C library that implements a database, and talk about the challenges in doing so and how to end up with a pleasant C# API for the rest of the game to use. Read on to learn how!

Read the rest of this article »

5 Comments

Exotic CPU Instructions

Tags: , ,

Programming in high-level languages like C# often presents the illusion that the CPU is only capable of a few primitive operations like “add,” “multiply,” “push,” “pop,” and “move.” After all, those are the primitive operations that we write all of our C# code with. The reality is quite different. Modern CPUs have hundreds of instructions for tons of special-purpose operations. Entire algorithms in C# are built right into the CPU and can be executed with one instruction. Today we’ll look at some of these exotic instructions as a reminder of what CPUs can really do and see how we can tap into this potential.

Read the rest of this article »

4 Comments

C++ Scripting: Part 29 – Factory Functions and New MonoBehaviours

Tags: , , , ,

Since their introduction in part 7, support for C++ MonoBehaviour messages has always been a special case. The reason for this was that we didn’t have good enough support for what I’m calling “factory functions.” These are functions like GameObject.AddComponent<T> that instantiate a generic type. This week we’ll go over why that support was lacking, what was done to fix it, and how the new system works.

Read the rest of this article »

No Comments

C++ Scripting: Part 21 – Implement C# Properties and Indexers in C++

Tags: , ,

Part 19 of this series started to allow our C++ game code to derive from C# classes and implement C# interfaces. The first step was to override methods as they’re the most common. Today we’ll tackle the second-most common: properties. We’ll also handle indexers, which are like properties with more parameters. Read on to see how to use this and how it works behind the scenes.

Read the rest of this article »

No Comments

C++ Scripting: Part 20 – Performance Improvements

Tags: , , ,

The last time we looked at performance was way back in part four of the series. Ever since then we’ve been relentlessly adding more and more features to the C++ scripting system. So today we’ll take a break from feature additions to improve the system’s performance in a couple of key areas.

Read the rest of this article »

2 Comments

C++ Scripting: Part 16 – Events

Tags: , , , ,

Last week’s article covered delegates, so it’s only natural that we follow up this week by covering events. Supporting delegates has laid a good foundation for supporting events, so let’s dive in and see how to implement and use them in C++.

Read the rest of this article »

No Comments