how to create a thread in c ++ with windows using _beginthreadex?

0

I am trying to create a thread within a member function of a class with _beginthreadex.

void CapturaDeRed::startCapture() {
    unsigned threadID;
    HANDLE hTread;
     MyData *data=NULL;
    data->ifaceName = configCapture();
    data->s1 = configSniffer();
    //creo el hil y lo asigno
    hTread = (HANDLE)_beginthreadex(NULL, 0, &CapturarPacket1, data, 0, &threadID);
} 

But the compiler gives me the following error:

  

Severity Code Description Project File Line Suppression State   Error (active) argument of type "unsigned int (__stdcall CaptureDeRed :: *) (void * ap)" is incompatible with parameter of type "_beginthreadex_proc_type"

I put all the code of the implementation:

#include "..\Include\CapturaDeRed.h"

using namespace Tins;

CapturaDeRed::CapturaDeRed() 
{

};

CapturaDeRed::~CapturaDeRed()
{
}

unsigned  __stdcall CapturaDeRed::CapturarPacket1( void *ap ) 
{
    dta =  (MyData*)ap;
    Sniffer snfi(dta->ifaceName.name(), dta->s1);
    Packet pack(snfi.next_packet());
    do {
        const TCP & tcp = pack.pdu()->rfind_pdu<TCP>();
        TCP tcp1 = TCP(tcp);
    } while (true);
    _endthreadex(0);
    //return 0;
}

void CapturaDeRed::clasificarPacket() {
}

const NetworkInterface CapturaDeRed::configCapture() {
    NetworkInterface iface = NetworkInterface::default_interface();
    return iface;
}

const SnifferConfiguration CapturaDeRed::configSniffer() {
    SnifferConfiguration sc;
    sc.set_promisc_mode(true);
    sc.set_snap_len(64 *1024);
    sc.set_filter("ip and tcp");
    return sc;
}

void CapturaDeRed::startCapture() {
    unsigned threadID;
    HANDLE hTread;
    MyData *data=NULL;
    data->ifaceName = configCapture();
    data->s1 = configSniffer();
    //creo el hil y lo asigno
    hTread = (HANDLE)_beginthreadex(NULL, 0, &CapturarPacket1, data, 0, &threadID);
}

I appreciate your help and guidance.

    
asked by alvaroc 05.09.2016 в 18:07
source

1 answer

0

The rule here is that the code you put in must be minimal and compile, which is not the case. Anyway, something you should keep in mind is that a function pointer is not the same type of beast as a non-static member function pointer, hence the error message

  

"unsigned int (__stdcall CaptureDeRed :: *) (void * ap)" is incompatible   with parameter of type "_beginthreadex_proc_type"

So one of two, or you declare CapturePacket1 () as static

  

static unsigned __stdcall CapturePacket1 (void * ap);

or doing a normal function.

    
answered by 06.09.2016 / 03:17
source