[Windows programming] getmodulehandleex is used to obtain the DLL/EXE of the function caller.

Source: Internet
Author: User

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>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.