How to call static DWORD WINAPI ThreadFunc(LPVOID pvParam)
I am facing a problem:
I have say 3 files :
ABC.h -- this file has the function prototypes
ABC.cpp -- the definitions of the functions
ABC_Test.cpp --- the file with main() which calls the functions
using ABC's object say "ob".
Now I want to make a function say "startNewThread()" inside ABC.cpp and
its prototype in ABC.h, then where should I define
static DWORD WINAPI ThreadFunc(LPVOID pvParam);
function. Inside ABC_Test.cpp or inside ABC.cpp
Also Iwill call the createThread() of Win API inside startNewThread()
and pass the ThreadFunc as one of its parameters. If this is possible,
then its OK. But if I have to call some function of mine say
void ABC :: printXYZ() inside ThreadFunc , then how can I do this. I do
not have my object say ABC ob; i.e. "ob" inside my ThreadFunc.
Please reply, if possible with a EXAMPLE code
Pawan
# 1 Re: How to call static DWORD WINAPI ThreadFunc(LPVOID pvParam)
Whu is this function declared static? Static functions (not member functions, only ordinary functions) are deprecated. If you want to restrict the access to that function to a single source file, declare it inside an unnames namespace. Otherwise, get rid of the static.
As a rule, you define functions in a .cpp file (but not the one in which main() is defined -- any other .cpp file will do). The prototype of a function, i.e.,
DWORD WINAPI ThreadFunc(LPVOID pvParam);
Should be in a dedicated .h file. Either a separate file for this declaration or an existing .h file.
These rules apply to regular fuctions and classes. Templates work differently.
Danny at 2007-11-11 20:58:51 >

# 2 Re: How to call static DWORD WINAPI ThreadFunc(LPVOID pvParam)
I removed the static from my DWORD.....
but I still face problems.
Here's everything in more detail :
The 3 file are :
1) GridServer.h containing function prototypes
DWORD WINAPI runThread(LPVOID Parameter);
void startNewThread(SOCKET c_socket);
2) GridServer.cpp -- the .cpp file with function definitions
DWORD WINAPI GridServer :: runThread(LPVOID Parameter)
{
//Get the information about client entity
SOCKET clientSocket = (SOCKET)Parameter;
printf( "\n New Client Connected.\n");
cout.flush();
int bytesRecv = SOCKET_ERROR;
char *recvbuf;
recvbuf = new char[1];
int ch ;
bytesRecv = recv( clientSocket, recvbuf, 1, 0 );
cout.flush();
printf( "\n Bytes Received: %ld", bytesRecv );
printf("\n Data Received = %c", recvbuf[0]);
ch = atoi(recvbuf);
switch(ch)
{
case 1 : getFile(clientSocket);
break;
case 2 : createAccount(clientSocket,TABLE_OF_INTEREST);
break;
default : printf("\n Invalid option sent from client.");
break;
}
return 0;
}
/******************/
void GridServer :: startNewThread(SOCKET c_socket)
{
HANDLE hThread; //Handle to thread
DWORD ThreadId; //used to store the thread id
hThread = CreateThread( NULL,
0,
&GridServer::runThread,
(LPVOID)c_socket,
0,
&ThreadId);
//printf("\n After CreateThread()");
return;
}
and
3) GridServer_Test.cpp -- the .cpp with main()
it makes a GridServer object named : "ob" and calls ob.startNewThread(acceptSocket);
here acceptSocket is a variable of type SOCKET.
now the problems are :
when I compile the code, I get the following error :
g:\smartsafe\gridserver\gridserver.cpp(207) : error C2664: 'CreateThread' : cannot convert parameter 3 from 'DWORD (__stdcall GridServer::* )(LPVOID)' to 'LPTHREAD_START_ROUTINE'
There is no context in which this conversion is possible
What should I do?
What I require are :
1) calling the runThread() function properly from startNewThread() using CreateThread().
2)Also if you can see that I am calling getFile() and createAccount() from inside the runThread() I want this to be called, with respect to the object which called startNewThread().
I think I have tried to make my code clear this time.
Please Reply, if more information is required, I will reply
Pawan
# 3 Re: How to call static DWORD WINAPI ThreadFunc(LPVOID pvParam)
So these functions are member functions of a class, they are not independent functions. In that case, you have to declare them inside their class. You can define them in as separate .cpp file as you already have done but the .h file must include the declaration of class GridServer and its member functions prototypes.
Also, what I said about static applied to ordinary functions. When used in the context of a member function, static has a different meaning. You probably need to add it back to the member functions. Remember: there's a huge difference between a function and a member function. A member function belong to a class. It has access to protected and private memebers of a class. An ordinary function doesn't.
Danny at 2007-11-11 21:01:01 >

# 4 Re: How to call static DWORD WINAPI ThreadFunc(LPVOID pvParam)
I have declared them inside their .h file i.e. Grid Server.h and done their definition in GridServer.cpp.
I think I am still not able to make myself clear. I have posted my requirement in the post as:
What I require are :
1) calling the runThread() function properly from startNewThread() using CreateThread().
2)Also if you can see that I am calling getFile() and createAccount() from inside the runThread() I want this to be called, with respect to the object which called startNewThread().
Please reply how to do these 2 things.
Pawan
# 5 Re: How to call static DWORD WINAPI ThreadFunc(LPVOID pvParam)
I assume you have a problem with this call:
hThread = CreateThread( NULL,
0,
&GridServer::runThread,
(LPVOID)c_socket,
0,
&ThreadId);
The problem is that &GridServer::runThread doesn't evaluate to an ordinary function. Rather, it's a member function. To make this call work, replace the declaration of GridServer::runThread() to
static DOWRD GridServer::runThread(...)
Also, you may want to consult the following articles to learn about pointers to member functions:
http://www.dev-archive.com/dev-archive/LegacyLink/9465
http://www.dev-archive.com/dev-archive/LegacyLink/9483
Danny at 2007-11-11 21:02:55 >
