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. DllMain safety

DllMain safety

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 Posters 3 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    Is it safe to call GetModuleHandle and GetProcAddress inside DllMain?

    The difficult we do right away... ...the impossible takes slightly longer.

    _ 1 Reply Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      Is it safe to call GetModuleHandle and GetProcAddress inside DllMain?

      The difficult we do right away... ...the impossible takes slightly longer.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      If you're trying to get the address of functions in the same DLL, I don't see why you need to call these APIs. Instead you could directly get the address of the functions. If you're trying to get the address of functions from a different module, then calling GetModuleHandle from DllMain is not safe and can deadlock because of a loader lock being acquired. Here is a best practice guide to DLLs - Dynamic-Link Library Best Practices[^] Here are some interesting reads with regards to DllMain - Does creating a thread from DllMain deadlock or doesn't it?[^] Another reason not to do anything scary in your DllMain: Inadvertent deadlock[^]

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++) (October 2009 - September 2013)

      Polymorphism in C

      Richard Andrew x64R 1 Reply Last reply
      0
      • _ _Superman_

        If you're trying to get the address of functions in the same DLL, I don't see why you need to call these APIs. Instead you could directly get the address of the functions. If you're trying to get the address of functions from a different module, then calling GetModuleHandle from DllMain is not safe and can deadlock because of a loader lock being acquired. Here is a best practice guide to DLLs - Dynamic-Link Library Best Practices[^] Here are some interesting reads with regards to DllMain - Does creating a thread from DllMain deadlock or doesn't it?[^] Another reason not to do anything scary in your DllMain: Inadvertent deadlock[^]

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

        Polymorphism in C

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Thanks Superman!

        The difficult we do right away... ...the impossible takes slightly longer.

        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