C++ - CreateThread -compilation error
-
12-06-2021 - |
Question
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.
Solution
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:
_tid is DWORD, not WORD.
the readFromPort returns value 0.
OTHER TIPS
Let me guess: readFromPort
must return a value?
Next time please add the error also.
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).