Q:

Can't get by "DllMain already defined" error

I'm trying to write a .dll library for .dll injection purposes.  And because of this fact, it must have a routine called DllMain, since this is what will be used as the entry point.  I think my problem may be stemming from the fact that I'm linking in a static library that I've wrote which utilizes a threads and mutexes from afxmt.h.  Because somewhere down the line, the inclusion of this is causing the linker to link from mfcs100ud.lib which apparently contains its own version of DllMain.
Here is the file that is giving me trouble:
dllmain.cpp
#include "stdafx.h"
#include <stdio.h>
#include "NamedPipeLogger.h"

static CNamedPipeLogger m_PipeLogger("Log.txt");

BOOL APIENTRY DllMain(HANDLE hModule, 
                      DWORD  ul_reason_for_call, 
                      LPVOID lpReserved)
{
}

Here is the stdafx.h file that dllmain.cpp is including.
stdafx.h
#pragma once

#define _AFXDLL
#include <Afx.h>

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers

Here is my Error message:

Error 32  error LNK2005: _DllMain@12 already defined in
  dllmain.obj   D:\xxxxx\xxxxx\xxxxxx\mfcs100ud.lib(dllmodul.obj)

Am I just screwed here because I cannot change the name of my Dll entry point to something other than DllMain?

A:

In many cases this is caused by having _USRDLL in the preprocessor settings, where it should be _LIB. This has to do with 'MFC extension dlls' which I don't think anyone still makes today, yet the VS wizard seems to assume you do want to use this when you check 'Use MFC' in the wizard.

A:

Recently, I experienced the same or a similar issue, and found a solution.
Background
I have an MFC project in Visual Studio 2013 Pro, which generates a DLL. I have several .c modules in the project, which I'm able to do by conditionally specifying the 'extern "C"' construct, disabling precompiled headers for those C files, and - in my case - disabling inherited forced includes, which was pulling-in stdafx.h from the project defaults.
Problem
One day, after having used this method successfully on several C files, when I'd try to add just one more, I'd get the following error.
1>Link:
1>  All outputs are up-to-date.
1>mfcs120d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in MSVCRTD.lib(dllmain.obj)
1>     Creating library C:\path\to\project\build_dir\myproj.lib and object C:\path\to\project\build_dir\myproj.exp
1>C:\path\to\project\build_dir\myproj.dll : fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.

Solution
I resolved this by implementing "Solution One" from Microsoft Knowledge Base article Q148652, "A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++". This forces the linker to link the libraries in the correct order.
Steps:

Right-click the project, choose Properties.
Ensure you're making changes for All Configurations, and All Platforms.
In the left pane, browse to Linker → Input.
In the right pane, pull down Additional Dependencies, choose <Edit...>.
Add mfcs120d.lib.

Based on the Code Project article Solve error LNK2005: _DllMain@12 already defined in msvcrtd.lib(dllmain.obj) in MFC Projects", I figure I might have to add another library to that list someday, but this much works for me for now.

A:

Well, I guess I threw in the towel on this one (sort of).  I was able to at least get by all my problems.  I just had to stop using some of the Microsoft classes.
I touched on this in the problem description, but I recall starting to have difficulty with compiling as soon as I started including:
#include <afxmt.h>
#include <afxwin.h>

So I went through and figured out what exactly I was using that required these includes.  I was using the AfxBeginThread() method, and the classes CMutex and CCriticalSection.  So I figured maybe if I could just get away from any of the proprietary windows stuff that maybe my problems would go away.  That means removing all includes of , , and  and then address the compilation errors with more standard c++ code. Here is what I did:

Instead of using AfxBeginThread() I used CreateThread().
Instead of using CMutex and CCriticalSection I used the CRITICAL_SECTION structure with its accompanying routines.

After this I was able to compile the .dll and it worked fine.

