Archive

Posts Tagged ‘MessageBox’

How to create a GUID programmatically?

Do the following…

GUID guId = { 0 };
CoCreateGuid( &guId );
// or
UUID uid = { 0 };
UuidCreate( &uid );

For converting to string use…

PUCHAR pChar = 0;

// Convert to string
UuidToString( &uid, &pChar );
MessageBox( NULL, (char*)pChar, NULL, MB_OK );

//Don't forget to free allocated string
RpcStringFree( &pChar );
Advertisements

MessageBoxTimeout

A printf type of MessageBox function which takes variable arguments!

It’s a hard chore for a programmer to display string and integers in a message box, mainly because they have to use CString or sprintf to get these integers in formatted manner…

For eg:

int nAge = 27, nSal=100;
CString csStr;
csStr.Format( _T( "Age: %d, nSal: %d" ), nAge, nSal );
AfxMessageBox( csStr );

How can we automate this whole process? Here is the answer in the form of a function called ShowMsg.

Code:

// Shows dialog
void __cdecl ShowMsg( LPCTSTR lpctszTitle_i, const DWORD dwIconType_i, LPCTSTR lpctszMessage_i, ... )
{
        va_list vaArgList;
        va_start( vaArgList, lpctszMessage_i );

        CString csMsg;
        csMsg.FormatV( lpctszMessage_i, vaArgList );

        MessageBox( AfxGetMainWnd()->GetSafeHwnd(),
                    csMsg,
                    lpctszTitle_i,
                    dwIconType_i );

        va_end( vaArgList );
}// End ShowMsg

//And call this function like wise
ShowMsg( _T( "Title" ),               
         MB_OK|MB_ICONINFORMATION,              
         _T( "My name is %s, my age is %d, my salary is %d" ),              
         _T( "Nibu babu thomas" ), 27, 100 );

You can customize this function based on your need.

%d bloggers like this: