Archived

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

zackriggle

Cast from void* to function pointer

Recommended Posts

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
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
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
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