Search This Blog

Saturday, February 28, 2009

How to convert CString into LPTSTR ?

CString csStr = "Hello";
LPTSTR lpStr = csStr.GetBuffer( 0 );
csStr.ReleaseBuffer();

Friday, February 27, 2009

Getting Local Application Path in XP & Vista

typedef HRESULT (WINAPI * SHGetKnownFolderPathFn)(REFKNOWNFOLDERID rfid, DWORD dwFlags, HANDLE hToken,PWSTR *ppszPath);

PWSTR pszPath[1];
SHGetKnownFolderPathFn shGetKnownFolrPth = NULL ;

HINSTANCE hins = LoadLibrary(”Shell32.dll”);

if(hins != NULL)
{

shGetKnownFolrPth = (SHGetKnownFolderPathFn)::GetProcAddress(hins,”SHGetKnownFolderPath”);

if( shGetKnownFolrPth != NULL)
{
shGetKnownFolrPth(FOLDERID_LocalAppDataLow,0,NULL,pszPath);
CString csData=pszPath[0]; // converting from wchar to ANSI.
}
}





int GetCurrentUserLocalLowAppPath(LPTSTR szProfileDir,int dirlen )
{

memset(szProfileDir,0,dirlen);

if(IsWinXP() == TRUE)
{
GetCurrentUserAppPath( szProfileDir, dirlen );
}
else if(IsVista() == TRUE)
{
PWSTR pszPath[1];
SHGetKnownFolderPath(NULL,FOLDERID_LocalAppDataLow,0,NULL,pszPath);
//copying the pszPath to szProfileDir array
}
return 1;

}

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.