Search This Blog

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);
}

}

Monday, May 25, 2009

Code to Create a file, If file exist already append a GUID with that file and write the content



#pragma comment (lib, "Rpcrt4.lib")

CString GetUniqueGUID( )
{
HRESULT hr = NULL;
CString sUUID(_T(""));
UUID *pUUID = NULL;
BOOL bAllocated = FALSE;
unsigned char *sTemp = NULL;

if (pUUID == NULL)
{
pUUID = new UUID;
bAllocated = TRUE;
}

if (pUUID != NULL)
{
hr = UuidCreate( pUUID );
if (hr == RPC_S_OK)
{
hr = UuidToString(pUUID, &sTemp);
if (hr == RPC_S_OK)
{
sUUID = sTemp;
sUUID.MakeUpper();
RpcStringFree(&sTemp);
}
}
if (bAllocated)
{
delete pUUID;
pUUID = NULL;
}
}
return sUUID;
}


void CreateFileWithoutReplacingExisting(CString & csFileName , CString & csContent )
{

CFileStatus cf;
if(CFile::GetStatus(csFileName,cf)) // File Exist
{
CString csExt ;
CString csGUID=GetUniqueGUID();
csGUID = "_"+csGUID;
int nPos = csFileName.ReverseFind('.');
if(nPos > -1 )
{
csExt = csFileName.Mid(nPos);
csFileName = csFileName.Left(nPos);
csFileName = csFileName+csGUID+"."+csExt;
}
}
CreateFile(csFileName,csContent);

}


int CreateFile(LPCTSTR lpcFileName, LPCTSTR lpcText)
{
if( !lpcFileName || lpcFileName[0]=='\0' )
{
return -1;
}
if(!lpcText || lpcText[0]=='\0') return -1;

CFile cf;
CFileException cfe;

try
{
if(!cf.Open(lpcFileName,CFile::modeCreate|CFile::modeWrite|CFile::shareDenyWrite, &cfe))
{
return 0;
}

cf.Write(lpcText, lstrlen(lpcText) );
cf.Close();
}
catch(CFileException *ce)
{
ce->Delete();
cf.Close();
return -1;
}
return 1;
}

IGlobalInterfaceTable alternate for marshalling in multithreaded enviroment



typedef CComQIPtr<IWebBrowser2,&IID_IWebBrowser2> IWebBrowserQIPtr;
CComPtr<IDispatch> spBrowserPointer ;
IWebBrowserQIPtr pBrowserPtr;
HRESULT hres = S_OK;
DWORD dwCookie = 0;
//Create the GIT
IGlobalInterfaceTable* pGlobalInterfaceTable = NULL;
hres = ::CoCreateInstance(CLSID_StdGlobalInterfaceTable,NULL,CLSCTX_INPROC_SERVER,IID_IGlobalInterfaceTable,(void**)&pGlobalInterfaceTable);
//Register the interface in the GIT
hres = pGlobalInterfaceTable ->RegisterInterfaceInGlobal(pBrowserPtr,IID_IWebBrowser2,&dwCookie);

//this can be used in were ever we unmarshall and use.
//hres =pGlobalInterfaceTable->GetInterfaceFromGlobal(dwCookie,IID_IWebBrowser2,(void**)&pWebBrowserPtr);

//this should be called after using and remove the entry from table
//pGlobalInterfaceTable->RevokeInterfaceFromGlobal(dwCookie);
pGlobalInterfaceTable->Release( );//Don't need the GIT

Friday, May 22, 2009

Download a web page into a file

     HRESULT hr;
     CString csWebsiteName,csFileName;
     csWebsiteName="http://google.com";
     csFileName="c:\\google.htm";
     hr = URLDownloadToFile( NULL ,csWebsiteName ,csFileName , 0 ,NULL );