Search This Blog

Saturday, February 21, 2009

VC++ Code equivalent to DoEvents is Visual basic

//VC++ Code equivalent to DoEvents is Visual basic

void DoEvents()
{
MSG msg;

while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if ( ::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else
{
break;
}
}
}

MFC code to get the Path of Appliction and DLL






//MFC code to get the Path of Appliction
CString GetExePath ()
{
   TCHAR szPath[_MAX_PATH];
   szPath[0] = '\0';

   GetModuleFileName( NULL, szPath, sizeof(szPath) );

   CString csPath(szPath);

   int nPos = csPath.ReverseFind('\\');
   if ( nPos != -1)
   {
       csPath = csPath.Left(nPos+1);
   }
   else
   {
       csPath = "";
   }

   return csPath ;

}


//MFC code to get the Path of DLL if it is loaded from different path
#include <atlbase.h>
CString GetDLLPath ()
{
   TCHAR szPath[_MAX_PATH];
   szPath[0] = '\0';

   GetModuleFileName((HINSTANCE)&__ImageBase, szPath, sizeof(szPath) );

   CString csPath(szPath);

   int nPos = csPath.ReverseFind('\\');
   if ( nPos != -1)
   {
       csPath = csPath.Left(nPos+1);
   }
   else
   {
       csPath = "";
   }

   return csPath ;

}

Saturday, February 7, 2009

Useful Cstring Functions



//For replacing nonprintable and non-ASCII unicode to space
CString RemoveNonPrintableCharWithSpacer(LPCSTR lpText)
{
CString csData;
for(;*lpText!='\0'; lpText++)
{
if(*lpText<0 || *lpText>255)
{
csData += " ";
}
else
{
csData += *lpText;
}
}

return csData;
}



//For counting no of small characters
unsigned int CountSmallCharacters(LPCTSTR lpText)
{
unsigned int nSCharCount =0;
for(;*lpText!='\0'; lpText++)
{
if(*lpText>='a' && *lpText <='z')
nSCharCount++;
}
return nSCharCount;
}



//For counting no of characters
unsigned int CountCharacters(LPCTSTR lpText)
{
unsigned int nCharCount =0;
for(;*lpText!='\0'; lpText++)
{
if( (*lpText>='a' && *lpText <='z') || (*lpText>='A' && *lpText <='Z') )
nCharCount++;
}
return nCharCount;
}

Code to Click Internt Explorer Security warning Dialog Box

//Code to Click Internt Explorer Security warning Dialog Box Automatically

BOOL g_bStopDialogWatcherThread = FALSE; // To Stop & start Thread

UINT WindowWatcher(LPVOID lpParam)
{
g_bStopDialogWatcherThread = FALSE;
while(1)
{
Sleep(300);
HWND hWnd;
HWND hWndWarning = ::FindWindow(NULL,"Internet Explorer");
hWnd=::FindWindowEx(hWndWarning , NULL , "Button","&Yes" );
if(hWnd!=NULL && hWndWarning!=NULL)
{
::SetForegroundWindow(hWndWarning);
::SetFocus(hWnd);
::PostMessage ( hWnd , WM_LBUTTONDOWN , 0 , 0 );
::PostMessage ( hWnd , WM_LBUTTONUP , 0 , 0 );
Sleep(1000);
}

if(g_bStopDialogWatcherThread)
{
break;
}
}
return 1;
}

AfxBeginThread(WindowWatcher,THREAD_PRIORITY_NORMAL,0,0,0);//to start the thread

Tuesday, January 27, 2009

How to empty recycle bin programmatically?

While processing or writing huge files to disk, its quite possible that disk will go out of space. To Squeeze and to get more disk space, its a good idea to clean the recycle bin. But how to do it programmatically?


You can use the api - SHEmptyRecycleBin(). See the code snippet below.

SHEmptyRecycleBin( NULL, NULL, SHERB_NOCONFIRMATION | SHERB_NOPROGRESSUI | SHERB_NOSOUND );

Note:
By modifying the options, you can show progress UI, Show confirmation dialog and play sound on finishing task. Just remove the unwanted flags.

Friday, January 23, 2009

Interprocess communication between two application in MFC

This is the example to show how to send a cstring from one application to another.


Paste this follwing code at sender.

CString csData(_T(""));
csData=" This is the message";
COPYDATASTRUCT copyDT;//Is used for Interprocess communication
copyDT.dwData = NULL;
copyDT.cbData = _tcslen(csData) + 1;
copyDT.lpData = ( void* ) ( ( LPCTSTR ) csData );
HWND hWnd = ::FindWindow(NULL,"Receiver application");
// Finding the handle of receiver application and send

// WM_COPYDATA message is used for Interprocess communication
if(hWnd!=NULL)
{
::SendMessage( hWnd, WM_COPYDATA, 0, (LPARAM)&copyDT );
}


At receiver add WM_COPYDATA message.
Paste the following code

CString csReceived;
csReceived=(LPCTSTR)pCopyDataStruct->lpData;

Saturday, January 17, 2009

Function delete a Folder or directory with files and subfolders.

There is no API to delete a directory with files and subfolders.
It is the function to delete folders with files using shell operation.


bool DeleteDirectory(LPCTSTR lpszDir)
{
int len = _tcslen(lpszDir);
TCHAR *pszFrom = new TCHAR[len+2];
_tcscpy(pszFrom, lpszDir);
pszFrom[len] = 0;
pszFrom[len+1] = 0;

SHFILEOPSTRUCT fileop;
fileop.hwnd   = NULL;    // no status display
fileop.wFunc  = FO_DELETE;  // delete operation
fileop.pFrom  = pszFrom;  // source file name as double null terminated string
fileop.pTo    = NULL;    // no destination needed
fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;  // do not prompt the user


fileop.fAnyOperationsAborted = FALSE;
fileop.lpszProgressTitle     = NULL;
fileop.hNameMappings         = NULL;

int ret = SHFileOperation(&fileop); 
delete [] pszFrom;  
return (ret == 0);
}

This function return true if it succeed.