In some cases, you need to obtain the module name of the function caller. For example, you want to limit that a function can only be called by a specific DLL. For example, in exception handling, you want to know that the DLL/EXE throws an exception.
API functions_ ReturnaddressAndGetmodulehandleexFunctions can help us achieve this purpose. The following code demonstrates their usage:
Void showcallermodulename () <br/>{< br/> hmodule hcallermodule = NULL; <br/> tchar szmodulename [max_path] = _ T (""); <br/> void * calleraddress = _ returnaddress (); <br/> If (getmodulehandleex (get_module_handle_ex_flag_from_address, (lpctstr) calleraddress, & hcallermodule )) <br/>{< br/> getmodulefilename (hcallermodule, szmodulename, arraysize (szmodulename); <br/> MessageBox (null, szmodulename, _ T ("who is Calling me? "), Mb_ OK); <br/>}< br/>}
Explanation:
_ ReturnaddressThe return address of your function, that is, the caller's address.
GetmodulehandleexYou can give handle to the module where an address is located. After obtaining the handle, you can use getmodulefilename to get the module file name.
As mentioned above, we can use this method to capture which dll/EXE throws an exception. Add at program entry
Setunhandledexceptionfilter (showcallermodulename); </P> <p>