Cant find how to do this asm code in c++...

Started by
8 comments, last by h8CplusplusGuru 6 years, 5 months ago

Hi guys, i know this is stupid but i've been trying to convert this block of asm code in c++ for an hour or two and im stuck :)


//////////////////////////////////////////////////////////////////////////////////////////////
/////// This routine write the value returned by GetProcAddress() at the address p ///////////
//////////////////////////////////////////////////////////////////////////////////////////////
bool SetProcAddress(HINSTANCE dll, void *p, char *name)
{
	UINT *res = (UINT*)ptr;
	
	void *f = GetProcAddress(dll, name);
	if(!f)
		return false;

	_asm {
		push ebx
		push edx
	
		mov ebx, f
		mov edx, p

		mov [ebx], edx  // <--- put edx at the address pointed by ebx 

		pop edx
		pop ebx
	}

	return res != 0;
}

...

// ie:
SetProcAddress(hDll, &some_function, "function_name");

I tried:


memcmp(p, f, sizeof(p));

and

UINT *i1 = (*UINT)p;
UINT *i2 = (*UINT)f;

*f = *p;

The first one dosent seem to give the right retult, and the second one won't compile.

Any idea?

Advertisement
2 minutes ago, Vortez said:

second one won't compile

When something doesn't compile, please post the exact compile error.

Hello to all my stalkers.

Okay, i fixed the code at bit, i tested the asm part and it work (the first post had a last minute change bug...)

Here's the code:


#define USE_ASM
#define USE_INTEGERS
#define USE_MEMCPY

//////////////////////////////////////////////////////////////////////////////////////////////
/////// This routine write the value returned by GetProcAddress() at the address p ///////////
//////////////////////////////////////////////////////////////////////////////////////////////
bool SetProcAddress(HINSTANCE dll, void *p, char *name)
{
	UINT *x = (UINT*)p;  // <--- Used for debugging (if *x change to the value of f in the watch, it mean it work)
	void *f = GetProcAddress(dll, name);
	if(!f)
		return false;

	#ifdef USE_ASM // This work...
		_asm {
			push eax
			push ebx
	
			mov eax, f
			mov ebx, p

			mov [ebx], eax  // <--- put eax at the address pointed by ebx 

			pop ebx
			pop eax
		}
	#endif

	#ifdef USE_INTEGERS
		*x = 0;

		UINT *x1 = (UINT*)p;
		UINT *x2 = (UINT*)f;

		(*x1) = (*x2); // this give the value at address f, so it doesn't work
	#endif

	#ifdef USE_MEMCPY // this give save as above
		*x = 0;
		memcpy(p, f, sizeof(p));
	#endif

	return *x > 0;
}

 

Here's some screenshots that show what i mean.

Screenshot #1

Screenshot #2

You do realize that mov [ebx], eax will move the value IN eax to the block of memory ebx points to, right?

Meanwhile, memcpy will copy the memory pointed at by f into the memory pointed at by p.

Lastly, your integer solution has the same problem as the memcpy one.

 

I'm not going to give you the solution, because figuring this out is an important step in understanding pointers.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

"You do realize that mov [ebx], eax will move the value IN eax to the block of memory ebx points to, right?"

That's what he said.  "put eax at the address pointed by ebx"

 

Also, what the FUCK is up with this site's 'quote' function?  It's completely FUBAR in Chrome for Desktop.

You need to re-read the concept of dereferencing c++ pointers.


bool SetProcAddress(void* p)
{
	void *r = p; // <--- Used for debugging (if *r change to the value of f in the watch, it mean it work)
	ULONG func=0;
	void *f = &func;//

    std::cout << " f = &func = " << (ULONG)f;

/*		#ifdef USE_ASM // This work...
		_asm {
			push eax
			push ebx
	
			mov eax, f
			mov ebx, p

			mov [ebx], eax  // <--- put eax at the address pointed by ebx 

			pop ebx
			pop eax
		}
	#endif
*/	

	#ifdef USE_INTEGERS
        ULONG* r1 = (ULONG*)r;
        
    	ULONG address = (ULONG)f;  
    	
    	void* p1 = p; 
	    *(ULONG*)p1 = address; //cast on left
        
        std::cout << "; " << *r1 << ", " << address << std::endl;

	#endif

	#ifdef USE_MEMCPY // this give save as above
        ULONG* r2 = (ULONG*)r;  
	  
	    void* dest = p;
	    void* source = f;
	    
        memcpy( (void*)(*(ULONG*)dest), source, sizeof(dest) );
	#endif

    std::cout << "*r2= " << *r2 << ", " << (ULONG)source << std::endl;

	return p1 == (void*)r1 && dest == (void*)r2;
}

So if this is correct, it turns out you need to do a lot of casting mayhem.

What is this function trying to accomplish, exactly?

edit: Never mind, read the comment above it.

It's a scratch function that should do the assembly but in c++, one way through assignment other through memcpy, and such that the value of r gets changed to f each time. Or I Think that's what's going on!

This topic is closed to new replies.

Advertisement