Starting command prompt in a particular directory

What I mean is, if we type ‘Start->Run->Cmd.exe’ then command prompt directory is our user directory, for e.g. ‘c:Documents and settingsNibu’. So how can we do this through code is the question? Directory that we see when command prompt starts up is called working directory. Every executable in windows has a working directory which is […]
Continue reading…

 

How to retrieve special folder paths, for e.g. my documents?

SHGetFolderPath is the API that we use to retrieve special folder paths.  Some examples of special folders are as follows… My Documents My Pictures Windows directory System directory Program files Internet history directory Cookies directory etc… So here is a small useful function which retrieves path of any special directory documented for SHGetFolderPath API in […]
Continue reading…

 

Increase blog hit count

Recently a fellow blogger asked me my help on increasing blog hit count, so here are some tips from my blogging experience… Always post contents that genuinely/honestly interests you so that others will also feel interested. Be honest with what you write, don’t write to grab attention. You may succeed otherwise for a short period […]
Continue reading…

 

Functions for converting strings to upper/lower case

Use following functions _strupr _strlwr std::transform – does the trick too but hard to understand [sourcecode language=’cpp’]/* STRLWR.C: This program uses _strlwr and _strupr to create  * uppercase and lowercase copies of a mixed-case string.  */ #include #include void main( void ) { char str[100] = “The String to End All Strings!”; printf( “Mixed: %sn”, […]
Continue reading…

 

How to display notational base for a number?

Well what the heck do you mean by notational base? It just means the prefix we give for hexadecimal and octal numbers. So in C++ there is an io manipulator called ‘showbase’ which shows the base and ‘noshowbase’ which hides the base from display. An e.g. from MSDN… [sourcecode language=’cpp’]#include int main( ) {    […]
Continue reading…

 

How to create protected/readonly pages?

Ever wondered if at all it’s possible to allocate read only chunk of memory. Now you may ask why we would  need to do something like this, well may be just for security reasons. So ideally what happens when we try write to read only memory? We typically get a message box which says… ————————— […]
Continue reading…

 

How to force show/hide + sign for numbers?

We know that ‘-‘ i.e. minus is always displayed for numbers but how about ‘+’ sign. Is there a way to show this sign using output streams in c++? The answer is yes! Use “showpos” function and use “noshowpos” to disable this functionality… [sourcecode language=’cpp’]int main() {    cout
Continue reading…

 

Writing ‘endl’ like functions for output streams

Ever wondered how endl works! Quite simple, endl is just a function with a predefined signature and output streams has ‘operator <<‘ overloaded to take function pointers which satisfy this particular signature, so whenever it finds such a function the compiler just calls this operator with argument set to pointer to this function which in […]
Continue reading…

 

How to change console application text color?

Easy! call SetConsoleTextAttribute function with appropriate color codes as follows… [sourcecode language=’cpp’]// A generic function call to set color for text being output void SetColor( const int Color ) { SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), Color ); } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { // Only change foreground color SetColor( FOREGROUND_BLUE ); cout
Continue reading…

 

How to check for keyhit in a windows console application?

For this purpose we use _kbhit function included in conio.h file. Please note that conio.h is a non-standard header file so can’t say if _kbhit will be available in other libraries. The purpose of  _kbhit is to check whether a key has been hit and so then we can use this function to wait till […]
Continue reading…