Search code examples
mfcbeginthreadex

issue with _beginthreadex used with MFC class


Can't use _beginthreadex in MFC giving error as error C3861: '_beginthreadex': identifier not found beginthreadex working fine without MFC code.

Edit:Issue was with file inclusion sequence and need was to use _beginthreadex

        // EventsHandshakeDlg.cpp : implementation file
            //
            #include <crtdefs.h>
            #include <process.h>
            #include"windowsx.h"
            #include "stdafx.h"
    unsigned int __stdcall/*AFX_THREADPROC*/ /*__cdecl*/ Server(void *iData)
                {

                }
            // CEventsHandshakeDlg dialogCEventsHandshakeDlg::CEventsHandshakeDlg(CWnd* pParent /*=NULL*/)
                : CDialog(CEventsHandshakeDlg::IDD, pParent)
            {
                m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
            }
        BEGIN_MESSAGE_MAP(CEventsHandshakeDlg, CDialog)
                ON_WM_SYSCOMMAND()
                ON_WM_PAINT()
                ON_WM_QUERYDRAGICON()
                //}}AFX_MSG_MAP
                ON_BN_CLICKED(IDC_BTN_REVERSE, &CEventsHandshakeDlg::OnBnClickedBtnReverse)
            END_MESSAGE_MAP()



            // CEventsHandshakeDlg message handlers

            BOOL CEventsHandshakeDlg::OnInitDialog()
            {

                // TODO: Add extra initialization here

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

          void CEventsHandshakeDlg::OnBnClickedBtnReverse()
                {unsigned int threadId = 0;
                HANDLE Thread =(HANDLE)_beginthreadex(
                    NULL,
                    NULL,
                    &/*CEventsHandshakeDlg::*/Server/*(void *iData)*/,
                    NULL,
                    0,
                    &threadId
                    );
}

Solution

  • Usually AfxBeginThread() is used in MFC. Are you sure you need _beginthreadex()? All your parameters seem like defaults.

    You might want to check the order of your #includes and the contents of stdafx.h and/or check your include directories. You also need to include <process.h> after stdafx.h. That might fix it.