Archived

This topic is now archived and is closed to further replies.

zackriggle

Cast from void* to function pointer

Recommended Posts

zackriggle    130
How do I cast from a void* to a PTHREAD_START_ROUTING (something pass-able to CreateThread())? EDIT: PTHREAD_START_ROUTINE, not ..._ROUTING [edited by - zackriggle on January 22, 2004 9:51:00 PM]

Share this post


Link to post
Share on other sites
LessBread    1415
Have you tried something like this:

void *ptr = ...
PTHREAD_START_ROUTING pThread = (PTHREAD_START_ROUTING)ptr;

(perhaps someone else will supply the appropriate C++ cast).

Is the targetted thread function declared using the standard calling convention? (_stdcall, aka WINAPI). Passing a cdecl function can cause problems.

Are you confronting a specific problem?

Share this post


Link to post
Share on other sites
Fruny    1658
PTHREAD_START_ROUTING pThread = static_cast<PTHREAD_START_ROUTING>(ptr);


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites
zackriggle    130
Sorry about that, there was a typo. Shouldn''t be PTHREAD_START_ROUTING, it should be PTHREAD_START_ROUTINE (which is typedef''d as a DWORD WINAPI function-pointer with 1 param [a void*]).

Sorry about the confusion there. I just can''t seem to cast a void* into the appropriate function type (casting as a PTHREAD_... doesn''t do the job).

Share this post


Link to post
Share on other sites
Ranok    122
If the PTHREAD_START_ROUTINE has the form


int PTHREAD_START_ROUTINE( int, int )

then do:

typedef int (*PTHREAD_START_FUNC)(int,int);
PTHREAD_START_FUNC pFunc = (PTHREAD_START_FUNC)ptr;


[edited by - Ranok on January 22, 2004 9:59:07 PM]

Share this post


Link to post
Share on other sites