Archived

This topic is now archived and is closed to further replies.

Someone explain this to me please.

This topic is 5011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why does this:
DWORD CallFunction(DWORD func,size_t sz,const void* args)
{
    DWORD vreturn;
    __asm
    {
        mov   ecx, sz       // get size of buffer

        mov   esi, args     // get buffer

        sub   esp, ecx      // allocate stack space

        mov   edi, esp      // start of destination stack frame

        shr   ecx, 2        // make it dwords

        rep   movsd         // copy params to real stack

        call  [func]        // call the function

        mov   vreturn,  eax // save the return value

        add   esp, sz       // restore the stack pointer

    }
    return vreturn;
}

.......
.......

	struct
	{
		int n;
		char* str;
		char* ti;
		int ty;
	} t;
	t.str=new char[8];
	sprintf(t.str,"123t456");
	t.ti=new char[8];
	sprintf(t.ti,"title23");
	t.n=0;
	t.ty=MB_OK;
	CallFunction((DWORD)MessageBox,24,&t);
tells me that "the value of esp wasn''t correctly saved across a function call.... blablabla..."? MessageBox is declared as _cdecl, so it should be ok right?
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"

Share this post


Link to post
Share on other sites
Oh I got, sz is 16bytes not 24bytes of course and MessageBox is _stdcall so there is no nedd to add esp, sz, mystery solved move along.



"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"



[edited by - CPPMaster Poppet on March 24, 2004 7:18:46 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by fredizzimo
Why does this thread appear in the lounge instead of the general programming forum?


Because I''m too lazy to delete 3 and type 21.



"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"

Share this post


Link to post
Share on other sites