E235
E235

Reputation: 13500

How to debug DLLMain function

I want to write DLL but I want to test what I am writing.
I tried to debug it with F5 but I receive an error:
enter image description here
I read the article Walkthrough: Creating and Using a Dynamic Link Library (C++) how to do it and its latest version.
But they suggest to create header file that contains the functions.

In my case, I created DLL project (Loader) with dllmain.cpp.

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    HANDLE hd;
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        hd = CreateFileA("C:\\Users\\myuser\\Desktop\\test.dll", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        OutputDebugString(L"HELLO");
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

I created a new C++ console project in the same solution but I don't know how to call the function DllMain and debug it.

Just to make it organized, I have now two projects:
1. Loader - this is the DLL project
2. DLLTester - this is a console application that will run the DLL from the Loader project.

I added the folder of the DLL project (Loader) to the Additional Include Directories of the DLLTester project.
I created a header (Loader.h) to the DLL project and added the function signature DLLMain.

Now I can see it in the DLLMain function.
But I don't have idea what arguments I need to pass to this function:
enter image description here

I understand that I need to pass 3 arguments:
1. HMODULE hModule
2. DWORD ul_reason_for_call
3. LPVOID lpReserverd

But I don't what I need to enter there.

Code:
Loader project:
Loader.h:

#pragma once
#include "stdafx.h"

BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
);

dllmain.cpp:

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    HANDLE hd;
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        hd = CreateFileA("C:\\Users\\myuser\\Desktop\\test.dll", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        OutputDebugString(L"HELLO");
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

DLLTester project:
DLLTester.cpp:

// DLLTester.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Loader.h"

int main()
{
    DllMain(?,?,?) -> not sure what to enter here
    return 0;
}

How it looks:
enter image description here

Upvotes: 2

Views: 4314

Answers (2)

SoronelHaetir
SoronelHaetir

Reputation: 15172

The easy way to debug DllMain is to use static linking (that is with __declspec(dllexport)/__declspec(dllimport) syntax), and to then set your DLL project as the startup project but have the debugger start the .exe that links to the library. You set the program to run in the project settings (Project->Properties then Debugging -> Command.

You can then set breakpoints and do traces as normal from your DLL.

I know you can do breakpoints and whatnot for normal functions in a dynamic-loaded DLL I'm just not sure about DllMain.

Upvotes: 0

David A
David A

Reputation: 394

DllMain is actually a fairly special function that is automatically called when a DLL is loaded using LoadLibrary or LoadLibraryEx (unless you do some evil stuff to prevent it).

If you really want to call it, you can use GetProcAddress, but believe it or not, a DLL's actual entry point will normally be _DllMainCRTStartup (which actually calls DllMain in addition to doing things like calling constructors and doing other initialization). It can even be overridden by the developer to be something completely different.

So I don't really see how calling DllMain can do anything useful for any normal DLL. It doesn't perform all of the initialization/cleanup (notably the C++ constructor calls at startup), and it's already being called (unless you suppress it with DONT_RESOLVE_DLL_REFERENCES).

Upvotes: 3

Related Questions