如何修复_DllMain @ 12已经定义的错误? [英] How to fix _DllMain@12 already defined error?

查看:244
本文介绍了如何修复_DllMain @ 12已经定义的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自己的DLL,它使用MS Research的Detours 2.1.
但是我得到了这些错误:

I am creating my own DLL which make use of Detours 2.1 of MS Research.
But I got these errors:

detours.lib(detoured.obj) : error LNK2005: _DllMain@12 already defined in SampleDLL.obj
Debug/SampleDLL.dll : fatal error LNK1169: one or more multiply defined symbols found


我认为是因为,当我使用nmake
编译时,detoured.cpp包含BOOL WINAPI DllMain(...)
我应该如何解决这个问题.我在其他一些论坛上看到应该删除它.但是没有它的DLL也可以吗?

这是有关我要制作的DLL的代码(该代码不是我的,只是将其复制到论坛上.它实际上是尝试与DrawTextW挂钩):

//SampleDLL.cpp代码


I think its because, detoured.cpp contains BOOL WINAPI DllMain(...) when I compiled it using nmake

How should I fix this problem. I saw in some other forums, that it should be removed. But is it okay have a DLL without it?

Here''s the code regarding the DLL I want to make (The code isn''t mine, just copied it on a forum. It actually tries to hook to DrawTextW):

// SampleDLL.cpp code

int (WINAPI * Real_DrawText)(HDC a0, LPCWSTR a1, int a2, LPRECT a3, UINT a4) = DrawTextW;

// Our custom version of DrawText
int WINAPI Mine_DrawText(HDC hdc, LPCWSTR text,  int nCount, LPRECT lpRect, UINT uOptions)
{
    TRACE("=== %s", text, " ===");
    int rv = Real_DrawText(hdc, text, nCount, lpRect, uOptions);
    return rv;
}

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
            
            DetourTransactionBegin(); 
            DetourUpdateThread(GetCurrentThread());
            DetourAttach(&(PVOID&)Real_DrawText, Mine_DrawText); // <- magic
            DetourTransactionCommit();
            break;

        case DLL_PROCESS_DETACH:
            DetourTransactionBegin(); 
            DetourUpdateThread(GetCurrentThread());
            DetourDetach(&(PVOID&)Real_DrawText, Mine_DrawText);
            DetourTransactionCommit();
            break;
    }
    return TRUE;
}

推荐答案

您实际上不需要将DLLMain用于DLL-如今,大多数DLL都不需要.当DLL加载时,当您拥有需要某种虚拟化的资源时,它确实存在.

无论如何,听起来Detours是作为DLL实现的.与其修改或添加代码,不如创建您想要做的另一个DLL,然后将其与Detours链接.

干杯,

You don''t actually need to use DLLMain for a DLL - most DLLs don''t these days. It''s really there for those times when you have resources that need some sort of virtualisation when the DLL loads.

Anyway, it sounds like Detours is implemented as a DLL. Instead of modifying or adding code to it create another DLL that does what you want to do and then link it with Detours.

Cheers,

Ash


这篇关于如何修复_DllMain @ 12已经定义的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