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

CloseHandle

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 3 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.
  • J Offline
    J Offline
    jmkhael
    wrote on last edited by
    #1

    In the following code which is a selfdeleting code on NT i cant seem to understand why the call CloseHandle((HANDLE)4); is there? what is this 4 value that is casted to a HANDLE? #include int main(int argc, char *argv[]) { char buf[MAX_PATH]; HMODULE module; module = GetModuleHandle(0); GetModuleFileName(module, buf, MAX_PATH); CloseHandle((HANDLE)4); __asm { lea eax, buf push 0 push 0 push eax push ExitProcess push module push DeleteFile push UnmapViewOfFile ret } return 0; } Papa while (TRUE) Papa.WillLove ( Bebe ) ;

    P 1 Reply Last reply
    0
    • J jmkhael

      In the following code which is a selfdeleting code on NT i cant seem to understand why the call CloseHandle((HANDLE)4); is there? what is this 4 value that is casted to a HANDLE? #include int main(int argc, char *argv[]) { char buf[MAX_PATH]; HMODULE module; module = GetModuleHandle(0); GetModuleFileName(module, buf, MAX_PATH); CloseHandle((HANDLE)4); __asm { lea eax, buf push 0 push 0 push eax push ExitProcess push module push DeleteFile push UnmapViewOfFile ret } return 0; } Papa while (TRUE) Papa.WillLove ( Bebe ) ;

      P Offline
      P Offline
      Phil Hamer
      wrote on last edited by
      #2

      Look at NickRepin's comment near the bottom of this page: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20170646.html[^] Basically he says CreateFileMapping always returns a handle with a value of 4.

      S 1 Reply Last reply
      0
      • P Phil Hamer

        Look at NickRepin's comment near the bottom of this page: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20170646.html[^] Basically he says CreateFileMapping always returns a handle with a value of 4.

        S Offline
        S Offline
        Steve S
        wrote on last edited by
        #3

        I can't be the only one that thinks this can't be true, or you'd never be able to create more than one active file mapping for a given process? Steve S

        P J 2 Replies Last reply
        0
        • S Steve S

          I can't be the only one that thinks this can't be true, or you'd never be able to create more than one active file mapping for a given process? Steve S

          P Offline
          P Offline
          Phil Hamer
          wrote on last edited by
          #4

          Yeah, I must not have interpreted it correctly. I don't really understand it, so I won't even try to interpret again. Anyone else?

          S 1 Reply Last reply
          0
          • S Steve S

            I can't be the only one that thinks this can't be true, or you'd never be able to create more than one active file mapping for a given process? Steve S

            J Offline
            J Offline
            jmkhael
            wrote on last edited by
            #5

            Or for any other orelse they will overlap, right? Papa while (TRUE) Papa.WillLove ( Bebe ) ;

            1 Reply Last reply
            0
            • P Phil Hamer

              Yeah, I must not have interpreted it correctly. I don't really understand it, so I won't even try to interpret again. Anyone else?

              S Offline
              S Offline
              Steve S
              wrote on last edited by
              #6

              Having read the thread, I think the answer is simpler. There's a qualifying comment that says mapping the file for the current EXE always returns 4. I suspect that this is 'true' because the EXE is already mapped to address 00400000, and it's just something happening to obfuscate the value returned, or there's some shifting/misreading going on. In other words, the code is committing suicide in a fancy way by unmapping itself from memory while it's executing. The CPU pipeline cache may then probably prevent a page fault while it terminates. Steve S

              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