Visual C++ Tips
Gold mine of Visual C++ tricks!
Gold mine of Visual C++ tricks!
Mar 4th
In multi-threaded environments, sometimes we need to check whether a given thread is alive or not. But how can we check it?

Picture courtesy – Erica Marshall
![]()
You can use the function – GetExitCodeThread(). If thread is alive, the function returns STILL_ACTIVE. Have a look at the code snippet.
// Checks whether given thread is alive.
bool IsThreadAlive(const HANDLE hThread, bool& bAlive )
{
// Read thread's exit code.
DWORD dwExitCode = 0;
if( GetExitCodeThread(hThread, &dwExitCode))
{
// if return code is STILL_ACTIVE,
// then thread is live.
bAlive = (dwExitCode == STILL_ACTIVE);
return true;
}
// Check failed.
return false;
}
void main()
{
bool bAlive = false;
if( IsThreadAlive( GetCurrentThread(), bAlive))
{
// IsThreadAlive is success.
// Now check whether thread is alive.
// It should, because its our main thread.
if( bAlive)
{
std::cout << "Thread Alive!!!";
}
}
}
Feb 23rd
I just want to introduce a new tool that I met recently. Its the CppDepends! its a wonderful tool which can do static code analysis and generate lot of metrics. It has got CQL( Code Query Language) by which you can write sql like queries to get code metrics.

Have a look at the complete feature list. You can download the tool from here. I think its worth a trial. Try it!
Feb 11th
I still remember my first project which I did seven years back. In that I got a task to split a string which contains ids separated by slash. I wrote a big snippet of string parser code by using pointers. But now when i see the following tricks, i feel – how childish was my first code snippet.

![]()
Some of the tricks are as follows.
1) strtok()
If you want plain api and no object oriented fanciness, then strtok is for you. Download the source from here.
#include "string.h"
...
// String to be splitted.
char String[] = "Long.Live_Visual.C++";
// Seperators.
char Seperators[] = "._";
// Start Tokenizing.
char* Token = strtok(String, Seperators);
// Loop until end.
while(Token != NULL)
{
cout << Token << endl;
// Tokenize the remaning string.
Token = strtok(0, Seperators);
};
2) istringstream
C++ Streams are good option for string splitup. But it has only one drawback – one one delimiter can be specified. Download the source from here.
#include "iostream"
#include "sstream"
#include "string"
...
string String = "Long.Live.Visual.C++";
char Seperator = '.';
// Create input string stream.
istringstream StrStream(String);
string Token;
while(getline(StrStream, Token, Seperator))
{
cout << Token << endl;
}
3) CString::Tokenize()
Wanna MFC way? Then CString::Tokenize() is for you. Download the source from here.
// String.
CString String = _T("long.live_Visual.C++");
// Token seperators.
CString Seperator = _T("._");
int Position = 0;
CString Token;
// Get first token.s
Token = String.Tokenize(Seperator, Position);
while(!Token.IsEmpty())
{
wcout << Token.GetBuffer() << endl;
Token.ReleaseBuffer();
// Get next token.
Token = String.Tokenize(Seperator, Position);
}
![]()
Do you know any other mind blowing tokenizing tricks? Then, share with us.
Feb 10th
Do you remember the old days, where memory was a premium? At that time, unions were used to save memory by merging multiple variables. Gone are the days where we had memory constraints. Now we have GB’s of RAM itself. So are unions just overhead to language now? Or is it still useful?

