Category Archives: C#

Fast shared Array, Buffer and Circular Buffer / Ring Buffer for .NET IPC with Memory Mapped Files

About the SharedMemory library

The SharedMemory class library provides a set of C# classes that utilise a non-persisted memory-mapped file for very fast low-level inter-process communication (IPC) to share data using the following data structures: buffer, array and circular buffer (also known as a ring buffer).

SharedMemory uses the MemoryMappedFile class introduced in .NET 4, and provides a partial P/Invoke wrapper around the Windows API for versions prior to this. The library was originally inspired by the following CodeProject article: “Fast IPC Communication Using Shared Memory and InterlockedCompareExchange“.

The library abstracts the use of the memory mapped file, adding a header that includes information about the size of the shared memory region so that readers are able to open the shared memory without knowing the size before hand. A helper class to support the fast copying of C# fixed-size structures in a generic fashion is also included.

The project source can be found on GitHub, with binaries available via Nuget.

What is a memory mapped file?

A memory-mapped file is an area of virtual memory that may have been mapped to a physical file and can be read from and written to by multiple processes on the same system. The file or portions thereof are accessed via views to one or more processes as a contiguous block of memory. Each view can then be read/written to using a memory location.

A memory-mapped file can be created as one of two types:

  • persisted: mapped to a physical file, and
  • non-persisted: in-memory only,  commonly used for inter-process communication (IPC).

A memory-mapped file is referenced using a unique name, unlike named pipes however a memory-mapped file cannot be accessed directly over a network.

Continue reading Fast shared Array, Buffer and Circular Buffer / Ring Buffer for .NET IPC with Memory Mapped Files

x86/x86-64 Disassembler for .NET – SharpDisasm

About the disassembler

SharpDisasm is a full C# port of the libudis86 x86/x86-64 disassembler. Disassembly of 16-bit, 32-bit and 64-bit instructions can be output to both Intel and AT&T syntax.

The library supports decoding each instruction to an object that provides access to low-level information for further analysis (e.g. instruction size, operand number and types etc).

SharpDisasm is a full C# port of the libudis86 C-library disassembler.

Disassembler features

  • a full C# port of the libudis86 C library
  • a set of simple C# classes wrapping the udis86 C API
  • support for x86 16-bit, 32-bit and 64-bit instruction set architectures
  • support for outputting in Intel and AT&T syntax
  • support for all x86 and x86-64 (AMD64) General purpose and System instructions.
  • support for the following ISA extensions:
    • MMX, FPU (x87), AMD 3DNow
    • SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, AES

The disassembler is exposed through the SharpDisam.Disassembler class.

Output from the provided example console app:

C:\>echo a1 c9 fd ff ff a1 37 02 00 00 b8 37 02 00 00 b4 09 8a 
25 09 00 00 00 8b 04 6d 85 ff ff ff 89 45 f0| disasmcli 32

00000000 a1 c9 fd ff ff mov eax, [0xfffffdc9]
00000005 a1 37 02 00 00 mov eax, [0x237]
0000000a b8 37 02 00 00 mov eax, 0x237
0000000f b4 09 mov ah, 0x9
00000011 8a 25 09 00 00 00 mov ah, [0x9]
00000017 8b 04 6d 85 ff ff ff mov eax, [ebp*2-0x7b]
0000001e 89 45 f0 mov [ebp-0x10], eax

C:\>echo 488b05f7ffffff67668b40f06766035e1048030425ffff
000067660344bef04c0384980000008048a10000000000800000 | disasmcli 64

0000000000000000 48 8b 05 f7 ff ff ff mov rax, [rip-0x9]
0000000000000007 67 66 8b 40 f0 mov ax, [eax-0x10]
000000000000000c 67 66 03 5e 10 add bx, [esi+0x10]
0000000000000011 48 03 04 25 ff ff 00 00 add rax, [0xffff]
0000000000000019 67 66 03 44 be f0 add ax, [esi+edi*4-0x10]
000000000000001f 4c 03 84 98 00 00 00 80 add r8, [rax+rbx*4-0x80000000]
0000000000000027 48 a1 00 00 00 00 00 80 00 00 mov rax, [0x800000000000]


Download source from GitHub or download binaries on NuGet.

Direct3D Rendering Cookbook is published!

After 8 months of hard work, I am now a published author! It has been a challenging but rewarding experience and I’m really excited to finally see it published. I hope people find it as enjoyable to read as I found it to write.

