Search This Blog

Thursday, January 1, 2009

error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wc

If you are getting following error:
error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'

Solution1:
In Project Menu select your Project Properties.

In Configuration Properties
Set "Character Set" as "Use Multi-Byte Character Set"

Solution2:
where ever you are declaring "CString" variable add Unicode support.
Example:
CString csStr(_T("")); //_T("") is for unicode support


Wednesday, December 24, 2008

MFC Code to get Process Name & Process ID and kill by process ID

#include "Tlhelp32.h"
#include "winbase.h"


// Function to kill Process using Process ID
int Killprocess(int nProcessID)
{
HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, 0, nProcessID );
if (hProcess == NULL)
return -1;
BOOL nReturnVal = TerminateProcess( hProcess, 9 );
CloseHandle (hProcess);
return nReturnVal;
}


// Function to get all Process Name and Process ID
CString GetProcessNameandID()
{
HANDLE hSnapShot;
hSnapShot=CreateToolhelp32Snapshot (TH32CS_SNAPALL,NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize =sizeof(pEntry);
CString csProcessList;
DWORD dwID;
CString csProcessID;
//Get first process
Process32First (hSnapShot,&pEntry);
//Get all processes
while(1)
{
BOOL hRes=Process32Next (hSnapShot,&pEntry);
if(hRes==FALSE)
break;
csProcessList=csProcessList+pEntry.szExeFile;
dwID=pEntry.th32ProcessID;
csProcessID.Format("%i",dwID);
csProcessList=csProcessList +" "+csProcessID +"\r\n" ;
}
return csProcessList;
}

Friday, December 19, 2008

MFC code to Download a webPage

#include "afxinet.h" //add this header file

BOOL DownloadURL(const char *url, const char *filename, CString &errorMessage)
{
const int FILEBUFLEN = 1024;
char httpBuff[FILEBUFLEN];
TCHAR szErr[255];
errorMessage = "";

TRY {
CInternetSession session;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 3);
CFile *remoteFile = session.OpenURL(url, 1 ,INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
CFile localFile(filename, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
int numBytes;
while (numBytes = remoteFile->Read(httpBuff, FILEBUFLEN))
{
localFile.Write(httpBuff, numBytes);
}
}
CATCH_ALL(error) {
error->GetErrorMessage(szErr,254,NULL);
errorMessage.Format("%s",szErr);
return FALSE;
}
END_CATCH_ALL;
return TRUE;
}

void CDownloadWebPageDlg::OnOK() //Copy the following Lines where you need to download.
{
CString csErr;
if(!DownloadURL("http://www.google.com","c:\\google.htm",csErr))
{
AfxMessageBox(csErr);
}
}

Hide the Taskbar application button

Open the .rc file in notepad
search for the line

"EXSTYLE WS_EX_APPWINDOW "

and replace "WS_EX_APPWINDOW" with "WS_EX_TOOLWINDOW".

Save and Close

MFC Code to avoid a Application running Many times

Paste this following code in BOOL CYourProjectNameApp::InitInstance() function of you Aplliction.


CreateMutex(NULL,TRUE,"Sample");
// mutex will be automatically deleted when process ends.
BOOL bAlreadyRunning = (GetLastError() == ERROR_ALREADY_EXISTS);
if(bAlreadyRunning)
{
::MessageBox(NULL, "This Appliction is Already Running ", "Information",
MB_ICONINFORMATION|MB_OK|MB_DEFBUTTON1);
return 0;
}

Thursday, December 18, 2008

MFC Code to Close Task manager Automatically if it is lanched

BOOL g_bStopDialogWatcherThread = FALSE; // To Stop & start Thread

UINT DialogWatcher(LPVOID lpParam)
{
g_bStopDialogWatcherThread = FALSE;
while(1)
{
Sleep(300);
HWND hWnd = ::FindWindow(NULL,"Windows Task Manager");//Windows Task Manager


if(hWnd !=NULL)
{
// ::SetForegroundWindow(hWnd);
// ::SetFocus(hWnd);
// ::SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE );
// ::SendMessage(hWnd,WM_COMMAND,(WPARAM)0x0000025C,(LPARAM)0x0000025C);

::SendMessage(hWnd,WM_CLOSE,0,0);

}

if(g_bStopDialogWatcherThread)
{
break;
}
}
return 1;
}




void CFindWindowDlg::OnbtnClick()
{
AfxBeginThread(DialogWatcher,THREAD_PRIORITY_NORMAL,0,0,0);

}

void CFindWindowDlg::OnCancel()
{
g_bStopDialogWatcherThread = TRUE;
CDialog::OnCancel();
}

Thursday, December 11, 2008

UnManaged and Managed (CLR) Data Type equivalants

Unmanaged Windows API types Unmanaged C language types CLR type
HANDLE void* IntPtr
BYTE unsigned char Byte
SHORT short Int16
WORD unsigned short UInt16
INT, LONG int, long Int32
BOOL int, long Int32 or Boolean
UINT unsigned int UInt32
DWORD, ULONG unsigned long UInt32
CHAR char Char
LPSTR, LPCSTR, LPWSTR, LPCWSTR char*, wchar_t* String or StringBuilder
FLOAT float Single
DOUBLE double Double