Search This Blog

Thursday, May 24, 2012

Linker Errors when using InetIsOffline


Some times we will get linker errors like below when you are usnig InetIsOffline() Function.
 error LNK2019: unresolved external symbol __imp__InetIsOffline@4 referenced in function

To solve this problem add the following code after Header files include.

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

(OR)

Otherwise add "url.lib" in the Linker input of your project settings.

Wednesday, June 16, 2010

xtreme toolkit Sample Dialog Gradient Window Example



//In Header File

#include "c:\program files\codejock software\mfc\xtreme toolkitpro v13.0.0\source\controls\xtbutton.h"
#define HIGH_DLG_CLR RGB(163,194,236)
#define LOW_DLG_CLR RGB(220,235,253)


//public:
//Add Two buttons
CXTButton m_btnOK;
CXTButton m_edBTNNN;




//In CPP File
BOOL CextremeSampleDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

m_btnOK.SetTheme( new CXTButtonThemeOffice2003 ( TRUE ) );
m_edBTNNN.SetTheme( new CXTButtonThemeOfficeXP ( TRUE ) );
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon



return TRUE; // return TRUE unless you set the focus to a control
}


void CextremeSampleDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
CXTPClientRect rc(this);
CXTPPaintManager::SetTheme(xtpThemeOffice2003);
CXTPOffice2003Theme *pPaintManager = (CXTPOffice2003Theme *)XTPPaintManager();
/*pPaintManager->GradientFill(&dc, rc,
pPaintManager->m_clrCommandBar.clrLight,
pPaintManager->m_clrCommandBar.clrDark ,
NULL);*/

pPaintManager->GradientFill(&dc, rc,LOW_DLG_CLR, HIGH_DLG_CLR , NULL);
}

Thursday, April 1, 2010

Download a webpage using WinInet



#include <wininet.h>


BOOL DownloadWebContentUsingWinInet(CString csURL,CString &csDownloadedContent)
{

if ( csURL.IsEmpty() )
return FALSE;
HINTERNET hINet, hFile;
hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
if ( !hINet )
{
AfxMessageBox("InternetOpen Failed");
return FALSE;
}
hFile = InternetOpenUrl( hINet, csURL, NULL, 0, 0, 0 ) ;
if ( hFile )
{
CHAR buffer[1024];
DWORD dwRead;
while ( InternetReadFile( hFile, buffer, 1023, &dwRead ) )
{
if ( dwRead == 0 )
break;
buffer[dwRead] = 0;
csDownloadedContent += buffer;
}
InternetCloseHandle( hFile );
}
InternetCloseHandle( hINet );

return TRUE;
}

Sunday, March 21, 2010

Check whether a file exist

//Check whether a file exist
//Portable to linux

#include <sys/stat.h>
#include <string>
using namespace std;

bool FileExists(string strFilename)
{
struct stat stFileInfo;
bool blnReturn= false;
int intStat=-1;
// Attempt to get the file attributes
intStat = stat(strFilename.c_str(),&stFileInfo);
if(intStat == 0)
{
// We were able to get the file attributes
blnReturn = true;
}
else
{
// We were not able to get the file attributes.
blnReturn = false;
}
return blnReturn;
}

Thursday, January 21, 2010

Stealing icon of another appliaction.



HICON hIcon;
hIcon = ExtractIcon( AfxGetApp()->m_hInstance, "C:\\WINDOWS\\system32\\calc.exe", 0 );
SetIcon( hIcon, FALSE );
//Do at destroy of dialog
//To avoid Resourse Leak
//DestroyIcon(hIcon);

Tuesday, December 15, 2009

Linker Errors when using winservice

Some times we will get linker errors like below when creating winservice.
To solve this add following code.



#pragma comment (lib, "advapi32.lib")
#pragma comment (lib, "user32.lib")


Or

include user32.lib and advapi32.lib in linker input of project settings



