View Single Post
  #1  
Old 03-19-2018, 17:30
0xall0c 0xall0c is offline
Friend
 
Join Date: Mar 2018
Posts: 67
Rept. Given: 0
Rept. Rcvd 4 Times in 3 Posts
Thanks Given: 25
Thanks Rcvd at 65 Times in 35 Posts
0xall0c Reputation: 4
[C] Winapi Call Dynamically and easily

This Small Function Let You call winapi dynamically, without having to define function definition, or writing nonsense wrappers.

Code:
void* DynCall(void *ptr, ...)
{
	char* function;
	char* library;
	va_list va;
	void *p;
	int i = 0;
	DWORD argBuf[32];
	DWORD ret;
	HANDLE lib;

	va_start(va, ptr);
	library = ptr;
	function = va_arg(va, void*);
	p = va_arg(va, void *);
	if(!(lib = GetModuleHandleA(library)))
		lib = _LoadLibraryA(library);
	if (!lib)
		error("Cant load libarary %s", library);
	void *funcAddress = _GetProcAddress(lib, function);
	if (!funcAddress)
		error("Cant Find Dynamic Address %s", function);
	for (; p != 0xb33f; p = va_arg(va, void *)) {
		argBuf[i++] = p;
	}
	
	for (i--; i >= 0; i--)
	{
		p = argBuf[i];
		_asm {
			push p
		}
	}
	va_end(va);
	_asm {
		push lb
		jmp funcAddress
	lb:
		mov ret, eax
	}
	return ret;
}
Example :

Code:
DWORD dwResult = DynCall("ntdll.dll","NtUnmapViewOfSection",
					PI.hProcess,
					(LPVOID)(NtHeader->OptionalHeader.ImageBase),0xb33f
				);
Dont forget to add one extra paramater at end i.e 0xb33f

I think it will be useful to someone.
Reply With Quote
The Following User Gave Reputation+1 to 0xall0c For This Useful Post:
mr.exodia (03-20-2018)
The Following 8 Users Say Thank You to 0xall0c For This Useful Post:
dosprog (04-04-2018), Indigo (07-19-2019), niculaita (03-20-2018), nimaarek (10-18-2018), ontryit (04-03-2018), schrodyn (03-11-2019), tonyweb (03-21-2018), yoza (07-28-2019)