C # Call the C ++ dynamic library (dll)

Source: Internet
Author: User

In the actual software development process, because the company uses multiple languages for development, a function may need to be implemented in C #, and this function may have been implemented in other languages, can we call modules written in other languages? In addition, because C # developed projects, we can use reflector and other decompilation tools to decompile their source code, so we do not want to be known by others for some core algorithms, therefore, to enhance code security, we need to write some core algorithms in C or C ++, and then use C # to call these interfaces that have been written. What should we do in the face of the above situation? Solution 1: Re-implement. In the first case, we can re-implement the C or C ++ functions with C #. In this case, the code is unified and the maintenance is convenient, however, this increases the cost of software development, and it is complicated to change the C ++ code function to C # Involving pointer and memory operations. Besides, why do some developed modules not reuse them repeatedly? The second scenario cannot be effectively solved. Although obfuscators can be used to confuse the Code, it is not safe. Solution 2: encapsulate COM components. We can encapsulate C or C ++ functions into COM components, which is easier to call in C #, but the COM component needs to be registered, multiple registration may also cause some problems, and it may be troublesome to convert the types of C or C ++ and COM components. Solution 3: using the dynamic link library, we can directly call the dynamic link library that has been written in C or C ++, which is convenient and can solve the above problems. In actual projects, we need to use C # to call some interfaces of C ++. Therefore, I use dynamic libraries in solution 3, next I will explain how to handle it in practice. During the process of calling the dynamic library, I also encountered the following problems: 1. are pointers in C ++, And do pointers need to be used in C? Since the dynamic library in C ++ contains pointer parameters, I also use it. NET Unsafe code, using the C # pointer, but there are still some problems in the end, such as the problem occurs when the input parameter in C # is a two-dimensional array, I found a lot of information on the Internet and did not solve this problem. Finally, I discussed it with the c ++ programmer and changed the parameter type of the input parameter. Pointer is not used at last. 2. How do I convert the types in C # And C ++? Although C # And C ++ are similar, the parameter types provided by C # correspond one to one with C, therefore, I found some materials to map their types one by one. For details, refer to the subsequent instructions. 3. Where should I place the dynamic library written in C ++? The location of the C ++ dynamic library is also a problem. In the application, we use the relative path and absolute path for testing, and some find that it can be called in, however, the dynamic library cannot be called after the release. In the end, you only need to put the dynamic dll under the system directory system32 to solve the problem. No other methods have been found yet, if you have other better methods, please give us some advice. 4. How to decompile the dll name and port of C ++? You can use the Dependency Walker tool to decompile and view information about the dynamic libraries written by others. 5. There are other details, for example, C # calls a dynamic library and needs to specify its encoding and code writing. public class TestDLL {www.2cto.com [DllImport (@ "SomClient. dll ", EntryPoint =" CreateSomClient ", CallingConvention = CallingConvention. cdecl, CharSet = CharSet. unicode)] public static extern UInt32 CreateSomClient (String hostIp, UInt16 port); [DllImport (@ "SomClient. dll ", EntryPoint =" ReleaseSomClient ", CallingConvention = C AllingConvention. Cdecl, CharSet = CharSet. Unicode)] public static extern void ReleaseSomClient (ref UInt32 clientHandle);} parameter description: EntryPoint specifies the DLL entry point to be called. CharSet controls the encoding of the name and string parameters in the function. The default value is CharSet. Ansi. Whether ExactSpelling modifies the entry point to correspond to different character encoding methods. CallingConvention specifies the call Convention for passing method parameters. The default value is WinAPI. This value corresponds to _ stdcall Based on the 32-bit Intel Platform. Whether the best ing function is enabled for BestFitMapping. The default value is true. The optimal ing function provides automatically matched characters when no matching item exists.

Related Article

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.