Thread: DllMain implementation

  1. #31
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    DllMain exists in (my environment with Visual Studio 2008)

    D:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src\dllmain.c

    Code:
    BOOL WINAPI DllMain(
            HANDLE  hDllHandle,
            DWORD   dwReason,
            LPVOID  lpreserved
            )
    {
    #if defined (CRTDLL)
            if ( dwReason == DLL_PROCESS_ATTACH && ! _pRawDllMain )
                    DisableThreadLibraryCalls(hDllHandle);
    #endif  /* defined (CRTDLL) */
            return TRUE ;
    }
    The pending issues is,

    We need to prove in which lib file DllMain (default) exists;

    Quote Originally Posted by CornedBee View Post
    Why would the generic DllMain have to be in some library? The linker might just have it hardcoded. We don't know, because MS doesn't document this stuff. But it also shouldn't matter.

    regards,
    George

  2. #32
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    We need to prove in which lib file DllMain (default) exists;
    Why?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #33
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Maybe I can ask the question in another way, I have shown the source code of CRT default DllMain. But where is the binary format one?

    Quote Originally Posted by CornedBee View Post
    Why?

    regards,
    George

  4. #34
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Again, why?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suppose if you look in the crt\src directory, you can find where a makefile or some such, which uses this file for input and produces an output file - such as libc.lib, perhaps.

    I agree with CornedBee - why does it matter?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #36
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee and Mats,


    My point is, we need to prove obj has higher priority than lib when linking met with the same symbol, like a function, right?

    So, we need to make sure the default CRT provided DllMain is in lib, and this is my logics.

    Any comments?

    Quote Originally Posted by matsp View Post
    I suppose if you look in the crt\src directory, you can find where a makefile or some such, which uses this file for input and produces an output file - such as libc.lib, perhaps.

    I agree with CornedBee - why does it matter?

    --
    Mats

    regards,
    George

  7. #37
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you can try that theory by implementing a replacement function for anything else provided in a library, such as printf, malloc, etc, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #38
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    I have tried,

    1. Implement a function in a cpp file and comple it to obj file;
    2. Implement another function with the same name in a cpp file, and compile/build into static lib file;
    3. Implement a client cpp file to invoke the function;
    4. No matter we provide the obj file before or after the lib file to build the application, the function in obj file will be invoked.

    I am just interested in where the default DllMain is and if it is in a lib, our theory is good, if not (for example, in another obj file, then our theory will have trouble) :-)

    Quote Originally Posted by matsp View Post
    Well, you can try that theory by implementing a replacement function for anything else provided in a library, such as printf, malloc, etc, etc.

    --
    Mats

    regards,
    George

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What obj file would it be in? It's in a library, or I'll eat the hat that is on top of my cupboard at home [it's a straw one, so I'm not exactly feeling that it's good eating material].

    I was trying to find it yesterday, but I didn't have the right tools to do it on my home machine. Here at work I don't think I have the libraries [or the time].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #40
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    What tools could be used to find the contained exported functions of a static library? Dumpbin? I have the tools at hand and if you can direct me How-to, I can do the practices and post the results here. :-)

    Quote Originally Posted by matsp View Post
    What obj file would it be in? It's in a library, or I'll eat the hat that is on top of my cupboard at home [it's a straw one, so I'm not exactly feeling that it's good eating material].

    I was trying to find it yesterday, but I didn't have the right tools to do it on my home machine. Here at work I don't think I have the libraries [or the time].

    --
    Mats

    regards,
    George

  11. #41
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, "lib /list somelibrary" will show which object files there are. Then use "lib /extract somelibrary someobj" to extract a suspect library, and dumpobj to actually see the contents [assuming it's not obvious what it is and does already, e.g called DllMain.obj - but you probably want to confirm that it REALLY is what you say].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #42
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Im going to assume that the default DllMain just clears the stack and returns, so it probably isnt part of an external library.

  13. #43
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Do you think the locations to find the libs to be scanned is? Any other locations do you think needed?

    cd D:\Program Files\Microsoft Visual Studio 9.0\VC\lib

    If you agree this is the only location to be scanned, there are many libs, which one(s) do you think we need to scan?

    2007/11/06 21:06 419,840 comsupp.lib
    2007/11/06 21:06 435,758 comsuppd.lib
    2007/11/06 21:06 419,624 comsuppw.lib
    2007/11/06 21:06 435,562 comsuppwd.lib
    2007/11/06 20:19 63,258 delayimp.lib
    2007/11/06 20:19 9,218,246 libcmt.lib
    2007/11/06 20:19 11,364,288 libcmtd.lib
    2007/11/06 20:19 5,019,506 libcpmt.lib
    2007/11/06 20:19 6,409,034 libcpmtd.lib
    2007/11/06 20:19 1,524,102 msvcmrt.lib
    2007/11/06 20:19 1,620,522 msvcmrtd.lib
    2007/11/06 20:19 1,650,568 msvcprt.lib
    2007/11/06 20:19 1,823,396 msvcprtd.lib
    2007/11/06 20:19 1,353,800 msvcrt.lib
    2007/11/06 20:19 1,386,760 msvcrtd.lib
    2007/11/06 20:19 12,819,158 msvcurt.lib
    2007/11/06 20:19 14,193,848 msvcurtd.lib
    2007/11/06 20:19 101,414 oldnames.lib
    2004/03/07 13:58 17,576 opends60.lib
    2007/11/06 20:20 4,904 pgobootrun.lib
    2007/11/06 20:20 27,710 pgort.lib
    2007/11/06 20:19 427,220 ptrustm.lib
    2007/11/06 20:19 434,432 ptrustmd.lib
    2007/11/06 20:19 427,220 ptrustu.lib
    2007/11/06 20:19 434,432 ptrustud.lib
    2007/11/06 20:19 485,218 RunTmChk.lib
    2007/11/06 21:22 28,446 vcomp.lib
    2007/11/06 21:22 28,574 vcompd.lib


    Quote Originally Posted by matsp View Post
    Well, "lib /list somelibrary" will show which object files there are. Then use "lib /extract somelibrary someobj" to extract a suspect library, and dumpobj to actually see the contents [assuming it's not obvious what it is and does already, e.g called DllMain.obj - but you probably want to confirm that it REALLY is what you say].

    --
    Mats
    Hi abachler,


    I have posted default implementation of DllMain in post #31, I am not sure whether you description below conforms to the default implementation of DllMain. Any comments?

    Quote Originally Posted by abachler View Post
    Im going to assume that the default DllMain just clears the stack and returns, so it probably isnt part of an external library.

    regards,
    George

  14. #44
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would start with the libc*.lib - then the msvc*.lib - note that there are several libs that have the same content, e.g. libcmt.lib, libcmtd.lib, libcpmtd.lib and libcpmt.lib all have the same functions [nearly all the same at least - the debug version (name ending with "d") may have some debug code in it that isn't in the other libs, but that's not what you are looking for].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #45
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    I did two search, one hit. Any comments?

    1.

    D:\Program Files\Microsoft Visual Studio 9.0\VC\lib>lib /list libc*.lib | "d:\Pr
    ogram Files\GnuWin32\bin\grep.exe" "dllmain"
    f:\dd\vctools\crt_bld\SELF_X86\crt\src\build\INTEL \mt_obj\dllmain.obj

    2.

    D:\Program Files\Microsoft Visual Studio 9.0\VC\lib>lib /list msvc*.lib | "d:\Pr
    ogram Files\GnuWin32\bin\grep.exe" "dllmain"

    (nothing found)

    Quote Originally Posted by matsp View Post
    I would start with the libc*.lib - then the msvc*.lib - note that there are several libs that have the same content, e.g. libcmt.lib, libcmtd.lib, libcpmtd.lib and libcpmt.lib all have the same functions [nearly all the same at least - the debug version (name ending with "d") may have some debug code in it that isn't in the other libs, but that's not what you are looking for].

    --
    Mats

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should I export DllMain() function?
    By chiefmonkey in forum Windows Programming
    Replies: 4
    Last Post: 07-12-2009, 05:32 AM
  2. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  3. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 01:59 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Pure virtual implementation, or not.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 08:05 PM