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);
}
}
Search This Blog
Saturday, May 30, 2009
Folder Select Dialog in MFC
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 );
MFC code to execute a command exe and read the output
CString ReadReturnValueOfCmdFile(CString csExeName, CString csArguments)
{
CString csExecute;
csExecute=csExeName + " " + csArguments;
SECURITY_ATTRIBUTES secattr;
ZeroMemory(&secattr,sizeof(secattr));
secattr.nLength = sizeof(secattr);
secattr.bInheritHandle = TRUE;
HANDLE rPipe, wPipe;
//Create pipes to write and read data
CreatePipe(&rPipe,&wPipe,&secattr,0);
//
STARTUPINFO sInfo;
ZeroMemory(&sInfo,sizeof(sInfo));
PROCESS_INFORMATION pInfo;
ZeroMemory(&pInfo,sizeof(pInfo));
sInfo.cb=sizeof(sInfo);
sInfo.dwFlags=STARTF_USESTDHANDLES;
sInfo.hStdInput=NULL;
sInfo.hStdOutput=wPipe;
sInfo.hStdError=wPipe;
char command[1024]; strcpy(command,
csExecute.GetBuffer(csExecute.GetLength()));
//Create the process here.
CreateProcess(0, command,0,0,TRUE,
NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo);
CloseHandle(wPipe);
//now read the output pipe here.
char buf[100];
DWORD reDword;
CString m_csOutput,csTemp;
BOOL res;
do
{
res=::ReadFile(rPipe,buf,100,&reDword,0);
csTemp=buf;
m_csOutput+=csTemp.Left(reDword);
}while(res);
return m_csOutput;
}
Friday, May 15, 2009
UnMashalling WebBroser Pointer
DWORD dwBrowserPtr = NULL;
dwBrowserPtr =dwStream //input
::CoInitialize(NULL);
IStream** pStream = NULL ;
pStream = reinterpret_cast<IStream**>(dwBrowserPtr);
if( pStream == NULL )
{
::CoUninitialize();
return -1;
}
CComPtr<IDispatch> spBrowserPointer ;
IWebBrowserQIPtr pBrowserPtr;
HRESULT hr = CoGetInterfaceAndReleaseStream(*pStream,IID_IWebBrowser2,(LPVOID*)&pBrowserPtr);
if ( hr == S_OK )
{
//success
}
else
{
//Failed To get browser pointer
ASSERT(0);
}
Marshalling WebBrowser Pointer
/* ============================================================================
#include <mshtml.h> */
typedef CComQIPtr<IWebBrowser2,&IID_IWebBrowser2> IWebBrowserQIPtr;
IStream** pStream = NULL ;
pStream = new IStream* ; // free this memory only at the end of parsing.If you are freeing before that parse will fail
CComPtr<IDispatch> spBrowserPointer ;
spBrowserPointer = APPPTR->m_pdlg->m_webBwsrCntrl.get_Application();
IWebBrowserQIPtr pBrowserPtr;
pBrowserPtr = spBrowserPointer;
HRESULT hr = ::CoMarshalInterThreadInterfaceInStream(IID_IWebBrowser2,pBrowserPtr,pStream);
DWORD dwStream = 0;
if( hr == S_OK )
{
dwStream = reinterpret_cast<DWORD>( pStream );
}
else
{
return -1;
}
//(*pStream)->Release(); // Do not release this.While calling CoGetInterfaceAndReleaseStream it will be automaticaly released.
//=============================================================================
UIThreadImplementation
Create a new class and inherit CWinThread
Add dialog member object in the Thread class
BOOL CMyNewThread::InitInstance()
{
m_dlg = new CMyDialog();
m_dlg->Create (IDD_DIALOG_MY, NULL);
m_dlg->ShowWindow( TRUE );
m_dlg->UpdateWindow();
return TRUE;
//return TRUE;
}
// calling the Thread
void CMainFrame::OnNewCreatenewwindow()
{
// TODO: Add your command handler code here
CWinThread *pThread = AfxBeginThread( RUNTIME_CLASS( CMyNewThread ) );
}
Add dialog member object in the Thread class
#pragma once
#include "MyDialog.h"
// CMyNewThread
class CMyNewThread : public CWinThread
{
DECLARE_DYNCREATE(CMyNewThread)
protected:
CMyNewThread(); // protected constructor used by dynamic creation
virtual ~CMyNewThread();
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
CMyDialog *m_dlg;
protected:
DECLARE_MESSAGE_MAP()
};
BOOL CMyNewThread::InitInstance()
{
m_dlg = new CMyDialog();
m_dlg->Create (IDD_DIALOG_MY, NULL);
m_dlg->ShowWindow( TRUE );
m_dlg->UpdateWindow();
return TRUE;
//return TRUE;
}
// calling the Thread
void CMainFrame::OnNewCreatenewwindow()
{
// TODO: Add your command handler code here
CWinThread *pThread = AfxBeginThread( RUNTIME_CLASS( CMyNewThread ) );
}
Thursday, May 14, 2009
WorkerThread Implementation
class CWorkerThreadImplementationDlg : public CDialog
{
typedef struct DLG_STRUCT
{
CWorkerThreadImplementationDlg* _this;
}DLG_STRUCT , *PDLGSTRUCT;
void CWorkerThreadImplementationDlg::OnBnClickedOk()
{
//CString *cstrValue = new CString("Hai");
//AfxBeginThread( CWorkerThreadImplementationDlg::RunProgress , (LPVOID)cstrValue );
PDLGSTRUCT lpDLG1 = new DLG_STRUCT;
lpDLG1->_this = this;
AfxBeginThread( CWorkerThreadImplementationDlg::RunProgress1 , lpDLG1 );
PDLGSTRUCT lpDLG2 = new DLG_STRUCT;
lpDLG2->_this = this;
AfxBeginThread( CWorkerThreadImplementationDlg::RunProgress2 , lpDLG2 );
}
UINT CWorkerThreadImplementationDlg::RunProgress( LPVOID lpThreadParam )
{
CString pstr = *((CString*)lpThreadParam);
return 1;
}
UINT CWorkerThreadImplementationDlg::RunProgress1( LPVOID lpThreadParam )
{
PDLGSTRUCT pstr = (PDLGSTRUCT)lpThreadParam;
int nPos = 0;
while( nPos < 100 )
{
pstr->_this->m_Progress1.SetPos( nPos++ );
Sleep( 100 );
}
return 1;
}
UINT CWorkerThreadImplementationDlg::RunProgress2( LPVOID lpThreadParam )
{
PDLGSTRUCT pstr = (PDLGSTRUCT)lpThreadParam;
int nPos = 0;
while( nPos < 100 )
{
pstr->_this->m_Progress2.SetPos( nPos++ );
Sleep( 100 );
}
return 1;
}
Subscribe to:
Posts (Atom)