Calling C functions from Python – part 1 – using ctypes

Recently I’ve been evaluating Python interop technologies for a project at work and I think it’ll made an interesting blog series. Let’s say your have following C code (add extern “C” if you are in C++ land) and compile it into a dynamic library (dll/.so/.dylib): int Print(const char *msg) { printf(“%s”, msg); return 0; } int Add(int…


Calling node.js sqlite callback function using promise and await

Callback pain I was experimenting with sqlite package with node.js the other day and the async programming style is rather painful, so I did some experiment to wrap it with promise + async/await to make it easier to use. This post shows you how to do that. For example, let’s say I’m working with a database with…


C++ coroutine tutorial

I’ve been experimenting with coroutines recently and I found that information on C++ coroutines are very difficult to find. I’m planning to write a series of C++ coroutine blogs on how to use C++ coroutines, how they work, and how to write your own library that uses C++ coroutines. My last post Basic Concepts are…


Writing your own lock-free reader/writer lock

It is sort of puzzling for me why C++ standard doesn’t have a implementation of reader/writer lock. So just like every other C++ developer, I’ve decided to roll my own.  A typical implementation might be using semaphore / mutex. However, this has a major disadvantage that it requires entering a lock mutex every time a…


Sharing .NET generic code under the hood

If you come from a C++ programming background, you are most likely already familiar with C++’s template code bloat problem. Each template instantiation gets its own copy of the code (of course, compiler/linker can optimize by throwing away unused methods). The reason being that C++ template are more like C macro on steriods. I know…


C# value type boxing under the hood

I recently had some really interesting discussion with a .NET typesystem expert in the team, and during the conversation he had pointed out an interesting aspect of .NET value type boxing when using constraints. Intrigued by that discussion, I decided to take a further look. See the full article here in my new blog  


Moving my blog to GitHub pages –

I haven’t posted in a good while – it’s because I’ve been busy working on .NET native! Now that things has settled down for a bit, I’ve decided to resume writing posts. After spending some time thinking, I’ve decided that I prefer writing my blog posts in plain text/markdown and working on Git repos, rather…


Getting rid of TypeLoadException in your WinRT app

I’ve seen enough people asking about this so I thought I should talk about this in my blog. They see strange first-chance TypeLoadException happening in their application but it doesn’t seem never cause any problem (app doesn’t crash) and everything still seem to work ‘fine’. This seem to happen mostly if you have a C++…


Async talks on Channel 9

Even though I work on .NET interop and WinRT these days, async/await is still the one topic that gets me confused from time to time. It’s a great concept logically but difficult to grasp once you start thinking about how the actual code flow goes (and all hell breaks loose once you start debugging the…


Avoiding C++ types in your P/invokes

It’s easier to explain this in an example. Let’s say you have a export function in your native DLL: void WINAPI MyFunction(_com_ptr_t<IMyInterface> ptr) { // … do something with ptr ptr->Func(); } And the DllImport would be something like this: [DllImport("mydll.dll")] void MyFunction(IMyInterface ptr); Everything seems to be perfectly valid, except that there is a…