Search This Blog

Sunday, November 29, 2009

String Cstring comparision



/*
string and CSting Find, mid, left funtion
Example and comparison.
*/



#include <string> //string
using namespace std; //string


string strSample,strLeft,strRight,str;
size_t pos=0;
strSample="This is my sample";

pos=strSample.find("my");
if(pos!=string::npos)
{
strLeft=strSample.substr(0,pos);
//This is

strRight=strSample.substr(pos);
//my sample

str=strSample.substr(pos+strlen("my"));
// sample
}


CString csSample,csLeft,csRight,csStr;
int nPos=-1;
csSample="This is my sample";
if( (nPos=csSample.Find("my"))>-1)
{
csLeft=csSample.Left(nPos);
//This is

csRight=csSample.Mid(nPos);
//my sample

csStr=csSample.Mid(nPos+_tcslen("my"));
// sample

}

Tuesday, November 17, 2009

DWORD to CString and CString to DWORD



//DWORD to CString
DWORD dwNumber = 1234;
CString csNumber;
csNumber.Format("%lu", dwNumber);


//CString to DWORD
DWORD dwNO;
CString csDwNumber="1234";
dwNO= atol((char*)(LPCTSTR)csDwNumber);

COM Header and lib files




/*Error 122 error LNK2019: unresolved external symbol "wchar_t * __stdcall _com_util::ConvertStringToBSTR(char const *)" (?ConvertStringToBSTR@_com_util@@YGPA_WPBD@Z
) referenced in function
"public: __thiscall _variant_t::_variant_t(char const *)"
(??0_variant_t@@QAE@PBD@Z)


Error 121 error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)" (?ConvertBSTRToString@_com_util@@YGPADPA_W@Z)
referenced in function "public: char const * __thiscall _bstr_t::Data_t::GetString(void)const " (?GetString@Data_t@_bstr_t@@QBEPBDXZ)
*/


/*
Some Times when we are using COM lt will give above Linker Errors
To fix this add the following header file and library
*/



#include <comutil.h>
# pragma comment(lib, "comsuppwd.lib")

Friday, November 13, 2009

To Set a window as a topmost window



// Set a window position as a topmost window.
::SetWindowPos( GetSafeHwnd(),
HWND_TOPMOST,
0, 0, 0, 0,
SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE );

To set a text in Textbox using Resource ID



//To set a text in Textbox using Resource ID
//instead of CEdit member variable
  ::SetDlgItemText(GetSafeHwnd(), IDC_EDIT1, "username");

Tuesday, November 10, 2009

CDHtmlDialog crash while updating frequently



/*
when using CDHtmlDialog to navigate a page frequently
and changing content and refresh it will crash in
ieframe.dll or mshtml.dll.

To avoid crash check browser busy state
before navigating a file in CDHtmlDialog

*/


//sample code to navigating html file

void CMYDHtmlDialog::NavigateFile()
{
CString csFilePath;
csFilePath = "c:\\sample.html";

//wait for 1 second if not loaded or in busy state
if( FALSE == WaitTillLoaded (1000) )
return;

Navigate(csFilePath, NULL, NULL);

}


//sample code to wait till page lode in browser
BOOL CMYDHtmlDialog::WaitTillLoaded (int nTimeout)
{
READYSTATE result;
DWORD nFirstTick = GetTickCount ();

do
{
m_pBrowserApp->get_ReadyState (&result);

if (result != READYSTATE_COMPLETE)
Sleep (50);

if (nTimeout > 0)
{
if ((GetTickCount () - nFirstTick) > nTimeout)
break;
}
} while (result != READYSTATE_COMPLETE);

if (result == READYSTATE_COMPLETE)
return TRUE;
else
return FALSE;
}