Question

Have prepared such function, where some WSA functions will be used as callback:

int StartWinSock(int (*WSAStartup)(WORD, LPWSADATA))
{

}

But when in other code, I'm trying to launch it:

StartWinSock(WSAStartup);

I'm getting an error:

'WSClient::StartWinSock' : cannot convert parameter 1 from 'int (__stdcall *)(WORD,LPWSADATA)' to 'int (__cdecl *)(WORD,LPWSADATA)'

Also, I don't know how to pass parameters correctly through callback function like WSAStartup() ( its parameters: WORD ( unsigned short number of version ) && LPWSADATA ( reference to WSAData ) ).

Was it helpful?

Solution

You are missing the __stdcall calling convention on the function pointer type, which comes from the WINAPI macro. The compiler is therefore assuming the default __cdecl calling convention for this pointer. The two calling conventions are not compatible.

Consider creating this typedef:

typedef int WINAPI (*WSAStartupCallback)(WORD, LPWSADATA);

Then declare your function like this:

int StartWinSock(WSAStartupCallback wsaStartup)
{
}

You should then be able to call this function with the external WSAStartup pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top