Well im not good with APIs and i only know .NET though:

The way i supplied is very simple, short & easy (like my title).
Also the code i supplied works completly fine, no flaus. Unless you can find any.