STL string utils

STL’s std::string doesn’t have methods for transforming the string to upper or lower case or for trimming (as other string implementations, such as CString, have). I’ve noticed that many people don’t know how to implement such functions, so I’ll show you here how to do that. class stringutils { public: static void make_upper(std::string& text) { … Read more

Create a font with the desired size

Let’s asume you want to change the font of a window\control and for the sake of simplicity let’s consider an MFC dialog application. In that case, there are several steps you should follow: declare a CFont variable in the dialog class create the font in OnInitDialog (using CreateFont) set the font to the control (using … Read more

Beware of Sequence Points

Consider the following C++ code: void foo(int a, int b, int c) { std::cout << a << ‘ ‘ << b << ‘ ‘ << c << std::endl; } int main(int argc, char* argv[]) { int value = 1; foo(value++, value++, value++); return 0; } That looks pretty straight-forward: there is a function that takes … Read more

Visual C++ 2008 Fix of Containers Swap Bug

The C++ standard, specifies in the paragraph 23.1.10 that swapping two containers should not invalidate any references, pointers or iterators refering to the elements of the containers being swapped. Unfortunatelly, that is not the case of the VC++ 2005 implementation. Imagine the following case: std::vector vec1; vec1.push_back(1); vec1.push_back(2); vec1.push_back(3); std::vector vec2; vec2.push_back(10); vec2.push_back(20); std::vector::iterator it1 = vec1.begin(); … Read more

How to Detect Available COM Ports

I’ve seen this question many times asked in forums. Unfortunatelly, not all the answers are ok. A a good wait to find the available COM ports is by using function GetDefaultCommConfig, that returns the default configuration for a specified communication device. Following code shows how to use it: #include <vector> #include <string> // this can … Read more

Update of Article “Determine Windows Version and Edition”

Recently I have updated my article published on CodeGuru about determining Windows version and edition. The update offers support for determining the Windows Vista and Windows Server 2008 edition installed on a machine. Since both these new operating systems have the same major and minor version returned by GetVersionEx, i.e. 6 for major and 0 … Read more

File Dialogs in MFC 9.0

Windows Vista redesigned the way the file dialogs look. MFC 9.0 (that will ship with Visual Studio 2008) was updated to support the new look and feel. If you compile your MFC application with VS 2008 you get the new file dialogs with no additional change. On the other hand if you run the application … Read more

Syslink Control in MFC 9.0

Visual Studio 2008 (formely code-named ‘Orcas’) has several important updates for VC++ and MFC. Among them the possibility to create syslink controls, command or split buttons and network address controls. In this post I will show how you can work with the syslink control. The control provides a way to embed hypertext links in a window. … Read more