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:
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:
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;
}