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. HGLOBAL question

HGLOBAL question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++performancelearning
7 Posts 4 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.
  • B Offline
    B Offline
    Beer26
    wrote on last edited by
    #1

    Hey all, I had a post on here about a week ago about executing code from memory instead of a PE file. So far this is what I've done. have this as the test code compiled ############################################ #include int main(int argc, char* argv[]) { printf("Hello World!\n"); getc(stdin); return 0; } compiled that and stored it as RCDATA resource 130 in my new project ############################################ this is the code I'm trying to execute it with, without writing it back to a file in my MFC dialog app HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int (*ptr)(int argc, char *str[]); ptr = (int (__cdecl *)(int, char *[]))hbytes; int j = 1; char *strs; (*ptr)(j, &strs); CODE DONE ###################################### MODIFIED: I also tried reallocating it with a FIXED flag, and it also didn't work ############ TRIED THIS TOO HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int size = (UINT)SizeofResource(AfxGetResourceHandle(), hrInfo); HGLOBAL pexec = GlobalReAlloc(hbytes, size, GMEM_FIXED); int (*ptr)(int argc, char* argv[]); ptr = (int (__cdecl *)(int, char *[]))pexec; int j = 1; char *strs; (*ptr)(j, &strs); ################ OK, the function I cast it to matches that of the main of the test code. BUT, it gives me an access violation. WHY? HGLOBAL DATA is supposed to be executable from memory without any special function right?, Will this work with the HGLOBAL as is, or do I need to explicitly GlobalAlloc it? I realize that this may be an advanced topic, and I urge some people to refrain from replying as that my last post got a couple of very ignorant responses. Thanks for your understanding.

    A P T 3 Replies Last reply
    0
    • B Beer26

      Hey all, I had a post on here about a week ago about executing code from memory instead of a PE file. So far this is what I've done. have this as the test code compiled ############################################ #include int main(int argc, char* argv[]) { printf("Hello World!\n"); getc(stdin); return 0; } compiled that and stored it as RCDATA resource 130 in my new project ############################################ this is the code I'm trying to execute it with, without writing it back to a file in my MFC dialog app HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int (*ptr)(int argc, char *str[]); ptr = (int (__cdecl *)(int, char *[]))hbytes; int j = 1; char *strs; (*ptr)(j, &strs); CODE DONE ###################################### MODIFIED: I also tried reallocating it with a FIXED flag, and it also didn't work ############ TRIED THIS TOO HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int size = (UINT)SizeofResource(AfxGetResourceHandle(), hrInfo); HGLOBAL pexec = GlobalReAlloc(hbytes, size, GMEM_FIXED); int (*ptr)(int argc, char* argv[]); ptr = (int (__cdecl *)(int, char *[]))pexec; int j = 1; char *strs; (*ptr)(j, &strs); ################ OK, the function I cast it to matches that of the main of the test code. BUT, it gives me an access violation. WHY? HGLOBAL DATA is supposed to be executable from memory without any special function right?, Will this work with the HGLOBAL as is, or do I need to explicitly GlobalAlloc it? I realize that this may be an advanced topic, and I urge some people to refrain from replying as that my last post got a couple of very ignorant responses. Thanks for your understanding.

      A Offline
      A Offline
      AlexO
      wrote on last edited by
      #2

      The topic is advanced, and using that kind of trickery requires skills and most importantly reason. I am not even going to discuss what possible reason you have to write code like this. Lets concentrate on what you actually trying to do: Beer26 wrote: HGLOBAL pexec = GlobalReAlloc(hbytes, size, GMEM_FIXED); I am not completely sure what you expect from the statement above. I assume you want to create a copy of the resource in memory (I do not think GMEM_FIXED is going to be honored in Win32) You might be better off allocating new block of memory, insteadof realloc-ing something that you did not alloc-ed. Beer26 wrote: ptr = (int (__cdecl *)(int, char *[]))pexec; I read the line above as - you assume that recourse you loaded is actually executable code of the function with signature "int (*ptr)(int argc, char* argv[]);" (please confirm). Are you sure that it is a valid executable code? Beer26 wrote: int j = 1; char *strs; (*ptr)(j, &strs); the line above actually try to execute the loaded code. (please confirm). Please note also that strs is not initialized. How did you compile it? How did you put into resource? How are calls "getc" or "printf" going to be resolved ?

      1 Reply Last reply
      0
      • B Beer26

        Hey all, I had a post on here about a week ago about executing code from memory instead of a PE file. So far this is what I've done. have this as the test code compiled ############################################ #include int main(int argc, char* argv[]) { printf("Hello World!\n"); getc(stdin); return 0; } compiled that and stored it as RCDATA resource 130 in my new project ############################################ this is the code I'm trying to execute it with, without writing it back to a file in my MFC dialog app HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int (*ptr)(int argc, char *str[]); ptr = (int (__cdecl *)(int, char *[]))hbytes; int j = 1; char *strs; (*ptr)(j, &strs); CODE DONE ###################################### MODIFIED: I also tried reallocating it with a FIXED flag, and it also didn't work ############ TRIED THIS TOO HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int size = (UINT)SizeofResource(AfxGetResourceHandle(), hrInfo); HGLOBAL pexec = GlobalReAlloc(hbytes, size, GMEM_FIXED); int (*ptr)(int argc, char* argv[]); ptr = (int (__cdecl *)(int, char *[]))pexec; int j = 1; char *strs; (*ptr)(j, &strs); ################ OK, the function I cast it to matches that of the main of the test code. BUT, it gives me an access violation. WHY? HGLOBAL DATA is supposed to be executable from memory without any special function right?, Will this work with the HGLOBAL as is, or do I need to explicitly GlobalAlloc it? I realize that this may be an advanced topic, and I urge some people to refrain from replying as that my last post got a couple of very ignorant responses. Thanks for your understanding.

        P Offline
        P Offline
        peterchen
        wrote on last edited by
        #3

        a) there is no guarantee that you have EXECUTE right on a HGLOBAL, but this doesn#t matter on Wintel Boxes b) You *are+ aware that EXE files come a) with a PE header, and b) with a relocation table? They are not just "sequences of code bytes"...


        "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
        sighist | Agile Programming | doxygen

        B 1 Reply Last reply
        0
        • P peterchen

          a) there is no guarantee that you have EXECUTE right on a HGLOBAL, but this doesn#t matter on Wintel Boxes b) You *are+ aware that EXE files come a) with a PE header, and b) with a relocation table? They are not just "sequences of code bytes"...


          "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
          sighist | Agile Programming | doxygen

          B Offline
          B Offline
          Beer26
          wrote on last edited by
          #4

          "a) there is no guarantee that you have EXECUTE right on a HGLOBAL, but this doesn#t matter on Wintel Boxes" From MSDN "Remarks If the heap does not contain sufficient free space to satisfy the request, GlobalAlloc returns NULL. Because NULL is used to indicate an error, virtual address zero is never allocated. It is, therefore, easy to detect the use of a NULL pointer. Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary. All memory is created with execute access; no special function is required to execute dynamically generated code. " "b) You *are+ aware that EXE files come a) with a PE header, and b) with a relocation table? They are not just "sequences of code bytes"..." Yes, I am. What if i write a small bit of code in ASM and only generate the OPCODE? Then try it with that? Would that work? I'm assuming it's not going to work with all the PE junk linked and compiled with it?

          P 1 Reply Last reply
          0
          • B Beer26

            Hey all, I had a post on here about a week ago about executing code from memory instead of a PE file. So far this is what I've done. have this as the test code compiled ############################################ #include int main(int argc, char* argv[]) { printf("Hello World!\n"); getc(stdin); return 0; } compiled that and stored it as RCDATA resource 130 in my new project ############################################ this is the code I'm trying to execute it with, without writing it back to a file in my MFC dialog app HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int (*ptr)(int argc, char *str[]); ptr = (int (__cdecl *)(int, char *[]))hbytes; int j = 1; char *strs; (*ptr)(j, &strs); CODE DONE ###################################### MODIFIED: I also tried reallocating it with a FIXED flag, and it also didn't work ############ TRIED THIS TOO HRSRC hrInfo = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(130), RT_RCDATA); HGLOBAL hbytes = LoadResource(AfxGetResourceHandle(), hrInfo); int size = (UINT)SizeofResource(AfxGetResourceHandle(), hrInfo); HGLOBAL pexec = GlobalReAlloc(hbytes, size, GMEM_FIXED); int (*ptr)(int argc, char* argv[]); ptr = (int (__cdecl *)(int, char *[]))pexec; int j = 1; char *strs; (*ptr)(j, &strs); ################ OK, the function I cast it to matches that of the main of the test code. BUT, it gives me an access violation. WHY? HGLOBAL DATA is supposed to be executable from memory without any special function right?, Will this work with the HGLOBAL as is, or do I need to explicitly GlobalAlloc it? I realize that this may be an advanced topic, and I urge some people to refrain from replying as that my last post got a couple of very ignorant responses. Thanks for your understanding.

            T Offline
            T Offline
            Terry ONolley
            wrote on last edited by
            #5

            __asm { jmp (start of your code); return; }


            B 1 Reply Last reply
            0
            • T Terry ONolley

              __asm { jmp (start of your code); return; }


              B Offline
              B Offline
              Beer26
              wrote on last edited by
              #6

              woah, you gotta be kidding me, I'm going to try this now. It kind of makes sense too!

              1 Reply Last reply
              0
              • B Beer26

                "a) there is no guarantee that you have EXECUTE right on a HGLOBAL, but this doesn#t matter on Wintel Boxes" From MSDN "Remarks If the heap does not contain sufficient free space to satisfy the request, GlobalAlloc returns NULL. Because NULL is used to indicate an error, virtual address zero is never allocated. It is, therefore, easy to detect the use of a NULL pointer. Memory allocated with this function is guaranteed to be aligned on an 8-byte boundary. All memory is created with execute access; no special function is required to execute dynamically generated code. " "b) You *are+ aware that EXE files come a) with a PE header, and b) with a relocation table? They are not just "sequences of code bytes"..." Yes, I am. What if i write a small bit of code in ASM and only generate the OPCODE? Then try it with that? Would that work? I'm assuming it's not going to work with all the PE junk linked and compiled with it?

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                Beer26 wrote: All memory is created with execute access; Oops, sorry, I missed that (it's late over here ;-O ) b) yes that should work (as long as you refrain from absolute jumps/calls (or adress them correctly, with regards to the allocated base address) If it doesn't, try to follow the thing in the debugger (disassembly window)


                "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                sighist | Agile Programming | doxygen

                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