Thread: Should I export DllMain() function?

  1. #1
    Registered User
    Join Date
    Apr 2009

    Should I export DllMain() function?

    I created a dll file, and I wanted the resources it uses to be initialized when the dll gets loaded, so I wanted to include DllMain function in the dll file.

    // DllMain.cpp
    #include "windows.h"
    int WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*pReserved*/)
        ...Do Initialization...
    Do I need to compile DllMain.cpp file along with the sources for the dll
    file for the above DllMain() function to be called? or can I 1) compile the dllmain.cpp file into a .lib library file 2) statically link the library to the dll during a link-time?

    Also, do I need to export the DllMain function (using the.def file for the dll file)? Thanks!

  2. #2
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    I believe you ought to have posted here Windows Programming

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Right, and moved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    DllMain needs to be part of your dll source (compiled as part of the dll and part of the dll binary) and no, it does not need to be exported.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered /usr
    Join Date
    Aug 2001
    Newport, South Wales, UK
    DllMain is just like WinMain in the sense that it is required (or optional in the case of DllMain) simply for somewhere for the CRT to pass control to after it is has finished initializing. It is internal to the module.

    The PE format (which all Win32/64? EXEs and DLLs are in) specifies a single entry point address at which execution of the module begins; for C/C++ programs this usually the CRT routine that initializes, calls your WinMain/DllMain, then trashes any resources the CRT used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM