无法通过“DllMain 已定义”获得错误
我正在尝试编写一个用于 .dll 注入目的的 .dll 库。因此,它必须有一个名为 DllMain 的例程,因为它将用作入口点。我认为我的问题可能源于这样一个事实:我正在链接我编写的静态库,该库利用 afxmt.h 中的线程和互斥体。因为在某个地方,包含此内容会导致链接器从 mfcs100ud.lib 进行链接,而 mfcs100ud.lib 显然包含其自己的 DllMain 版本。
这是给我带来麻烦的文件:
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)
{
}
这是 dllmain.cpp 包含的 stdafx.h 文件。
stdafx.h
#pragma once
#define _AFXDLL
#include <Afx.h>
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
这是我的错误消息:
错误 32 错误 LNK2005:_DllMain@12 已在中定义 dllmain.obj D:\xxxxx\xxxxx\xxxxxx\mfcs100ud.lib(dllmodul.obj)
我只是搞砸了,因为我无法将 Dll 入口点的名称更改为 DllMain 以外的名称?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在许多情况下,这是由于预处理器设置中包含 _USRDLL 而导致的,而它应该是 _LIB。这与“MFC 扩展 dll”有关,我认为今天没有人还在制作它,但当您在向导中选中“使用 MFC”时,VS 向导似乎假设您确实想要使用它。
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.
最近,我遇到了相同或类似的问题,并找到了解决方案。
背景
我在 Visual Studio 2013 Pro 中有一个 MFC 项目,它生成一个 DLL。我的项目中有几个 .c 模块,我可以通过 有条件地指定 'extern "C"' 构造, 禁用这些 C 文件的预编译头 ,并且 - 就我而言 - 强制禁用继承的 包含,它从项目默认值中引入 stdafx.h。
问题
有一天,在对多个 C 文件成功使用此方法后,当我尝试再添加一个时,会收到以下错误。
解决方案
我通过实施 Microsoft 知识库文章 Q148652 中的“解决方案一”解决了此问题,“在 Visual C++ 中以错误的顺序链接 CRT 库和 MFC 库时会发生 LNK2005 错误”。这会强制链接器以正确的顺序链接库。
步骤:
基于代码项目文章 解决错误 LNK2005: _DllMain@12 已在 MFC 项目的 msvcrtd.lib(dllmain.obj) 中定义”,我想我可能需要添加另一个库总有一天会出现在这个清单上,但目前这对我来说很有效。
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.
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:
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.
好吧,我想我在这件事上认输了(某种程度上)。我至少能够解决所有问题。我只得停止使用一些微软的课程。
我在问题描述中谈到了这一点,但我记得当我开始包含时就开始编译遇到困难:
所以我仔细检查并弄清楚我到底在使用什么需要这些包含。我使用的是AfxBeginThread()方法,以及类CMutex和CCriticalSection。所以我想如果我能摆脱任何专有的 Windows 东西,也许我的问题就会消失。这意味着删除所有包含 、 、 和 的内容,然后使用更标准的 C++ 代码解决编译错误。这就是我所做的:
之后我就能够编译 .dll 并且工作正常。
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:
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:
After this I was able to compile the .dll and it worked fine.
当我将
#include afxdllx.h
从 dllmain.cpp 移动到 StdAfx.h 时,出现错误。我的项目没有这个也可以工作I got the error when I have moved
#include afxdllx.h
from dllmain.cpp to StdAfx.h. My project works without this include also