LoadLibrary the solution to load dynamic library failure

Source: Internet
Author: User

mode one adopts LoadLibraryEx

Reprint: http://blog.sina.com.cn/s/blog_62ad1b8101017qub.html

If the DLL is not in the same directory as the caller, it can be loaded with LoadLibrary (L "DLL absolute path"). However, if another DLL is called inside the calling DLL, the call will still fail. The solution is to use LoadLibraryEx:
LoadLibraryEx ("DLL absolute path", NULL, Load_with_altered_search_path);
By specifying Load_with_altered_search_path, let the system DLL search order begin with the directory where the DLL is located.


mode two adopts Setcurrentdir

Reprint: http://blog.csdn.net/flyayi2006/article/details/5526982

Calling DLLs across directories, you should do this
1 saving the current working directory with Getcurrentdir
2 using Setcurrentdir to set the current working directory to the path where your DLL is located, you need to use the absolute path
3 using LoadLibrary your DLL
4 using Setcurrentdir to revert to the original working path


Example

    TCHAR Chcurdir[max_path] = {0};
    GetCurrentDirectory (MAX_PATH, chcurdir);
    SetCurrentDirectory (_t ("e:\\test\\"));
    M_hdll = LoadLibrary (_t ("MyTest.dll"));
    SetCurrentDirectory (Chcurdir);

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.