SafeBuffer and UnmanagedMemoryStream

At work I have a situation where I have some binary data allocated in the native code. It’s pretty much a raw char*. I then would like to access that same information from the managed side. But how? Of course I can just copy the data to a byte[] but that’s just wasteful.

I did some googling around and found the obvious solution – UnmanagedMemoryStream. It can take pointers, or a SafeBuffer. The latter is basically a smart wrapper around memory handle. Take a look at the code I came up with. I hope somebody will find it useful. It’s still work in progress and can use some love, so any comments are welcome.

I needed to pass the std::unique_ptr as r-value, otherwise the linker complains (thunks for non-existent copy constructor). I still need to clean this code up to handle custom deleters. But it serves my needs for now.

Edit: There is a bug in this code. Can you spot it? 😉

HID Card and Android phone

At my workplace we use HID ProxCard II cards to enter the building and to open individual doors. Everybody has a card with their picture printed on it. The problem is that I keep forgetting mine, plus I’ve had it for such a long time that the card started to disintegrate and it looks rather nasty.

HID card

I really wanted to just use my Android phone in lieu of the card, but it seems like HID cards are not compatible with NFC 🙁
This of course wasn’t going to stop me. Somehow I wanted to “fuse” the card with my phone. If my phone had a plastic case or cover, I could just slip the card inside of it. But I own an HTC Once and since it’s made out of aluminium and gorilla glass, I don’t really need (or want) a cover for it. I decided to use a sticker to attach the card instead. I ordered a custom-fit sticker from SkinIt with a cute picture of my boys on it. Now I could finally start my project 🙂

Costom skin for HTC One


First I cut off a small strip of plastic along every edge of the card, starting with the top. Then I removed the front layer with the printed picture. It was clear that there were two additional layers to the card. Finally, I carefully separated the two remaining layers, exposing a copper coil and something that looked like a microchip.

Removed sticker and cut sides



Next step was to affix the layer with coil to the back of my HTC One. I chose not to remove the coil from the plastic it was glued to, and instead to attach the whole thing to the back of the phone with scotch tape. Of course I rounded it off a little bit witch scissors, just to make it look better.


Coil from HID card affixed to back of HTC One

The final step was to place the SkinIt sticker over the back of the phone. Sadly that didn’t go as smooth as I hoped. Since the coil and microchip are not perfectly flat, you can see a small bulge. It will take some time to get used to it, but ultimately it should not be a big deal. A small price to pay for the convinience.
Here is a photo of the finished product. Now I just need to remember to carry my phone around with me at all times at work 😀

2013-10-07 17.56.12

NFC Extravaganza

What’s cool about the new Android phones is that they support Near Field Communication (NFC) technology. This was actually one of the reasons I got my HTC One.


I can now use Google Wallet to pay with my phone, simply by tapping on NFC-enabled credit card terminal. Hopefully soon every point of sale will be accepting NFC payments, especially after Apple jumps on board and ships iPhones with NFC.

NFC payment

Tap to pay is pretty cool, but I was curious what else NFC can be used for. There is the obvious tap to share pictures, or contacts, but what else?

I did some googling and came across this website: TagStand not only sells physical NFC tags, but also provides neat Android apps for interacting with the tags. One can read and write data to NFC tags, and later use them as triggers to execute various actions. What does that mean? What can it be used for? Well, check this out.

NFC-enabled sticker

Car mode

Every time I get into my car I turn on navigation and Bluetooth, so that I can listen to music through car speakers. I also turn on the “car dock” app, so that the screen doesn’t lock while I’m driving. Of course when I leave the car I need to remember to turn all that stuff off. Pretty repetitive… NFC to the rescue!

I got myself several NFC stickers from TagStand and installed NFC Task Launcher app to my phone. I placed a circular, water-proof tag right next to the gear shifter. Now all I have to do is to tap the sticker once I get into the vehicle.

2013-10-07 15.51.27

The NFC Task Launcher app is programmed to execute these tasks when phone is near my “car mode” tag:

Car mode taskIt’s pretty awesome. I don’t need WiFi, since I’m on the road. But I want GPS, Bluetooth, car dock, loud volume. Just for fun I have the phone say “Car mode activated. Have fun but drive carefully.” The only thing to watch is space. The NFC tag I’m using has only 144 bytes 😉

I also use my car’s Bluetooth as a trigger – when the phone detects car is no longer connected, it will execute the following steps:

Out of car

I never need to worry about doing this manually again 😀 Next I need to consider buying an NFC-enabled cradle; then I think the project will be complete.

NFC cradle

Other possible uses

I can picture placing the tag at work for time-keeping reasons, or at the church entrance. First tap to activate the “church mode”, second tap to disable it (vibrate mode, Bluetooth off, GPS off, WiFi off, Bible app on). Although for these you could use a “geo-fence”, but constantly checking location brains the battery.

I promise to write more once I come up with any more clever uses for the NFC awesomeness.


Edit: I was hoping to use my phone’s NFC as smart HID card at work, but the frequencies do not match. Came up with an alternate solution.

Update: I found a good website ( which offers cheap NFC stickers, as well as high quality custom made NFC business cards. Definitely a must for high tech companies 😉

Hello (and goodbye) Tumblr, WordPress it is

After years of using, I decided I’m tired of hosting my own content and dealing with the lack of themes.

There were many contenders for replacement, but ultimately I settled on Tumblr. It’s easy, let’s me use custom domain name for free and, ever since it got acquired by Yahoo, it seems fast enough.

The only downside was importing content. I was very surprised it does not support BlogML! That’s just crazy. I had to hand code a simple importer in Python. Not a big deal, but still… Unfortunately all my permalinks are gone 🙁
Not the end of the world, since I don’t have that much content.

I guess we will see how it goes. I can always switch to something that gives me more flexibility.

Edit: And so it happened. I didn’t really like the way editing of posts happens on Tumblr, especially that it’s difficult to insert images into text posts. So I decided to switch to WordPress. Still didn’t bother with the permalinks (someday I will remap them), but I’m quite happy for now.