![]()
The merging property of unions can be used for parsing values. For instance have a look at the MessageParser below. If you want to deal with byte streams with specific message format, then unions will be really helpful.
#pragma pack(1)
// Message layout.
struct MessageLayout
{
char Signature[3];
WORD HeaderLen;
WORD Param1;
BYTE Param2;
};
// Union to parse header from byte streams.
union MessageHeaderParser
{
// Byte stream.
BYTE Bytes[8];
MessageLayout Layout;
};
int _tmain(int argc, _TCHAR* argv[])
{
// DWORD Parser.
BYTE Bytes[] = { 'M','Z', 0, // Header Signature.
10,0, // Header Length
20,0, // WORD param
30 }; // BYTE Param.
MessageHeaderParser Parser;
memcpy(&Parser.Bytes, Bytes, 8);
// Get the HIWORD by using standard windows macro.
cout << "Signature :" << Parser.Layout.Signature << endl;
cout << "Msg Length :" << Parser.Layout.HeaderLen << endl;
cout << "WORD param :" << Parser.Layout.Param1 << endl;
cout << "BYTE param :" << (int)Parser.Layout.Param2 << endl;
_getch();
return 0;
}
Download the code from here.
BTW, Do you know that the famous Audi brand is a union of 4 old legendary car companies? The four rings represents each of the four companies. Interesting, the history is. isn’t it?
Feb 8th
Do you want to set the focus to another control on displaying Dialog? Or tried SetFocus() to another control in OnInitDialog() and want to know why its not working? The answer for your ‘Focus’ question is here.

![]()
If you are setting the default focus to another control in dialog, then OnInitDialog() should return FALSE. Have a look at the code snippet below.
BOOL CStartupFocusDlg::OnInitDialog()
{
...
// Set focus to your control.
CWnd* pWnd = GetDlgItem(IDC_EDIT2);
pWnd->SetFocus();
// return TRUE; // Wizard Generated code.
// Return FALSE if you set focus to different control
return FALSE;
}
![]()
Download the Sample, if you want to see it in action. Please note that sample is compiled in Visual C++ 2008.
Feb 7th
Many times i lost my temper by waiting for the re-build to be finished. So i just attempted to tune and reduce the build time by removing unnecessary includes. At that time I just wondered how to get the build time?
![]()
Just follow the steps to enable the ‘Build Time’.
1) Take – Tools > Options.
2) Now take - Project and Solutions > VC++ Project Settings.
3) Now enable the ‘Build Timing’ option and rebuild your project.
![]()
This article seems useful – How do you reduce compile time, and linking time for Visual C++ projects?. Have a look at it.
Feb 7th
Dears Visual C++ Enthusiasts ,
At last the migration is successfully completed. It was a bit painful though. Encountered a lot of issues, but lucky was able to solve it without much trouble. Now WeSeeTips is back in new style. Enjoy!
You have already noticed that for last few months there were hardly any update. My sincere apologies to my readers that I was damn busy due to several personal reasons. But I’ve a chest full of interesting tips to show you. Now enjoy the show!
BTW, what do you think about the new theme? Do you like it? Please don’t hesitate to write a few lines in comment section. Thanks for your time!
For WeSeeTips,
Jijo.
Feb 7th
Nov 22nd
![]()
Ever wondered how to generate XML schema from XML file?
Indeed, a lot of 3ed party tools can do it for you. But as usual, any easy way?
![]()
You can use Visual Studio IDE itself to generate XSD from XML file. Follow the steps.
1) Open the XML file in Visual Studio IDE.
2) Take View > Other Windows > Command Window.
3) Now type and execute command – XML.CreateSchema
4) See, the schema is generated.
![]()
A hidden pearl in VS IDE. Isn’t it?
![]()
Targeted Audience – Beginners.
Nov 15th
![]()
Ever tried VisualAssist? Yes man, Its a killer product. The feature that I like most is its Symbol Search. You can specify words and it will list symbols that contain those words. Its very useful if you have a vague idea about the function name that you’re searching for. Have a look at the following screenshot.


But is there any PoorMan’s SearchSymbols without VisualAssist?
![]()
Did you forget about the hidden gem in VisualStudio find window? Yes, the Regular Expressions. You can use .* to do the same search done by VisualAssist. For instance, to do the above search, search for Show.*Msg. Have a look at following screenshots.

Now look at the results. Cool! isn’t it?

![]()
Quite easy. nah? BTW, did you try to kill that bug.
![]()
Targeted Audience – Beginners.