I'm trying to build a DLL which I would later like to inject into some processes using the SetWindowsHookEx() function. Strange thing is that when I try to load the DLL and try to get use GetProcAddress to get the address of the procedure contained within it, it returns NULL if I try to get the address of the CBT message handling procedure, but it works fine for the other functions.
This is the code.
DLL HEADER (.h)
#include <windows.h>
extern "C" {
__declspec(dllexport) LRESULT CALLBACK hookProc(int code, WPARAM wParam, LPARAM lParam);
__declspec(dllexport) int add(int a, int b);
}
DLL FILE (.cpp)
#include "SimpleHook.h"
extern "C" {
__declspec(dllexport) LRESULT CALLBACK hookProc(int code, WPARAM wParam, LPARAM lParam) {
return CallNextHookEx(0, code, wParam, lParam);
}
__declspec(dllexport) int add(int a, int b) {
return a + b;
}
}
MAIN FILE
#include <iostream>
#include <windows.h>
#include <tchar.h>
int main(int argc, char* argv[]) {
HINSTANCE dllHandle = LoadLibrary(_T("SimpleHook.dll"));
if (dllHandle) {
// returns the correct address
cout << "add address: " << GetProcAddress(dllHandle, "add") << endl;
// returns NULL
cout << "hookProc address: " << GetProcAddress(dllHandle, "hookProc") << endl;
}
}
If I use GetLastError() I get the 127 error code with means:
ERROR_PROC_NOT_FOUND: The specified procedure could not be found.
Strange thing is that the other functions from the same file are loaded correctly. Any help is greatly appreciated!