How to create dynamic Library in C++
C++ in how to create a dynamic library, many novices are not very clear, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
C++ dynamic library
1. We open VS2017, select the Windows desktop in C++, and create a new dynamic link library for Cppdll.
After creation, the solution Explorer on the right will come out.
two。 We select add-New item in the header file, and choose to create a new Cppdll.h header file
3. Declare an external calling method of AddCount in Cppdll.h. Pay special attention to the green font, especially extern "C". If you don't add it, the entry method name will not be found when C # is called.
4. Create the implementation method of AddCount. After we place the mouse over the method of AddCount, press the list at the bottom of ALT+ENTER, and choose to create the definition of "AddCount" in Cppdll.cpp.
When we open the Cppdll.cpp file again, we can see that the implementation method of AddCount has come out here.
5. Write the implementation method of AddCount, the output is actually very simple, we just add the two results of the input together.
Then generate the solution, and our C++ dynamic library is complete.
C # caller
Right-click in the solution to create a new C# forms application named CppImport
two。 Create a TextBox and a button Button in the form
3. Write the method of calling C++ dynamic library in the code
DllImport is the calling method, the first parameter is the name of the dynamic library, EntryPoint is the function name of the entry, which is the method of AddCount written in our C++ dynamic library, and CallingConvention is the calling protocol. Because we have the property description of EntryPoint, we can define the new function name as GetNewInt ourselves, but the parameters need to be consistent.
4. Click the button to generate two random numbers and get the added value.
5. Modify the platform goal (focus) of the C# application, which must be changed to x86, that is, 32-bit flat, or an error message will be called.
6. After we regenerate the solution, copy the DLL generated by C++ to the program directory of CSharp, and then we can run it to see the effect.
Let's take a look at the running results. I'll order a few more times.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.