Jump to content
  • Advertisement
Sign in to follow this  
Vortez

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
2 minutes ago, Vortez said:

second one won't compile

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

Share this post


Link to post
Share on other sites

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

Edited by Vortez

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

"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.

Share this post


Link to post
Share on other sites
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.

Edited by h8CplusplusGuru
changed an int

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!