c++ - Determine whether caller is called from EXE or DLL -
i need determine caller code whether coming exe or dll.
dll
#ifdef dll_exports __declspec(dllexport) void say_hello(); __declspec(dllexport) void getcurrentmodulename(); #else __declspec(dllimport) void say_hello(); __declspec(dllexport) void getcurrentmodulename(); #endif #include <cstdio> #include <windows.h> #include <dbghelp.h> #include <iostream> #include <tchar.h> #include "dll.h" #include "psapi.h" __declspec(naked) void *getstackpointer() { __asm { mov eax, esp ret } } void getcurrentmodulename() { bool result = syminitialize(getcurrentprocess(), null , true); dword64 dwbaseaddress = symgetmodulebase64(getcurrentprocess(), (dword64)getstackpointer()); tchar szbuffer[50]; getmodulebasename(getcurrentprocess(), (hmodule) dwbaseaddress, szbuffer, sizeof(szbuffer)); std::wcout << _t("--->") << szbuffer << std::endl; } void say_hello() { getcurrentmodulename(); } exe
#include <windows.h> #include <cstdio> #include "dll.h" int main() { printf ("entering exe code...\n"); getcurrentmodulename(); printf ("entering dll code...\n"); say_hello(); getchar(); } here output.
entering exe code... --->exe.exe entering dll code... --->exe.exe i wish can get
entering exe code... --->exe.exe entering dll code... --->dll.dll as last caller code dll (say_hello in dll)
is there way can achieve this?
getstackaddress returning value of esp, reference stack. stack allocated per thread, independently of modules loaded in process. need extract stack, value of return address - address in calling module.
given usual prefix code in function is:
push ebp mov ebp,esp sub esp, bytes_of_local_variables esp going random, [ebp] should pointing @ previous ebp, , [ebp+4] should pointing @ current frames return address.
so, try this:
__declspec(naked) void *getreturnaddressassumingstandardframepointers() { __asm { mov eax, [ebp+4] ret } } just make sure functions call arn't compiled /oy
Comments
Post a Comment