Thread: DllMain implementation

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    DllMain implementation

    Hello everyone,


    CRT is implemented a way that if developer provides DllMain, it will use the provided one, or else the default DllMain is used.

    I am wondering how to implement similar selective linking approach? i.e. if there are more than one implementations of a method of the same signature, I can control which one has the higher priority to link, and if the highest priority one does not exist, the second priority one will be used (and compiler will have no duplicated method warning/error).

    I do not think CRT implements the approach by using MACRO (if developer provides DllMain, developer needs to define some MACRO, but actually we do not define any macro when writing DllMain) to control the selective linking to DllMain.

    Any ideas?


    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    CRT is implemented a way that if developer provides DllMain, it will use the provided one, or else the default DllMain is used.
    Actually, I'm pretty sure that the trick is not the CRT, but the linker itself: if it finds a user-provided DllMain, it uses it; otherwise it supplies a built-in no-op version.
    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. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    I think you mean the sequence of object/lib files input to linker matters?

    (user defined DllMain is set to linker input before CRT default DllMain?)

    Quote Originally Posted by CornedBee View Post
    Actually, I'm pretty sure that the trick is not the CRT, but the linker itself: if it finds a user-provided DllMain, it uses it; otherwise it supplies a built-in no-op version.

    regards,
    George

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It doesn't matter. If it finds a user-specific DllMain, regardless of the other the files are passed, it will use it, otherwise the CRT default.
    ...I think is what CornedBee is suggesting.
    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 User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    So you think the sequence of input object/lib files to the linker matters?

    If yes, can you post your linker whole command line option please? :-)

    Quote Originally Posted by Elysia View Post
    It doesn't matter. If it finds a user-specific DllMain, regardless of the other the files are passed, it will use it, otherwise the CRT default.
    ...I think is what CornedBee is suggesting.

    regards,
    George

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ordering of object/lib does normally not matter, but there are some situations where it does. For example, if the linker expects to find DllMain before it encounters the .lib version (which is the case for example if you want to implement your own version of other library functions), then you have to make sure that YOUR object file is BEFORE the .lib - this is the normal ordering for Visual Studio anyways, so it won't make much difference.

    If it works like I described above, that the DllMain() is introduced from a .lib if it's not part of the object files, you'd get a linker error for duplicate symbols if you have the libs first then the main object files - but there's no real reason for that to happen unless you go out of your way to create such a situation. Just like buying a car with the steering wheel on the right-hand side is quite easy here in England, but a car-dealer in the US would not hold many of those in stock, so the fact that it makes it awkward to drive such a car in the US is not really an issue for you and me.

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

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    As CornedBee stated. Either the compiler or the linker handles inserting the default DllMain function. This is not limited to CRT. If you supply a DllMain it uses yours, otherwise it uses a default version that does nothing. Rather like the use of default constructor code for a class.

    Oh, and we do have some right side cars here, mostly mail trucks.
    Last edited by abachler; 02-12-2008 at 09:35 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    So you think the sequence of input object/lib files to the linker matters?
    My point was that it doesn't matter what order.

    If yes, can you post your linker whole command line option please? :-)
    And even if yes, I don't mess around with linker options. That's why we use IDEs!
    Set specific options and don't modify the compiler or linker commands.
    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.

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


    abachler and Elysia, any comments to my below points?

    Your reply covers generally how it works, but it does not cover internally why linker will select user defined DllMain other than default one provided by CRT -- which is the main point of this question. Maybe I can ask this question in another way.

    1. I do not think linker program writes special code for DllMain, and it should have a general rule to deal with how to use user defined one as higher priority if there is also a default one in lib;

    2. The another way to ask this question should be, suppose we want to override a function foo defined in CRT, how to achive this goal (put foo.obj before CRT libs in linker options?)

    Quote Originally Posted by matsp View Post
    Ordering of object/lib does normally not matter, but there are some situations where it does. For example, if the linker expects to find DllMain before it encounters the .lib version (which is the case for example if you want to implement your own version of other library functions), then you have to make sure that YOUR object file is BEFORE the .lib - this is the normal ordering for Visual Studio anyways, so it won't make much difference.

    If it works like I described above, that the DllMain() is introduced from a .lib if it's not part of the object files, you'd get a linker error for duplicate symbols if you have the libs first then the main object files - but there's no real reason for that to happen unless you go out of your way to create such a situation. Just like buying a car with the steering wheel on the right-hand side is quite easy here in England, but a car-dealer in the US would not hold many of those in stock, so the fact that it makes it awkward to drive such a car in the US is not really an issue for you and me.

    --
    Mats

    regards,
    George
    Last edited by George2; 02-13-2008 at 12:09 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    1. I do not think linker program writes special code for DllMain, and it should have a general rule to deal with how to use user defined one as higher priority if there is also a default one in lib;
    We're telling you it's simple. It searches all libraries for DllMain. If not found, it selects the CRT default DllMain (which consequently might not be named DllMain either, because function names are irrelevant to the linker).

    2. The another way to ask this question should be, suppose we want to override a function foo defined in CRT, how to achive this goal (put foo.obj before CRT libs in linker options?)
    You don't. You will most likely get multiple symbols error.
    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.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2
    2. The another way to ask this question should be, suppose we want to override a function foo defined in CRT, how to achive this goal (put foo.obj before CRT libs in linker options?)
    Quote Originally Posted by Elysia View Post
    You don't. You will most likely get multiple symbols error.
    If we put "foo.obj" ahead of any libraries should work just fine, and no you shouldn't get duplicate symbols, unless foo.obj isn't a COMPLETE replacement of the object file that it's attempting to replace [e.g. if you replace printf, but sprintf is also in the same xprintf.obj, then you get the a problem with printf being declared both on foo.obj and xprintf.obj - not that I think that this is the case in this particular case, but some object files do contain more than one function].

    --
    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. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Though I wouldn't venture as far as relying on it since there's no saying the linker has to do it that way. If it searches all libraries, then you might just as well get a big fat problem.
    Such as defining two main will get symbols error. But maybe that's just because it's within the same application.
    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.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Though I wouldn't venture as far as relying on it since there's no saying the linker has to do it that way. If it searches all libraries, then you might just as well get a big fat problem.
    Such as defining two main will get symbols error. But maybe that's just because it's within the same application.
    Let's put it this way: I have so far not found a linker that doesn't take things in the order specified, and only drags out .lib content AS NEEDED. In fact, it's documented that you sometimes have to specify the same lib twice for gcc's ld, because it doesn't rescan a previous lib for stuff needed in the current one, so you need the lib specified again.

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

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats and Elysia,


    I did some self-study work, please comment.

    I have written the following simple code to generate a DLL and name it main.cpp,

    Code:
    #include <iostream>
    #include <objbase.h>
    
    using namespace std;
    
    BOOL APIENTRY DllMain(HANDLE hModule,
                          DWORD dwReason,
                          void* lpReserved)
    {
    	if (dwReason == DLL_PROCESS_ATTACH)
    	{
    		cout << "Hello World" << endl;
    	}
    	return TRUE ;
    }
    Linker command line is (from command line item of Linker item of Visual Studio project properties),

    /OUT:"D:\Visual Studio 2008\Projects\test_link1\Debug\test_link1.exe" /NOLOGO /MANIFEST /MANIFESTFILE:"Debug\test_link1.exe.intermediate.ma nifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"D:\Visual Studio 2008\Projects\test_link1\Debug\test_link1.pdb" /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:PROMPT kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

    My confusion is, why main.obj is not an input file to linker? Main.obj is generated.

    Quote Originally Posted by matsp View Post
    Let's put it this way: I have so far not found a linker that doesn't take things in the order specified, and only drags out .lib content AS NEEDED. In fact, it's documented that you sometimes have to specify the same lib twice for gcc's ld, because it doesn't rescan a previous lib for stuff needed in the current one, so you need the lib specified again.

    --
    Mats

    regards,
    George

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Whatever does it matter? You're using Visual Studio so you shouldn't have to mess around with the linker.
    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.

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