error LNK2019: unresolved external symbol __imp__CloseServiceHandle@4 referenced in function
error LNK2019: unresolved external symbol __imp__CreateServiceA@52 referenced in function
error LNK2019: unresolved external symbol __imp__OpenSCManagerA@12 referenced in function
error LNK2019: unresolved external symbol __imp__DeleteService@4 referenced in function
error LNK2019: unresolved external symbol __imp__OpenServiceA@12 referenced in function
error LNK2019: unresolved external symbol __imp__ControlService@12 referenced in function
error LNK2019: unresolved external symbol __imp__StartServiceA@12 referenced in function
error LNK2019: unresolved external symbol __imp__StartServiceCtrlDispatcherA@4 referenced in function
error LNK2019: unresolved external symbol __imp__SetServiceStatus@8 referenced in function
error LNK2019: unresolved external symbol __imp__RegisterServiceCtrlHandlerA@8 referenced in function
error LNK2019: unresolved external symbol __imp__PostThreadMessageA@16 referenced in function

Wednesday, December 2, 2009

Remove Duplicates From Vector Template Example



#include <vector>
#include <algorithm>
using namespace std;
// Add this header files

/*
Template function to Remove Duplicates
From a vector */


template <class T>
void RemoveDuplicates(vector<T> &vecContents)
{
vector<T> ::iterator vItr;

sort(vecContents.begin(),vecContents.end());
vItr=unique(vecContents.begin(),vecContents.end());
if(vItr!=vecContents.end())
vecContents.erase(vItr,vecContents.end());
}




//Usage Example 1

vector<CString> vNames;
vNames.push_back("John");
vNames.push_back("Victor");
vNames.push_back("Nancy");
vNames.push_back("William");
vNames.push_back("Nancy");

RemoveDuplicates(vNames);


//Usage Example 2
vector<int> vNos;
vNos.push_back(1);
vNos.push_back(1);
vNos.push_back(4);

RemoveDuplicates(vNos);

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


Saturday, October 31, 2009

To view all values and size of CStringArray in watch window

In watch window, we cannot view all the elements in a CStringArray .
We can view only the first element of the CStringArray.


To View all elements Please change the following settings [For Visual Studio 2008]:
1. Open the file "c:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\autoexp.dat" in notepad

2. Search for the line "[AutoExpand]" add the following line.

[AutoExpand]
CStringArray=size = <m_nSize> //Add this line


3. Search for the line "[Visualizer]" add the following line.

[Visualizer] //Add the below lines after this line

CStringArray{
children
(
#array
(
expr : ($e.m_pData[$i]),
size : ($e.m_nSize)
)
)
}

4. Save this file and close All Visual studio application and open again and try.

Wednesday, September 23, 2009

Finding current , previous and next date/time using COleDateTime



COleDateTime currdate;
COleDateTime prevdate;
COleDateTime nextday;
currdate = COleDateTime::GetCurrentTime();
CString csTime= currdate.Format(); //current date and time

COleDateTimeSpan span(1,0,0,0);

int nDay = currdate.GetDay(); //current day

prevdate = currdate - span;
nextday = currdate + span;

int nPrevDay = prevdate.GetDay(); //yesterday or Previous day

int nNextDay = nextday.GetDay(); //next day

Tuesday, September 22, 2009

Creating SDI application with List View



// MainFrm.h

CSplitterWnd m_splitwnd;



//MainFrm.pp
//Override OnCreateClient using properties in classview
//CLeftView and CRightView are the class should be derived from any of view class
//like CTreeView, CListView, CFormView
// Here CRightView derived from CListView

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// Add following Code
if(!m_splitwnd.CreateStatic(this,1,2))
return FALSE;

if(!m_splitwnd.CreateView(0,0,RUNTIME_CLASS(CLeftView),CSize(200,200),pContext) ||
!m_splitwnd.CreateView(0,1,RUNTIME_CLASS(CRightView),CSize(100,200),pContext) )
{
m_splitwnd.DestroyWindow();
return FALSE;
}
// End


return CFrameWndEx::OnCreateClient(lpcs, pContext);
}



// RightView.cpp : implementation file
//in CRightView Class add WM_CREATE message

int CRightView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListView::OnCreate(lpCreateStruct) == -1)
return -1;

GetListCtrl().DeleteAllItems();

CListCtrl &mylist =this->GetListCtrl();

ModifyStyle(NULL, LVS_REPORT , 0);
mylist.SetExtendedStyle( mylist.GetExtendedStyle() |LVS_EX_CHECKBOXES| LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP | LVS_EX_TWOCLICKACTIVATE | LVS_EX_SUBITEMIMAGES );

