I wrote a program that should perform the function of an anticlooger (to catch hooks in the system through a function in a DLL). But for some reason, when the DLL is connected to the project, the program does not see the function in the DLL itself. How and what to change to see everything and work? VS15 environment. I attach the codes, maybe I'm where I made a mistake.

Code in DLL:

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include "stdafx.h" #pragma data_seg(".hdata") // секция в памяти общая для всех процессов HINSTANCE hi = NULL; // эта переменная может изменяться любым поэтому здесь не должно быть ничего лишнего #pragma data_seg() // конец секции #pragma comment(linker, "/section:.hdata,rws") // даем права этой секции #ifdef __cplusplus #define EXPORT extern "C" __declspec (dllexport) #else #define EXPORT __declspec (dllexport) #endif EXPORT HHOOK SetKeyboardHook(void); // объявляем экспортируемую функцию // а эти будут у всех свои #include "Header.h" HANDLE hFile = NULL; // !!!!! хэндл файла для каждого процесса должен быть свой !!!!! HHOOK hKeyHook = NULL; HHOOK hCBTHook = NULL; BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: // вызывается каждый раз когда новый процесс грузит DLL в свою память { if (!hi) // запоминаем запустивший хэндл(наша DLL) толко первый раз, чтобы он небыл переписан на новый hi = hModule; break; } case DLL_PROCESS_DETACH: { break; } default: break; } return TRUE; } LRESULT CALLBACK DebugProc(int nCode, WPARAM wParam, LPARAM lParam) // вызывается при нажатии или отпускании клавиши { if (nCode) return CallNextHookEx(hKeyHook, nCode, wParam, lParam); if (lParam >= 0) // нам нужно только нажатие printf("Замечен кейлогер"); return CallNextHookEx(hKeyHook, nCode, wParam, lParam); // обработали передаем дальше } HHOOK SetKeyboardHook(void) // эту функцию надо вызывать из приложения для создания ловушек { hKeyHook = SetWindowsHookEx(WH_DEBUG, &DebugProc, hi, 0); return hKeyHook; } // конец DLL 

The code of the main program in which the DLL is called:

 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <locale> #include <windows.h> INT main() { setlocale(LC_ALL, "russian"); HHOOK hook = SetKeyboardHook(); if (hook) { while (WaitMessage()) { MSG msg = { 0 }; while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } } system("pause"); ExitProcess(0); } 

DLL I connect through the project-properties -...

  • one
    Open any C ++ tutorial and read what a "function declaration" is. - Pavel Mayorov

1 answer 1

Probably this code:

 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include "stdafx.h" #ifdef __cplusplus #define EXPORT extern "C" __declspec (dllexport) #else #define EXPORT __declspec (dllexport) #endif EXPORT HHOOK SetKeyboardHook(void); // объявляем экспортируемую функцию // а эти будут у всех свои 

must be in the Header.h file. The main.c file should include Header.h with #include . In addition, most likely the following was meant:

 #ifdef __cplusplus #define EXPORT extern "C" __declspec (dllexport) #else #define EXPORT __declspec (dllimport) #endif 

That is, the dllexport attribute is used in the dll module, which is written in C ++, and the dllimport attribute is used in the main module, which is written in C (if everything is vice versa, you should write #ifndef __cplusplus ). Check all this, and the compiler should see your function. But this is not enough - the linker must still see it, and this is achieved by the layout keys. Most likely, in Visual Studio you just need to add in dependencies a *.lib file that was created with the dll .