Search This Blog

Friday, August 21, 2009

Get the User App Data Directory



#include <shlobj.h>

char path[_MAX_PATH];

//Get the User App Data Directory
SHGetFolderPath(NULL,CSIDL_LOCAL_APPDATA,NULL,0,path);

//Get the Common App Data Directory
SHGetFolderPath(NULL,CSIDL_COMMON_APPDATA,NULL,0,path);

Tuesday, August 11, 2009

To Close SDI/MDI Application Programmatically

AfxGetMainWnd()->PostMessage(WM_CLOSE);

To lock your machine programmatically

To lock your machine programmatically you can use the API LockWorkStation().

Syntax
C++

BOOL WINAPI LockWorkStation(void);


//Code:
if( !LockWorkStation())
{
// Failed to lock the machine
}

This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation. To unlock the workstation, the user must log in. There is no function you can call to determine whether the workstation is locked.

Monday, July 20, 2009

VC++ code to copy/move records from one database in to another database having the same table.

Download Library and header files from below link:
http://www.codeproject.com/KB/database/CppSQLite.aspx



int CopyMoveDBTable( LPCTSTR lpcSourceDBPath ,LPCTSTR lpcSourceTable , LPCTSTR lpcTargetDBPath ,LPCTSTR lpcTargetTable ,LPCTSTR lpcCriteria ,BOOL bCopy)
{

     CppSQLite3DB* m_db;    
     try
     {
          CString csQuery;              
          m_db=new CppSQLite3DB();         
          m_db->open(lpcSourceDBPath);          
         
          csQuery.Format( "Attach '%s' as sourcedbTable;",lpcTargetDBPath);
          m_db->execDML(csQuery);
          m_db->execDML("begin transaction");
          csQuery.Format( "insert into sourcedbTable.%s select * from main.%s %s ;",lpcSourceTable,lpcTargetTable,lpcCriteria);
          m_db->execDML("commit transaction");
          //insert into sourcedbTable.emptable select * from main.embdb where empid < 1 ;");
          m_db->execDML(csQuery);
          if(bCopy==FALSE)
          {
               m_db->execDML("begin transaction");
               csQuery.Format( "delete from main.%s %s ;",lpcSourceTable,lpcCriteria);
               m_db->execDML(csQuery);
               m_db->execDML("commit transaction");
          }
          csQuery.Format( "DETACH sourcedbTable;");
          m_db->execDML(csQuery);
         
          m_db->close();         
     }

     catch (CppSQLite3Exception& e)
     {
          delete m_db;
         
          AfxMessageBox( e.errorMessage());
          return -1;
     }
     delete m_db;
    
     return 1;

}    

EXample:

     CString csDBPath,csMirrorDB,csTable,csCriteria;
     BOOL bCopy=FALSE;
     csDBPath= "c:\\DatabaseSqlite\\Employee.db";
     csMirrorDB= "c:\\DatabaseSqlite\\Employeemirror.db";
     csTable="EmpTable";
     csCriteria="where empId>5";
     CopyMoveDBTable( csDBPath,csTable, csMirrorDB ,csTable ,csCriteria ,bCopy);

Thursday, July 16, 2009

Code to close Current application

//Code to close Current application

AfxGetMainWnd()->PostMessage(WM_CLOSE);

Program to create shortcut programatically



int createshotcut(CString csShorcutFile,CString csSourceFile,CString csWorkingDirectory,CString Comments )
{
     CoInitialize( NULL );
    
     IShellLink* psl;
    
     // Get a pointer to the IShellLink interface.
    
     HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
         
          IID_IShellLink, (PVOID *) &psl);
    
     if (SUCCEEDED(hres))
         
     {
         
          IPersistFile* ppf;
         
          // Set the path to the shortcut target and add the
         
          // description.
         
          psl->SetPath((LPCSTR) csSourceFile);
         
          psl->SetWorkingDirectory((LPCSTR)csWorkingDirectory);
         
          psl->SetDescription((LPCSTR) Comments );
         
          hres = psl->QueryInterface(IID_IPersistFile, (PVOID *) &ppf);
         
          if (SUCCEEDED(hres))
              
          {
              
               WORD wsz[MAX_PATH];
              
               MultiByteToWideChar(CP_ACP, 0, (LPCSTR) csShorcutFile, -1, wsz, MAX_PATH);
              
               hres = ppf->Save(wsz, TRUE);
              
               ppf->Release();
              
          }
         
          psl->Release();
         
     }
    
     CoUninitialize();

     return 1;
}


Example:
     createshotcut("c:\\mydairy.lnk","d:\\files\\secret.txt","d:\\","My Notepad Shortcut");

Saturday, May 30, 2009

Folder Select Dialog in MFC



BROWSEINFO bi;
ZeroMemory(&bi, sizeof(bi));
TCHAR szDisplayName[MAX_PATH];
szDisplayName[0] = ' ';

bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = szDisplayName;
bi.lpszTitle = _T("Please select a folder:");
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lParam = NULL;
bi.iImage = 0;

LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
TCHAR szPathName[MAX_PATH];
if (NULL != pidl)
{
BOOL bRet = SHGetPathFromIDList(pidl,szPathName);
if(bRet)
{
   AfxMessageBox(szPathName);
}

}