Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating my own DLL which make use of Detours 2.1 of MS Research.
But I got these errors:
VB
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

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
VB
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;
}
Posted
Comments
this_is_my_alias 15-Sep-10 10:03am    
To clarify, detours has also DllMain, specifically, detoured.cpp. I also got my own DllMain which is in SampleDLL.cpp

1 solution

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
 
Share this answer
 
Comments
this_is_my_alias 15-Sep-10 10:01am    
That's what I did but I got the errors.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900