boost regex, xpressive and spirit

I found a usage for regex lately, after I examined the boost.regex, I thought why not check out boost.xpressive and see what I can do with it. It’s cool! Then I found myself studying boost.spirit2,  and then boost.phoenix, both of which are in the svn trunk waiting to be released yet.

A long chain of learning, but indeed an eye-opener.  I really love the abstraction of spirit design, It takes weeks to understand it for the domain it aims at and the technology it relies on. However, once you get over that you could write very, very terse code.


WinUSB++ — Getting started

WinUSB is shipped as part of WDK, so the first step is to download and install WDK. Follow this link on how to get the WDK.

Once you properly get WDK installed, you should be finding the WinUSB specific header files under C:\WinDDK\6001.18002\inc\wdf\umdf\10:


and lib files for linking under C:\WinDDK\6001.18002\lib\wlh\:


redistrib files can be found under D:\WinDDK\6001.18002\redist\winusb.

Now you are ready to develop your application using WinUSB.

WinUSB++, wrapping WinUSB

WinUSB is an attempt from Microsoft to ease USB applications on Windows XP and above.  Though it’s been around for quite a while, and seemingly MS has done a good job, I haven’t seen it widely used. I am guessing it’s attributed to the fact that many proprietary home-made drivers already existed and worked fine before it’s even developed. Vista’s marketing failure to rule out XP is another factor that people has no stimulation to develop new drivers based on WinUSB just because it’s new.

However, with the imminent Win 7 and 64-bit era, many existing XP-base driver may have to be upgraded, ant chances are more WinUSB based drivers will appear.

A few months ago, with some ST7 based devices at hands,  I spent some spare time investigating WinUSB and having ported from the ST USB Driver for Tube device v2.0.3.

In my rough test, WinUSB is a little bit slower that STTub,  some similar transaction cost about 0.36s with  STTub, but 0.40s with WinUSB. Not quite surprising as WinUSB is more generic.

The other day, when I was cleaning up the PC, I found this earlier attempts being covered with dust. I was feeling like sharing it out would be nicer.

In the future,  I’ll being finding some time and talking about how to

1) wrap this C style API thing into a C++ interface and

2) overcome its major limits – no asynchronous access to this drive, which means if you have two WinUSB based application, you cannot let them talk to WinUSB at the same time.



Branch to Blogger

As live space is often blocked by the corpoarate firewall,I created a branch at Blogger.

convert hex string to int values in C++

boost::lexical_cast is quite handy to convert between string and numerical types. However, sometimes you need more. for example, when you want to convert a string "0x0A" to its int value 0x0A(10), or vice versa. you are out of luck. The following code comes handy for this purpose.

#pragma once
#include <string>
#include <sstream>
#include <iomanip>

// the following routines convert dec/hex string literal to size_t values

namespace numeric_conversion
    template <typename T, bool is_hex>
    size_t string_to_size_t(const std::basic_string<T>& s)
        std::basic_istringstream<T> ss(s, std::ios_base::in);

        size_t size(0);
        ss >> (is_hex ? std::hex : std::dec )>>size;
        return size;

    template <typename T>
    size_t hexstring_to_size_t(const std::basic_string<T>& s)
        return string_to_size_t<T, true>(s);

    template <typename T>
    size_t hexstring_to_size_t(const T* s)
        return string_to_size_t<T, true>(std::basic_string<T>(s));

    template <typename T>
    size_t decstring_to_size_t(const std::basic_string<T>& s)
        return string_to_size_t<T, false>(s);

    template <typename T>
    size_t decstring_to_size_t(const T* s)
        return string_to_size_t<T, false>(std::basic_string<T>(s));


    template <int N>
    std::wstring to_hex_string(size_t i, bool map_0_to_empty)
        if(map_0_to_empty && (0 == i)) return L"";

        std::wstringstream wss;
        wss << L"0x"/*std::showbase*/ << std::hex << std::setw(N) << std::setfill(L'0') << i;

        return wss.str();

Test code:
#include <iostream>
int main()
	using namespace std;
	using namespace numeric_conversion;
	wcout << L"convert string with base: 0x0a = " << hexstring_to_size_t(L"0x0a") << endl;
	wcout << L"convert string with base no base: 0a = " << hexstring_to_size_t(L"0a") << endl;	
	wcout << L"convert value to hex string: 0a = " << to_hex_string<2>(10, true);


Duplicate File Finder

I am in need of a tool to find the duplicate files on my computer. I would have write one myself with boost.filesystem, but I searched online first and found this free code.  By tweaking a little with this code. I added the delete feature.


Binary can be downloaded from here.

Boost.Thread, Condition Variables &Spurious wakeup

 boost::condition_variable is a utility to synchronize threads, and can be used as below:

boost::condition_variable cond;
boost::mutex mut;
bool data_ready;

void process_data();

void wait_for_data_to_process()
    boost::unique_lock<boost::mutex> lock(mut);

This example is simple and intuitive except for one thing, according to this documentation,

 void wait(boost::unique_lock<boost::mutex>& lock)


Atomically call lock.unlock() and blocks the current thread.

The thread will unblock when notified by a call to this->notify_one()

or this->notify_all(), or spuriously.

When the thread is unblocked (for whatever reason),

the lock is reacquired by invoking lock.lock() before the call to wait returns.

The lock is also reacquired by invoking lock.lock()

if the function exits with an exception.

Since  Wait() blocks the thread, why in the above example code snippet


rather than


is used?

This is because a spurious wakeup may cause cond.wait(lock) to return while data_ready remains unchanged(false).

This post gives a very detailed discussion about this issue.

How to link boost 1.36 against VC2008 SP1 CRT

For reasons described here, by default, VC 2008 would link all runtime dependencies to the OEM version, which is "9.0.21022.8", even if SP1(version=’9.0.30729.1′) is installed.


This behavior can be changed by defining _BIND_TO_CURRENT_VCLIBS_VERSION in the project setting.


However, if your project links to a 3rd-party library(for instance, boost) which is build without the macro be defined. your final executable will end up linking to two versions of Runtime, OEM and SP1, at the same time.


To build boost with the SP1 runtime, you need to go to the jam file of each library and add this macro,  and rebuild it.

Take boost.thread as an example, edit the jamfile.v2  at \libs\thread\build and insert one line.


Build the library again. Done.

The same method is applicable for other Boost libraries such as Filesystem.

How to get the Edit control of a ComboBox?

Find the item with ID = 1001, e.g. m_ctrlCbo.GetDlgItem(1001).

Couldn’t be easier!

Don’t bother with ComboBoxEx, ‘cos there’s a member function in the MFC wrapper class already.


In a recent project, I chose WTL as the UI frameworks. Nice.