If you’re interested in taking a look, there is a sample chapter available on the book’s page at Packt Publishing or from Amazon (Direct3D Rendering Cookbook) where you can choose to purchase the print and/or e-book editions.

Direct3D Rendering Cookbook (font cover)

Continue reading Direct3D Rendering Cookbook is published!

Visual Studio graphics content pipeline for C# projects

In this post we will look at how to use the graphics content pipeline for C# in both Visual Studio 2012 and Visual Studio 2013 for Desktop and Windows Store apps.

Since Visual Studio 2012 there has been a new graphics content pipeline and graphics debugger – including a DirectX frame debugger and  HLSL debugger. The graphics content pipeline provides a number of build targets for converting common 3D and graphics assets into a usable format for DirectX applications, this includes the compilation of common mesh formats such as Collada (.dae), AutoDesk FBX (.fbx), and Wavefront (.obj) into a compiled mesh object (.cmo) file, and converting regular images into .DDS files.

Unfortunately the graphics content pipeline tasks don’t work out-of-the-box with C# because the MSBuild targets are not compatible.

Continue reading Visual Studio graphics content pipeline for C# projects

Afterglow – an ambient lighting solution

I have long wanted to build an ambient lighting solution (e.g. like Ambilight)  and at last it is starting to take shape with Afterglow, a solution built by myself and Jono.

Afterglow provides a framework to perform image capturing, colour extraction, post processing and colour output – primarily for use with lighting systems such as for ambient lighting.

The Afterglow project is designed to be an input and output agnostic framework, allowing users to create their own plugin’s to read images from the screen or perhaps a camera and to output to their own hardware solution. One idea, unrelated to existing ambilight-type solutions, is to build a sound analysis plugin that provides mood lighting for a room.

The hardware solution we used is a LED pixel string driven by an Arduino Uno. You can see how to build the hardware here, and see images of the output at the Afterglow Gallery. This uses a small Arduino program listening for serial input sent by an output plugin within Afterglow.

The reason I am interested in building an ambient lighting solution is to drive the lighting by images captured while playing a game. Traditionally ambient lighting systems have been designed to work with movies / TV whereas Afterglow is a more flexible environment that can accept any input source. This is beginning to be realised within Afterglow with the recent addition of a DirectX capture plugin.

It is still early days for the project with a long to do list and plenty of issues, but if you are after an easy to build, extensible ambient lighting solution Afterglow is a good start! Anyone interested in participating in the project or building custom plugin’s is welcome!

MonoTouch: Auto scroll UITextField or other views hidden by keyboard

Here is a snippet that I use in my base view controller class to handle scrolling views that would be hidden by the keyboard when it appears. Any view within a UIScrollView can be made to scroll automatically to be visible when the keyboard appears over it.

This is based on the following Apple documentation example: Managing the Keyboard: Moving Content That Is Located Under the Keyboard.

Continue reading MonoTouch: Auto scroll UITextField or other views hidden by keyboard

C# – Screen capture and Overlays for Direct3D 9, 10 and 11 using API Hooks

So it’s been almost a year and I have finally got around to finishing a new version of my screen capture project that supports Direct3D 9, 10, and 11! This solution still uses SlimDX for the Direct3D API wrapper along with EasyHook to perform the remote process hooking and IPC between the host process and target process.

Some of the changes since the previous version:

  1. 100% C# implementation
  2. Added Direct3D 10 and 11 support
  3. Capturing multi-sampled/anti-aliased images (for 10 & 11) is supported
  4. Re-organised code making it easier to support multiple D3D versions
  5. Implemented a new and improved test bed application
  6. Provided example overlays for D3D 9 and 10
  7. Improved debug messaging from Target application to Host (mostly removed when compiled with “Release” configuration)

Update 2012-04-14: code now hosted on Github

Continue reading C# – Screen capture and Overlays for Direct3D 9, 10 and 11 using API Hooks

C# – Screen capture with Direct3D 9 API Hooks

Since investigating screen capture techniques for Direct3D 9 applications a year ago I have wanted to look into hooking the Direct3D 9 API to utilise the much faster GetBackBuffer for my screen captures. Well here it is at last – a mostly managed C# solution providing easy and safe hooking of the Direct3D 9 API thanks to EasyHook, supporting both 32-bit and 64-bit.
Continue reading C# – Screen capture with Direct3D 9 API Hooks