0

This is my first time dealing with windows. I tried to create thread, but I can't compile my code.

This is a part of my code:

WORD _tid;
HANDLE  readerThread= CreateThread(0,0,readFromPort,0,0,&_tid); 
DWORD WINAPI readFromPort(LPVOID lpParam ){}

I also tried :

LPDWORD_tid;
HANDLE  readerThread= CreateThread(0,0,readFromPort,0,0,_tid); 
DWORD WINAPI readFromPort(LPVOID lpParam ){}

Neither compiled. onyone knows what is the problem?

(I'm using windows xp)

thanks.

kakush
  • 3,334
  • 14
  • 47
  • 68
  • 1
    well, what's the error? You need let us know. and better a simple example code – RolandXu Jun 03 '12 at 14:31
  • Please use `_beginthreadex` instead of `CreateThread` (if you're using the CRT library, which you will usually unknowingly do). – demorge Jun 03 '12 at 18:21

3 Answers3

2

well, don't know what's error you have. But can provide a good example to you

#include "windows.h"
DWORD WINAPI readFromPort(LPVOID lpParam ){return 0;}
int _tmain(int argc, _TCHAR* argv[])
{
    DWORD _tid;
    HANDLE  readerThread= CreateThread(0,0,readFromPort,0,0,&_tid); 
    return 0;
}

difference to yours:

  1. _tid is DWORD, not WORD.

  2. the readFromPort returns value 0.

RolandXu
  • 3,566
  • 2
  • 17
  • 23
1

Let me guess: readFromPort must return a value?

Next time please add the error also.

Alexandru C.
  • 3,337
  • 1
  • 25
  • 27
0

Well, it would have been nice if you have provided a minimal code example and at least a compilation error. But fine, I will pretend to be a compiler. Assuming that your code snippet is inside a function, you have a function declaration in there:

DWORD WINAPI readFromPort(LPVOID lpParam ){}

I believe that is screwing you up. You have to declare/define functions outside other function's scope.

Also, readFromPort function is declared after the call to CreateThread where it is being referenced as a parameter. So it won't work even if you put all of that in a global scope (which is a bad idea in itself).