APIWrapper [Inline Assembler]



  • #include <map>
    #include <vector>
    using namespace std;
    
    typedef struct {
    	char * cszFilename;
    	HMODULE hHandle;
    } tDLL;
    std::map<int, tDLL*> pDLL;
    
    int FindFreeHandle() {
    	if(pDLL.size() == 0) {
            return 0x01;
    	} else {
            return (pDLL.end()->first)+1;
    	}
    }
    
    unsigned int API_Wrapper LoadDLL(const char *cszFilename) {
    	tDLL * pSubDLL = new tDLL;
    	memset(pSubDLL,0,sizeof(tDLL));
    	_cprintf("Loading Library\n");
    	HMODULE hMod = LoadLibraryA((LPCSTR)cszFilename);
    	int handle = FindFreeHandle();
    	if(hMod) {
    		pSubDLL->cszFilename = new char[strlen(cszFilename)];
    		strcpy(pSubDLL->cszFilename, cszFilename);
    		_cprintf("File: %s\n", pSubDLL->cszFilename);
    		pSubDLL->hHandle = hMod;
    		_cprintf("Handle: %u | %u\n", pSubDLL->hHandle, hMod);
    		pDLL[handle] = pSubDLL;
    		return handle;
    	} else {
    		return NULL;
    	}
    }
    
    void * API_Wrapper CallDLL(unsigned int uHandle, unsigned int uCallConvention, const char *cszFunction, ...) {
    	vector<void*> args;
    
    	va_list va;
    	void * aPointer;
    	void * tmpEax;
    	int n = 0;
    	std::map<int, tDLL*>::iterator itr = pDLL.find((int)uHandle);
    	if(itr != pDLL.end()) {
    		HMODULE hMod = itr->second->hHandle;
    		_cprintf("Calling (%s | %s): %u\n", itr->second->cszFilename, cszFunction, hMod);
    		__asm {
    			push cszFunction;
    			push hMod;
    			call dword ptr [GetProcAddress];
    			cmp eax,0;
    			jz func_end;
    			mov tmpEax, eax
    		}
    		va_start(va, cszFunction);
    		printf("Arglist: %p", va);
    		if(va) {
    			do {
    				aPointer = (void*)va_arg(va, void*);
    				if(aPointer) {
    					args.push_back(aPointer);
    					n += 4;
    				}
    			} while (aPointer);
    		}
    		va_end(va);
    
    		for(int i = args.size(); i = 0; i = i -1) {
    			if(args[i] != 0) {
    				void * pArg = (void*)args[i];
    				__asm {
    					push pArg;
    				}
    			}
    		}
    		__asm {
    			mov eax, tmpEax;
    			call eax;
    		}
    		if(uCallConvention == 0x01) {
    			if(n != 0) {
    				__asm {
    					add esp, n;
    				}
    			}
    		}
    		__asm {
    func_end:
    			retn;
    		}
    	} else {
    		return NULL;
    	}
    	return NULL; //Nothing worked?
    }
    
    unsigned int CloseDLL(int uHandle) {
    	std::map<int, tDLL*>::iterator itr = pDLL.find((int)uHandle);
    	if(itr != pDLL.end()) {
    		bool ret = FreeLibrary(itr->second->hHandle);
    		pDLL.erase(itr);
    		return (int)ret;
    	}
    	return 0;
    }
    

    Nunja ich versuche das ganze so ablaufen zu lassen

    ich nehme mal ein VB Beispiel

    Dim lngPtr as long
    lngPtr = LoadDLL("kernel32.dll")
    MsgBox CallDLL(lngPtr, 2, "IsDebuggerPresent", ürgendwelche, argumente, 0)
    CloseDLL(lngPtr)
    

    DLL wird geladen, ein Export wird aufgerufen und übergibt den wert, dll handle schliessen. Das problem ist jetzt es stürzt ürgendwie immer ab? Soviel ich jetzt weis ist das eax den richtigen wert/string was auch immer hat. Nur kommt es nicht zu übergabe von den Programm wo den Export aufruft. Und ich würde mal gern wissen ob es möglich ist die Argumenten liste zu loopen ohne eine 0 zu übergeben am ende?

    Vlt hat einer eine idee

    Thx im vorraus. 👍



  • Noe oda?


Anmelden zum Antworten