#include <Windows.h>
#include <iostream>
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH: {
HANDLE hProcess = nullptr;
DWORD dwProcessId = 0;
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast<LPCWSTR>(&DllMain), &hProcess)) {
dwProcessId = GetProcessId(hProcess);
if (dwProcessId != 0) {
WCHAR szProcessName[MAX_PATH];
DWORD dwSize = MAX_PATH;
if (QueryFullProcessImageName(hProcess, 0, szProcessName, &dwSize)) {
// Извлекаем имя файла из полного пути
WCHAR *pszFileName = wcsrchr(szProcessName, L'\\');
if (pszFileName != nullptr) {
std::wcout << L"The process ID loading this DLL is: " << dwProcessId << std::endl;
std::wcout << L"The process name is: " << pszFileName + 1 << std::endl;
} else {
std::cerr << "Failed to extract process name." << std::endl;
}
} else {
std::cerr << "Failed to get process image name." << std::endl;
}
} else {
std::cerr << "Failed to get the process ID." << std::endl;
}
CloseHandle(hProcess);
}
break;
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}