Thread Error Borland C++

   // Create thread for listening
    DWORD dwThreadId;
    HANDLE hthread = CreateThread(
        0,           //Security attribute
        0,           //Initial Stack
        Listen,  //Starting address of thread
        0,           // argument of thread
        0,           // Create flags
        &dwThreadId);  // thread ID

When I try to compile this I come across to errors.

Both errors occur on the last line of code &dwThreadID);

[C++ Error] Unit1.cpp(307): E2034 Cannot convert 'int (* (_closure )())()' to 'unsigned long (__stdcall *)(void *)'
[C++ Error] Unit1.cpp(307): E2342 Type mismatch in parameter 'lpStartAddress' (wanted 'unsigned long (__stdcall *)(void *)', got 'void')
LVL 2
corduroy9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
What's the declaration of 'Listen()'? It has to be like

DWORD WINAPI Listen(
  LPVOID lpParameter   // thread data
);

Nothing else is allowed.
corduroy9Author Commented:
Ok well that seemed to work.  But if I make a call to a function.  Lets say Recv within Listen.

DWORD WINAPI Listen(
  LPVOID lpParameter   // thread data

  Recv();
);

I get an error called to undefined function Recv();

class TForm1 : public TForm
{
__published:      // IDE-managed Components
    void __fastcall FormCreate(TObject *Sender);

private:      // User declarations

public:            // User declarations
    int             Recv();
};

Why doesn't that work?
jkrCommented:
You need to pass a pointer to the object the thread should work on when creating it, e.g.

TForm1 form;

    HANDLE hthread = CreateThread(
       0,           //Security attribute
       0,           //Initial Stack
       Listen,  //Starting address of thread
       (LPVOID) &form,           // argument of thread
       0,           // Create flags
       &dwThreadId);  // thread ID

//...


DWORD WINAPI Listen(LPVOID lpv)
{

TForm1* pf = (TForm1*) lpv;

 lpv->Recv();
);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
BTW, if you're using the latter techniqhe, you could also make 'Listen()' a static member of 'TForm1', e.g.

class TForm1 : public TForm
{
__published:     // IDE-managed Components
   void __fastcall FormCreate(TObject *Sender);

private:     // User declarations

public:          // User declarations
   int             Recv();
static DWORD WINAPI Listen(LPVOID lpv);
};
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.