About calling natives from C
#9

These methods are essentially doing the exact same thing that Peter's function does, only his version finds the native's address automatically so that you don't have to (however, the native must be present in an AMX interface for this to work). Here is a snippet based on his code:

Код:
typedef int
	(* amx_Function_t)(AMX * amx, cell * params);

int IsPlayerConnected(int playerID)
{
	int
		amx_idx;
	amx_FindNative(pAMX, "IsPlayerConnected", &amx_idx);
	if (amx_idx != 2147483647)
	{
		AMX_HEADER
			* amx_hdr = (AMX_HEADER *)(pAMX)->base;
		unsigned int
			amx_addr = (unsigned int)((AMX_FUNCSTUB *)((char *)amx_hdr + amx_hdr->natives + amx_hdr->defsize * amx_idx))->address;
		if (!amx_addr)
		{
			return 0;
		}
		amx_Function_t
			amx_Function = (amx_Function_t)amx_addr;
		cell
			params[2];
		params[0] = 4;
		params[1] = playerID;
		return amx_Function(pAMX, params);
	}
	return 0;
}
Of course, it gets slightly more complex if you want to pass strings or values by reference (you need to allocate and free memory).

I suppose this is a question of whether you want to compromise a little speed for convenience, because remember that the addresses will change if the server is recompiled.
Reply


Messages In This Thread
About calling natives from C - by Streetplaya - 01.12.2009, 17:02
Re: About calling natives from C - by Google63 - 01.12.2009, 17:29
Re: About calling natives from C - by Streetplaya - 01.12.2009, 17:35
Re: About calling natives from C - by Google63 - 01.12.2009, 17:46
Re: About calling natives from C - by Streetplaya - 01.12.2009, 18:11
Re: About calling natives from C - by Google63 - 01.12.2009, 18:39
Re: About calling natives from C - by Streetplaya - 02.12.2009, 12:18
Re: About calling natives from C - by Toribio - 02.12.2009, 16:56
Re: About calling natives from C - by Incognito - 02.12.2009, 17:31
Re: About calling natives from C - by Streetplaya - 02.12.2009, 17:42

Forum Jump:


Users browsing this thread: 3 Guest(s)