Thread: getting values off the stack back to c++

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    getting values off the stack back to c++

    //i want to get the values of message box from the stack back to c++, can someone plz help

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    
    int main()
    {
        char *arg1Address; 
    	__asm 
    	{ 
    		mov eax, [ebp+4]
    		mov arg1Address , eax
    	}
    
    	MessageBox(NULL,"Asm","",0);
    
    //0040104A   |.  6A 00                   PUSH 0                                          ; /Style = MB_OK|MB_APPLMODAL
    //0040104C   |.  68 20104300             PUSH ffff.00431020                              ; |Title = ""
    //00401051   |.  68 1C104300             PUSH ffff.0043101C                              ; |Text = "Asm"
    //00401056   |.  6A 00                   PUSH 0                                          ; |hOwner = NULL
    //00401058   |.  FF15 D4B24300           CALL DWORD PTR DS:[<&USER32.MessageBoxA>]       ; \MessageBoxA
    
    
    	return 0;
    
    }
    Last edited by Anddos; 04-30-2009 at 02:08 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you actually want to achieve?

    There is not even a guarantee that EBP in this case is pointing to the stack - that is convention, but it's entirely up to the compiler to decide what EBP is used for. Neither can you use ESP without depending on what the compiler does, and it's quite likely that a change to the settings in the compiler (e.g. optimization setting) will change what code the compiler generates, and thus the relative distance between current ESP and stuff on the stack before the current function.

    So explain what you ACTUALLY want to do, and we can probably explain how to do it, but your current scheme seems unlikely to work, whatever it is you are trying to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    i want to get the "Asm" data string off the stack back to a c++ varible
    Last edited by Anddos; 04-30-2009 at 02:33 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean the one that hasn't been pushed yet, because you haven't got to the call to MessageBox?

    The characters in "Asm" is not stored on the stack - it's stored in the Read Only Data section.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    ok so how do i get it in to the char*?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Windows functions use the _stdcall calling convention, which means that the called function will clean the stack after it has been called. It'd be very very hard to try and find the argument in memory after it is no longer in the stack (and that's if it hasn't been written over. I'm also not sure if it would cause a segmentation fault if you even tried).

    To assign "Asm" to 'arg1address' you would use:
    Code:
     char *arg1address = "Asm";
    After that, I'm not quite sure what you're trying to do with the inline assembly, what you're doing seems rather pointless (assuming it's in Intel Syntax).

    Please try and explain what you are trying to do a little clearer.
    Last edited by DeadPlanet; 04-30-2009 at 03:54 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably need to explain a bit more "big picture" of what you actually are trying to achieve.

    Right now it's like you are asking how to remove a wheel-nut on a car-wheel, when what you ACTUALLY want to do is fix a puncture.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Change
    Code:
    MessageBox(NULL,"Asm","",0);
    to
    Code:
    char str[] = "Asm";
    MessageBox(NULL,str,"",0);
    Now you can use str anywhere else you need it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Linked list Stack question
    By lyrick in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2005, 06:23 AM
  3. problem of garbage values
    By aldajlo in forum C Programming
    Replies: 5
    Last Post: 10-02-2004, 04:41 PM
  4. Stack
    By planet_abhi in forum C Programming
    Replies: 2
    Last Post: 04-12-2003, 04:22 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM