Common Bugs and solutions for calling unmanaged dll

Source: Internet
Author: User
Tags vc runtime

C and C ++ have a lot of good class libraries. NET, it is unwise and unrealistic to discard them completely and re-start them. Therefore, we often need to use the previously left-behind Unmanaged dll through Pinvoke. As for the use of unmanaged dll in. NET, there are at least two common problems, which are reflected by throwing exceptions during runtime.

1. Try to load a program with incorrect format

This exception usually occurs when the "Target Platform" of. NET applications is different from that of the hosted dll.

Generally, VS is used for development. NET Applications and class libraries, the default target platform is "Any CPU", that is, X86 or X64 can be automatically selected according to the CPU type at runtime.. NET compiled assembly is based on IL. during runtime, CLR will transmit its JIT to X86 or X64 machine code.

The dll generated by C or C ++ compilation is the machine code. Therefore, the platform decision is determined during compilation. By setting the compilation options, we can compile the C/C ++ project into X86 dll or X64 dll.

Therefore, in the. NET project that calls the unmanaged dll, you also need to set its target platform attribute to the operating platform of the non-hosted dll. Generally, the legacy Unmanaged dll is based on x86. Therefore, in the. NET project that calls such Unmanaged dll, set its target platform attribute to "X86 ".

You can find this setting based on "Project-> properties-> Generate-> Target Platform:

2. The dll cannot be loaded and the specified module cannot be found.

The operation calls an unmanaged. NET Applications sometimes encounter this exception, but it is depressing that this exception does not occur on all computers. In experience, it only appears on a few computers, it works normally on most computers. We encountered this problem when developing the audio and video recording component MFile, which was a headache at that time.

In this case, it is possible that a small number of computers do not have the VC ++ Runtime (CRT) installed, or the CRT installation is incorrect. There are two useful solutions:

(1) The following files are found on disk C: msvcm80d. dll, msvcp80d. dll, msvcr80d. dll, Microsoft. VC80.DebugCRT. manifest. Copy these files to the target machine and put them in the running directory or system32.

Note: Generally, these files have multiple versions and are located in different folders. Observe the folder Name: the options are x86, x64, debug, release, and MFC. These options must be consistent with the information of the non-hosted dll.

(2) If the source code of an unmanaged dll exists, modify the compilation option and recompile it: Change/MD or/MDd to/MT or/MTd, in this way, the static link to the VC Runtime Library is implemented, and the above dll is no longer needed during the runtime.

 

 

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.