Turn Off Monitor Power in Windows Using DDC/CI (C++)

Download Project Source for MSVC2013
Download Demo Executable

The simplest way in Windows to turn the monitor off is to call SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2);. Unfortunately this call causes the monitor to gradually fade out and since I needed the monitor to turn off immediately I had to find an alternative way.

In order to power the monitor off immediately I ended up using low level monitor configuration functions that provide access to Display Data Channel Command Interface (DDC/CI). To familiarize yourself with these functions please read Using the Low-Level Monitor Configuration Functions.

NOTE: Not all monitors support the DDC/CI power manipulation

I have created Monitors class that powers off/on all the connected monitors. Below is the sample usage:

Read more

Thread Safe Simple Logger in C++11

This logger is an exercise in C++11 thread safety and a demonstration of Return Value Optimization (RVO)/Copy elision. Even though RVO behavior is not directly prescribed by the standard, most new compilers avoid unnecessary copies and destructor calls.

This code was tested on MSVC 2013 and on MinGW GCC 4.8.1 x64. Since RVO varies between different compilers and may be affected by compiler optimization flags, the code in this post is not guaranteed to work on all systems in its current state.

My ideal logger for small scale C++ projects should have a very simple usage as demonstrated below.

Read more

Zipping/Unzipping files in C++

There seems to be very little in the way of simple zip/unzip class for C++. I have always liked the simplicity of Java implementation of ZipOutputStream/ZipInputStream and have made an implementation that is somewhat similar.

The sample project was made using Visual Studio 2010 and zlib-1.2.7. If you are using a different version of a compiler, you may need to recompile the zlib for your specific environment and use the newly compiled lib/dll in with the example project provided. You can find zlib at www.zlib.net.

Read more

Polynomial Fitting in C++ using Boost

A while back I needed to implement an algorithm utilizing an equivalent function of polyfit/polyval in LabView. The original algorithm was created in LabView for research purposes and since we were in the process of creating a commercial version of the device, the algorithm implementation in C++ needed to mirror the LabView implementation. I found that there is no simple and neat implementation of polyfit() polyval() in C++ so through a bit of digging, research, and implementation I came up with my own functions.

Polynomial fitting function returns coefficients of a polynomial representing a given data. MathWorks has a decent explanation of the function. Below is a graph showing 1000 randomly varying points and the polynomial curve in red generated from the given points.
Read more

Manipulating CSV Spreadsheet Data – C++

Often times where I have been required to deal with the data stored in a humanly readable format. I have found that the easiest and the most portable way to deal with this data is via CSV files. This way the data can be reviewed or generated using OpenOffice Calc or Excel.

The code I am posting here does not deal with all eventualities of CSV but is a solid base to include into your own projects and expand as you see fit. As a side note I have posted this code on one of my favourite coding sites full of nerdy goodness http://rosettacode.org.
Read more