Check directory or folder empty status

Use function PathIsDirectoryEmpty. Some working facts about this API… Returns false if directory does not exist Returns true is directory is empty Return false if directory contains nested directories but no files Returns false if there are files in directory Returns false if given path points to a directory C: is considered a directory Does […]
Continue reading…

 

Random shuffling of elements in STL containers

Use stl algorithm random_shuffle. This function has two overloads, first version randomizes using it’s own version of random number generator, second function takes a custom random number generator from our side. [sourcecode language=”cpp”]void TestShuffle() {     typedef short VT;     typedef std::vector VTVector;     // Count of elements     const VTVector::size_type Size = 10;     […]
Continue reading…

 

Capturing mouse input

There are two functions which helps in capturing mouse input, it’s called SetCapture and ReleaseCapture and a helper function called GetCapture which returns a handle to the capture window associated with the current thread. If no window in the thread has captured the mouse, the return value is NULL. Why do we need to capture […]
Continue reading…

 

Set resource handle in MFC and ATL

When working in MFC/ATL and then using a resource dll can at times cause bugs because resource handle is not properly set. For e.g. if you have a dialog resource in a dll and you instantiate a CDialog which results in an error because resource cannot be located in this application or dll since it’s in […]
Continue reading…

 

Associate a path with a drive letter

Use SUBST command… [sourcecode language=”cpp”]SUBST [drive1: [drive2:]path] SUBST drive1: /D drive1:        Specifies a virtual drive to which you want to assign a path. [drive2:]path  Specifies a physical drive and path you want to assign to a virtual drive. /D             Deletes a substituted (virtual) drive.[/sourcecode] Type SUBST with no parameters to display a list of current […]
Continue reading…

 

AfxGetMainWnd in worker threads

Recently I faced an issue in one of my project code bases. There is a worker thread (created using CreateThread native API) which calls AfxGetMainWnd() MFC function and since worker threads doesn’t have a main window associated with it, CWnd::Create( …WS_CHILD… ) call fails. A window created with WS_CHILD needs a parent window but AfxGetMainWnd() […]
Continue reading…

 

Optimal swapping of stl containers

Never use a temporary variable to swap two stl containers instead always use std::swap function. This function has different overloaded versions specialized in swapping every standard container. So what’s the difference if we use a temporary variable swap and std::swap. The idea is that basically all stl containers use dynamic memory to store data, so […]
Continue reading…

 

Changing scheduling priority of a process at runtime

I had a requirement sometime back to make my process have a higher priority at runtime, so went hunting around for an API which does this and found one with an intuitive name, “SetPriorityClass”. So what is process priority? Process priority is the value which determines priority of a process when it’s being scheduled. If […]
Continue reading…

 

How to handle child window messages in a parent window?

Is there any way to handle child window messages in a parent window? Yes you can use WM_PARENTNOTFIY message or OnParentNotify MFC handler. Note that this message is sent or this function is only called when WS_EX_NOPARENTNOTIFY style is not set. A child controls notifies it’s parent window on… Creation Destruction Mouse button clicks Please […]
Continue reading…

 

back_inserter explained

back_inserter stl iterator class is an adapter class. So what is an adapter? If you buy a laptop in India and then take it to the US/Europe, plugs given won’t fit into the sockets there, so what we do is buy an adapter, which adapts our laptop’s plug to the sockets there. Adapter pattern based […]
Continue reading…