Should DllMain be named another name?

Viewed 388

I'm learning about DLLs and i saw the following paragraph in the MSDN:

If you are providing your own entry-point, see the DllMain function. The name DllMain is a placeholder for a user-defined function. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools.

When trying to create a little dll for starter, i noticed that if i call my DllMain a different name,
it's not being called..

What do they mean by this paragraph? should this function really not be called DllMain
but some other name? How do the OS knows what function to call then?

3 Answers

They mean, that a piece of text, namely, DllMain is the default name for the entry point function of the DLL.

The default name means, that unless you configure your compiler otherwise, the compiler will assume that a function called DllMain should be the entry point.

This in turn means that you can configure your compiler to consider any other name to be the entry point. Usually, the compiler takes that as some kind of /commandlineparam or -commandlineparam. See the docs.

EDIT: Oh, I just noticed you're using MSVC.
Here's that part: enter image description here

How do the OS knows what function to call then?

The OS doesn't know the name of the function, it knows the address of the function. That address is stored in the DLL's PE header, and is setup by the compiler/linker. The name of the function is dictated by your compiler's RTL library. Whatever code the compiler uses to setup the DLL's internals at load time, that code dictates what function name and signature you must implement in your own code. It might be Dllmain(), it might be DllEntryPoint, or it might be something else entirely. It depends on tbe compiler and RTL used.

Oh, just because you can name it something else, doesn't mean that the linker will read your mind. You need to tell the linker what the name of the entry point is - otherwise it'll assume it should be DllMain, and if it's absent then the DLL won't have an entry point. The entry point is, after all, optional.

The name of your entry point should be passed to the linker via the /ENTRY command line parameter.

Related