mylist.InsertColumn(0, _T("S.No"),LVCFMT_LEFT| LVCF_TEXT);
mylist.InsertColumn(1, _T("Name"), LVCFMT_LEFT| LVCF_TEXT);
mylist.InsertColumn(2, _T("Country"), LVCFMT_LEFT| LVCF_TEXT);
mylist.SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER);
mylist.SetColumnWidth(1, LVSCW_AUTOSIZE_USEHEADER);
mylist.SetColumnWidth(2, 100);

/*
mylist.InsertItem( 0, _T(""));
mylist.SetItemText( 0, 0, _T("1") );
mylist.SetItemText( 0, 1, _T("John") );
mylist.SetItemText( 0, 2, _T("India") );
*/


return 0;
}

Monday, September 21, 2009

Removing Duplicates from STL Vector


#include <vector>
#include <algorithm>
using namespace std;
// Add this header files


vector<CString> vNames;
vector<CString> ::iterator vItr;
CString csName;

vNames.push_back("John");
vNames.push_back("Victor");
vNames.push_back("Nancy");
vNames.push_back("William");
vNames.push_back("Nancy");
sort(vNames.begin(),vNames.end());
vNames.erase(unique(vNames.begin(),vNames.end()),vNames.end());

for(vItr=vNames.begin();vItr!=vNames.end();vItr++)
{
csName=(*vItr);
//TRACE(csName);
//AfxMessageBox(csName);
}

Thursday, September 17, 2009

ExtractIcon of other application

The ExtractIcon function retrieves a handle to an icon from the specified executable file, DLL, or icon file.

HICON hIcon;
hIcon = ExtractIcon( AfxGetApp()->m_hInstance, "C:\\WINDOWS\\system32\\calc.exe", 0 );
SetIcon( hIcon, FALSE );

//You must destroy the icon handle returned by ExtractIcon by calling the DestroyIcon //function.
DestroyIcon(hIcon);

Change or Hide Start button name windows XP



void ChangeOrHideStartButton()
{
HWND hSysTrayWnd = ::FindWindow( "Shell_TrayWnd", 0 );
if( hSysTrayWnd )
{
// Get the start button
HWND hStartBtn = ::FindWindowEx( hSysTrayWnd, 0, "Button", "Start" );
if( hStartBtn )
{
// Hide the start button if shown or show if hidden
if( ::IsWindowVisible( hStartBtn ))
{
::ShowWindow( hStartBtn, SW_HIDE );
}
else
{
::ShowWindow( hStartBtn, SW_SHOW );
//To change name
::SetWindowText(hStartBtn,"Your Name");
}
}

}
}

Monday, September 7, 2009

To Read all section and keys in an Ini File



void ReadSectionsAndKeys(CString csPath)
{
char lpszReturnBuffer[MAX_PATH];
char* pNextSection = NULL;
GetPrivateProfileSectionNames(lpszReturnBuffer,MAX_PATH,csPath);
pNextSection = lpszReturnBuffer;
//TRACE("Section: %s\n", pNextSection);
//printf("Section: %s\n", pNextSection);
// for keys
int nPos=-1;
char lpszKeyNames[8192];
DWORD dSize;
dSize = sizeof(lpszKeyNames);
CString csNameandValue(""), csKey(""),csValue("");
//

while (*pNextSection != 0x00)
{
GetPrivateProfileSection( pNextSection, lpszKeyNames , dSize , csPath );
char *pKeyName = lpszKeyNames;

while (*pKeyName != 0x00)
{
nPos = -1;
if(*pKeyName != 0x00)
{
//TRACE("Keys: %s\n", pKeyName);
csNameandValue = pKeyName;
if((nPos = csNameandValue.Find("=")) > -1)
{
csKey = csNameandValue.Left(nPos);
csValue=csNameandValue.Mid(nPos+1);
}
}
pKeyName = pKeyName + strlen(pKeyName) + 1;
}
pNextSection = pNextSection + strlen(pNextSection) + 1;
if(*pNextSection != 0x00)
{
//TRACE("Section: %s\n", pNextSection);
}
}
}