Search This Blog

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