Skip to main content

Posts

Showing posts with the label MFC

Threaded multi Chat room client source code in c++ with MFC Part - 3

PREVIOUS ClientDlg.cpp is the class where all the action from user's are taken and initiated. The interaction with GUI to logic is so great and easy which will help any developer to understand the flow of code. Function for login -- void CClientDlg::OnBnClickedButton2() {     // TODO: Add your control notification handler code here     cTh = AfxBeginThread(     StaticThreadFunc,     this);         m_Thread_handle = cTh->m_hThread; } Start the thread for a login action. UINT __cdecl CClientDlg::StaticThreadFunc(LPVOID pParam) {     CClientDlg *pYourClass = reinterpret_cast<CClientDlg*>(pParam);     UINT retCode = pYourClass->ThreadFunc();     return retCode; } UINT CClientDlg::ThreadFunc() {     // Do your thing, this thread now has access to all the classes member variables     ...

Threaded multi Chat room client source code in c++ with MFC Part - 2

PREVIOUS                                                                                                                                NEXT Client.cpp class is the MFC GUI initiation class. You can have a look here .. BOOL CClientApp::InitInstance() {     // InitCommonControlsEx() is required on Windows XP if an application   ...

Threaded multi Chat room client source code in c++ with MFC Part - 1

As in my previous post I have talk about server , now it's time for client. This client will use as multi chat client with our very own server. In this client you need to provide server IP , PORT and an USERNAME . Then just click the log in button and you are in if you get an welcome message from server. Technology used: i.   C++ ii.   Microsoft visual Studio 2010 iii.  MFC GUI iv.  Winshock2 Socket v.   TCP/UDP vi.  Windows Thread This project consist of  3 classes. 1. Client.cpp 2. ClientDlg.cpp 3. ClientCon.cpp Lets start with core portion -- I mean the network level connection portion. I have used the Winshock2 library for the purpose of network communication. You can use this library on windows  with MVS 2010 by including library -- # include <winsock2.h> And as it is multi user  and have GUI so we need thread. For this I have used Windows thread. You can use this windows thr...

Threaded multi chat room server source code in c++ with MFC Part - 3

PREVIOUS   Now we will talk about the other MFC GUI portion which I have created and used threaded model. When in GUI the start button is clicked by providing port number on PORT text box this server start by creating a thread independent from GUI. You can start the server by ENTER also. Here is some code snap for help -- void CServerDlg::OnBnClickedButton1() {     // TODO: Add your control notification handler code here     cTh = AfxBeginThread(     StaticThreadFunc,     this);         //cTh->m_bAutoDelete = FALSE;     m_Thread_handle = cTh->m_hThread;     } Here we create a windows thread for back end action. UINT __cdecl CServerDlg::StaticThreadFunc(LPVOID pParam) {     CServerDlg *pYourClass = reinterpret_cast<CServerDlg*>(pParam);     UINT retCode = pYourClass->ThreadFunc(); ...

Threaded multi chat room server source code in c++ with MFC Part - 2

PREVIOUS  we will now demonstrate about the class Server.cpp . This class is the start point of the server project. It is basically used to call the MFC instance. There will be some preview of the class here as MFC is out of scope  at the moment -- BOOL CServerApp::InitInstance() {     // InitCommonControlsEx() is required on Windows XP if an application     // manifest specifies use of ComCtl32.dll version 6 or later to enable     // visual styles.  Otherwise, any window creation will fail.     INITCOMMONCONTROLSEX InitCtrls;     InitCtrls.dwSize = sizeof(InitCtrls);     // Set this to include all the common control classes you want to use     // in your application.     InitCtrls.dwICC = ICC_WIN95_CLASSES;     InitCommonControlsEx(&InitCtrls);     CWinApp::InitInstance();   ...

Threaded multi chat room server source code in c++ with MFC Part - 1

Multi-client chat server is open source as I have created it. In this server multiple user can log in and communicate with one another as multicast user. In reality it can use as a chat room server. Can be modified as peer to peer (p2p) text chat. It can also be used as a backbone of a video conferencing project. It has a beautiful and easy to use user interface in MFC. This project is compatible with Microsoft visual studio 2010. I will describe the use of class and function with source code. Technology used: i.   C++ ii.   Microsoft visual Studio 2010 iii.  MFC GUI iv.  Winshock2 Socket v.   TCP/UDP vi.  Windows Thread This project consist of  3 classes. 1. Server.cpp 2. ServerDlg.cpp 3. ServerManager.cpp Lets start with core portion -- I mean the network level connection portion. I have used the Winshock2 library for the purpose of network communication. You can use this library on windows  with MVS 2...

How to take input from a MFC Edit Control as string, convert it to integer and append text to it

 Take input from a MFC Control as string : CString txtname; GetDlgItemText(IDC_EDIT1, txtname); Here IDC_EDIT1 is the Control Edit Text and txtname is the input at the moment we call. Convert it to integer or int : int iPort = _atoi( txtname.GetString() ); int iPort = _wtoi( txtname.GetString() ); // if you use wide charater formats     Append text to Edit Control :   void CServerDlg::AppendTextToEditCtrl(CEdit& edit, LPCTSTR pszText) {    // get the initial text length    int nLength = edit.GetWindowTextLength();    // put the selection at the end of text    edit.SetSel(nLength, nLength);    // replace the selection    edit.ReplaceSel(pszText); }

How to stop a blocking thread from another function that start in other in MFC

ISSUE: In server client scenario it is mandatory to use thread where sever is listening on a particular port. Now in click of stop button ,how to stop this running thread. SOLUTION: #include <Windows.h> HANDLE m_Thread_handle; CWinThread *cTh; static UINT __cdecl StaticThreadFunc(LPVOID pParam); UINT ThreadFunc(); Step 1. Declared these on header file. Step 2. Starting the thread from a function or button click --- void CServerDlg::OnBnClickedStart() {     // TODO: Add your control notification handler code here     cTh = AfxBeginThread(     StaticThreadFunc,     this);     m_Thread_handle = cTh->m_hThread; } Step 3. Thread initialization function --  UINT __cdecl CServerDlg::StaticThreadFunc(LPVOID pParam) {     CServerDlg *pYourClass = reinterpret_cast<CServerDlg*>(pParam);     UINT retCode = pYourClass->ThreadFunc();   ...

GUI hanged on button pressed or some action initiation in MFC project

ISSUE: In a GUI project there is lots of calculation and works are needed to be done in background when necessary . So when a button pressed or a certain action initiate the GUI may stop responding for you. SOLUTION: In default when a MFC project is created the GUI portion has thread. So make sure the other necessary works are done on another thread (eg: calculation, process, response , internet query ). You can have a better and great idea about thread use in mfc from this simple post

How to convert between 'CString' and 'std::string' ?

ISSUE: When you are developing MFC project in most cases it becomes necessary to convert between 'CString' and 'std::string' . SOLUTION: 'CString' to 'std::string': CString sTest ( "Matrix" ); // Convert a TCHAR string to a LPCSTR CT2CA CStringToAscii(sTest); // construct a std::string using the LPCSTR input std::string sResultedString (CStringToAscii); 'std::string' to 'CString': std::string sTest( " Matrix " ); CString ResultedCString(sTest.c_str());  

IntelliSense: #error directive: Please use the /MD switch for _AFXDLL builds in Visual Studio 2010 MFC project

ERROR:     error C2001: newline in constant IntelliSense: #error directive: Please use the /MD switch for _AFXDLL builds    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxver_.h    81    3     Solution: There's a bug where the settings of a generated MFC project would show "Multi-threaded Debug DLL (/MDd)"; however, it doesn't pass that argument to the intellisense engine. To workaround this problem, please try the following: Step 1. Right-click the Project. Step 2. Go to Config Properties->C/C++-> Code Gen ->. Double-click "Runtime Library" and set to "Multi-threaded Debug DLL (/MDd)" . If this value already appears to be set, make sure it is by selecting it again (it should then appear as bold). Step 3. Click OK. Done!