Loadlibrary call failure cause

Source: Internet
Author: User

Use loadlibrary to display and load dynamic libraries:


Hinstance hdll; // DLL handle

Hdll = loadlibrary ("user32.dll ");

If (hdll! = NULL)




The result always jumps out of if directly, and DLL loading fails.


After other problems are ruled out, the following statement is successfully called:

Hdll = loadlibrary (text ("user32.dll "));


Baidu and Google discovered that it was originally caused by character formats.


Loadlibrary actually uses loadlibraryw instead of loadlibrarya. Therefore, a unicode string (wide string) instead of a narrow string is required, as shown below:


# Ifdef Unicode

# Define loadlibrary loadlibraryw

# Else

# Define loadlibrary loadlibrarya

# Endif //! Unicode



In C/C ++ code, the string defined by "" is a narrow byte string, while the text macro provided in the Windows header file can be defined based on whetherUnicodeMacro to automatically select the string type.


Therefore, the text macro is used to automatically select the correct character set. The DLL is successfully called.


PS: The loadlibrary function and LoadLibraryEx function have different mechanisms for loading the DLL. The former will automatically load the DLL when loading other DLL dependent on the DLL, while the latter will not, there are examples on the Internet that failed to load their own DLL. If you want to solve the Path Problem (it is best to use the full path), you should consider whether the DLL depends on other DLL.

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.