Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Really Not An Easy Ques. !!!!!!!!

Really Not An Easy Ques. !!!!!!!!

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A AhmedOsamaMoh

    what if i want to copy the function code from the memory to a file?? !! thanks

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #4

    Why would you need/want this? Even if it were possible, the "code" you'd end up with would be machine code.

    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

    1 Reply Last reply
    0
    • A AhmedOsamaMoh

      what if i want to copy the function code from the memory to a file?? !! thanks

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #5

      IMHO (I'm not expert about), you need to hack DLL (or executable) internals, i.e. you need to know PE file format, have a look at http://msdn.microsoft.com/msdnmag/issues/02/02/PE/default.aspx[^] :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      [my articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • A AhmedOsamaMoh

        Dear All how can i get the size of a function (its code size in the memory in bytes) from its pointer (its function pointer) function like double add (double , double) i tried sizeof by many ways and no result:~ any tips:confused::confused: thanks all bye

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #6

        The only way would be a loop that reads through the assembly code looking for the next ret. This would only work with simple functions having only one ret. Once you put the code in a file it would be useless anyway because code with jump instructions in it (most code) is usually address dependent. ie. if you don't load the code back at the same address it won't work anymore. As you can never gaurentee to do this in any future instance of the process it would be of no use to have the unbased machine code in a file, except possibly for comparison to detect a code modifying virus attack or something. There are better ways to do that anyway like security cookie checks which are already built into every function for you by the MS Compiler. See the /GS compiler switch. If you want to know more have a look at some disassemblers which turn compiled code back into something almost readable.

        Nothing is exactly what it seems but everything with seems can be unpicked.

        1 Reply Last reply
        0
        • A AhmedOsamaMoh

          Dear All how can i get the size of a function (its code size in the memory in bytes) from its pointer (its function pointer) function like double add (double , double) i tried sizeof by many ways and no result:~ any tips:confused::confused: thanks all bye

          M Offline
          M Offline
          Member 754960
          wrote on last edited by
          #7

          Caveat: This is dubious practice. The memory layout is not guaranteed and should not be relied upon. Pointer arithmatic is not good. It is mostly just an "irresistible" question. Given the above: an old technique is to use two function pointers

          void functionA()
          {
          // whatever
          }
          void functionB()
          {
          // whatever
          }

          void dontdothis()
          {
          void * p1 = functionA;
          void * p2 = functionB;
          char * p3 = static_cast<char *>(p1);
          char * p4 = static_cast<char *>(p2);
          size_t sizeFn = p4 - p3;
          }

          There is little, if anything, that can be done with this.

          A 1 Reply Last reply
          0
          • M Member 754960

            Caveat: This is dubious practice. The memory layout is not guaranteed and should not be relied upon. Pointer arithmatic is not good. It is mostly just an "irresistible" question. Given the above: an old technique is to use two function pointers

            void functionA()
            {
            // whatever
            }
            void functionB()
            {
            // whatever
            }

            void dontdothis()
            {
            void * p1 = functionA;
            void * p2 = functionB;
            char * p3 = static_cast<char *>(p1);
            char * p4 = static_cast<char *>(p2);
            size_t sizeFn = p4 - p3;
            }

            There is little, if anything, that can be done with this.

            A Offline
            A Offline
            AhmedOsamaMoh
            wrote on last edited by
            #8

            can u kindly explain more what the meaning by ur code ?? you just sub. two addr of two pointer of two function !! how this be the size and size of which one .. i really misunderstand thx

            D 1 Reply Last reply
            0
            • A AhmedOsamaMoh

              can u kindly explain more what the meaning by ur code ?? you just sub. two addr of two pointer of two function !! how this be the size and size of which one .. i really misunderstand thx

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #9

              Assuming the two functions are back-to-back in memory, you can simply take the difference of their addresses to obtain the size. In other words, if functionA() is at 0x1234 and functionB() is at 0x2345, then the size of functionA() would be 4,369 bytes.

              "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              M A 2 Replies Last reply
              0
              • D David Crow

                Assuming the two functions are back-to-back in memory, you can simply take the difference of their addresses to obtain the size. In other words, if functionA() is at 0x1234 and functionB() is at 0x2345, then the size of functionA() would be 4,369 bytes.

                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                M Offline
                M Offline
                Member 754960
                wrote on last edited by
                #10

                Nicely said. :) This is precisely what is happening. Once more, don't do this.

                D 1 Reply Last reply
                0
                • D David Crow

                  Assuming the two functions are back-to-back in memory, you can simply take the difference of their addresses to obtain the size. In other words, if functionA() is at 0x1234 and functionB() is at 0x2345, then the size of functionA() would be 4,369 bytes.

                  "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  A Offline
                  A Offline
                  AhmedOsamaMoh
                  wrote on last edited by
                  #11

                  ooh sure should not be used !! nothing can guaranty that the two functions are above each other in the memory !!! see no way here .. indeed its a client request for what i don't know ! thx all

                  M 1 Reply Last reply
                  0
                  • M Member 754960

                    Nicely said. :) This is precisely what is happening. Once more, don't do this.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #12

                    Member 754960 wrote:

                    Once more, don't do this.

                    I wouldn't. I was just explaining to Adore what was happening.

                    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    1 Reply Last reply
                    0
                    • A AhmedOsamaMoh

                      ooh sure should not be used !! nothing can guaranty that the two functions are above each other in the memory !!! see no way here .. indeed its a client request for what i don't know ! thx all

                      M Offline
                      M Offline
                      Member 754960
                      wrote on last edited by
                      #13

                      Since that won't do, try this: Load the program in a debugger and read the starting and ending address of the function and do the math. Otherwise you need to do a lot more research before you can ask your question.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups