I'm writing code for a DLL file to inject into another system's process. However, I'm getting the following error at compilation
LNK2001, unresolved external symbol _DLL_TEXT
LNK2001, unresolved external symbol _DLL_InitData
Now I know that this error means that DLL_TEXT
and DLL_InitData
are undefined in my code, but this isn't the case. I have them defined in the header file and I'm including it in the file. I've rewritten the problem part of my code and posted it below. I'm not sure what the issue is here...
Header File: file.h
#if !defined(HEADER_FILE)
#define HEADER_FILE
#if _MSC_VER > 1000
#pragma once
#endif
extern "C"
{
#include <windows.h>
__declspec(align(16)) extern UCHAR DLL_TEXT[0x11000];
extern UCHAR DLL_InitData[0x2319];
}
extern HMODULE g_hDLL;
BOOL DLL_Init();
BOOL DLL_LoadLibrary();
BOOL DLL_FreeLibrary();
void* __stdcall DLL_RVA(DWORD rvaAddr);
#endif
Main File: file.cpp
#include "stdafx.h"
#include "file.h"
HMODULE g_hDLL;
BOOL(WINAPI *DLL_DllEntryPoint)(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved);
void* __stdcall DLL_RVA(DWORD rvaAddr)
{
if (rvaAddr == 0)
return g_hDLL;
if (rvaAddr >= 0x1000 && rvaAddr < 0x12000)
return &DLL_TEXT[rvaAddr - 0x1000];
return NULL;
}
BOOL DLL_Init()
{
HMODULE hDll;
DWORD oldProtect;
void(*fInitData)(void*);
g_hDLL = GetModuleHandle(0);
oldProtect = PAGE_EXECUTE_READWRITE;
VirtualProtect(DLL_TEXT, sizeof(DLL_TEXT), PAGE_EXECUTE_READWRITE, &oldProtect);
hDll = ::LoadLibraryA("KERNEL32.dll");
if (!hDll)
return FALSE;
*(FARPROC*)DLL_RVA(0x12000) = ::GetProcAddress(hDll, "FuncName");
hDll = ::LoadLibraryA("USER32.dll");
if (!hDll)
return FALSE;
*(FARPROC*)DLL_RVA(0x12120) = ::GetProcAddress(hDll, "Func2Name");
if (!*(FARPROC*)DLL_RVA(0x12120))
return FALSE;
*(FARPROC*)&fInitData = (FARPROC)&DLL_InitData[0];
fInitData(DLL_RVA);
VirtualProtect(DLL_TEXT, sizeof(DLL_TEXT), oldProtect, NULL);
*(FARPROC*)&DLL_DllEntryPoint = (FARPROC)DLL_RVA(0x327C);
return TRUE;
}
As you can see in the code, the array DLL_TEXT
and DLL_InitData
are defined in the header file, and the header file is properly included. I've even tried the following fix which I found in an old stack overflow question
Project -> Properties -> Configuration Properties -> Linker -> System
and changing Subsystem
to Console
from Windows
as it was said that visual studio sometimes "gets confused" with the linking.
Any input would be greatly appreciated.
Note: The above code is a "working example" (similar logic but doesn't compile -- gives the same errors) to the code I'm working on.
You did not define those symbols which are missing. You only declared them in the header file. You still need to define them without the extern keyword in your .cpp file, like
UCHAR DLL_InitData[0x2319];
(it still needs to be in extern "C")