In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Visual Studio 2010 how to call non-C # written DLL files". In daily operation, I believe many people have doubts about how to call non-C # written DLL files in Visual Studio 2010. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer "Visual Studio 2010 how to call non-C # written DLL files". Next, please follow the editor to study!
Background
In the course of the project, sometimes you need to call DLL files written by non-C #, especially when using some third-party communication components, when developing application software through C #, you need to use the DllImport feature to make method calls.
Steps
1. Create a solution for CSharpInvokeCPP:
two。 Create a dynamic library project for C++:
3. In the application settings, select "DLL" and follow the default options:
* Click finish to get the project shown in the figure:
We can see that there are some files here, in which dllmain.cpp is used as the entry point for defining DLL applications, and its function is the same as that of exe files with a main or WinMain entry function, which is actually an optional file as an entry function for DLL. It is called when both LoadLibrary and FreeLibrary are called when static or dynamic links are called. For more information, please see (http://blog.csdn.net/benkaoya/archive/2008/06/02/2504781.aspx).
4. Now let's open the CSharpInvokeCPP.CPPDemo.cpp file:
Now let's add the following:
/ / CSharpInvokeCPP.CPPDemo.cpp: defines the export function of the DLL application. / / # include "stdafx.h" extern "C" _ declspec (dllexport) int Add (int x, int y) {return x + y;} extern "C" _ declspec (dllexport) int Sub (int x, int y) {return x-y;} extern "C" _ declspec (dllexport) int Multiply (int x, int y) {return x * y } extern "C" _ declspec (dllexport) int Divide (int x, int y) {return x / y;}
Extern "C" has a dual meaning, which can be obtained literally: first, the target modified by it is "extern"; second, the target modified by it is "C". Variables and functions modified by extern "C" are compiled and concatenated in the C language.
The purpose of _ _ declspec (dllexport) is to put the corresponding function into the DLL dynamic library.
The purpose of extern "C" _ declspec (dllexport) is to use DllImport to invoke the DLL file of unmanaged C++. Because using DllImport can only call DLL made by C language functions.
5. Compile the project program and generate CSharpInvokeCPP.CPPDemo.dll and CSharpInvokeCPP.CPPDemo.lib in the Debug directory
Let's use the decompiler PE Explorer to look at the methods in the DLL:
It can be found that the external public functions contain these four "addition, subtraction, multiplication and division" methods.
6. Now let's demonstrate how to use a C# project to call the DLL of unmanaged C++. First, create a C# console application:
7. Create a new CPPDLL class on the CSharpInvokeCSharp.CSharpDemo project and write the following code:
Public class CPPDLL {[DllImport ("CSharpInvokeCPP.CPPDemo.dll")] public static extern int Add (int x, int y); [DllImport ("CSharpInvokeCPP.CPPDemo.dll")] public static extern int Sub (int x, int y); [DllImport ("CSharpInvokeCPP.CPPDemo.dll")] public static extern int Multiply (int x, int y); [DllImport ("CSharpInvokeCPP.CPPDemo.dll")] public static extern int Divide (int x, int y);}
DllImport acts as the import entry feature of C++ 's DLL class in C#, and corresponds to extern "C" through static extern.
8. In addition, remember to copy the DLL file generated in CPPDemo to the bin directory of CSharpDemo. You can also set the output directory in "Project Properties"-> "configuration Properties"-> "General":
After compiling the project, the generated files are automatically output to CSharpDemo.
9. Then write the test code in the Main entry:
Static void Main (string [] args) {int result = CPPDLL.Add (10,20); Console.WriteLine ("10 + 20 = {0}", result); result = CPPDLL.Sub (30,12); Console.WriteLine ("30-12 = {0}", result); result = CPPDLL.Multiply (5,4); Console.WriteLine ("5 * 4 = {0}", result); result = CPPDLL.Divide (30,5) Console.WriteLine (30 / 5 = {0} ", result); Console.ReadLine ();}
Running result:
Method is called.
10. The above methods can only be called on functions in C++ through static methods. So how do you call a method in a class object in C++ through a static method? Now I add a header file, userinfo.h, to the CPPDemo project:
Class UserInfo {private: char* masked name; int masked Age; public: UserInfo (char* name, int age) {m_Name = name; m_Age = age;} virtual ~ UserInfo () {} int GetAge () {return masked name;} char* GetName () {return masked name;}}
In CSharpInvokeCPP.CPPDemo.cpp, add some code:
# include "malloc.h" # include "userinfo.h" typedef struct {char name [32]; int age;} User; UserInfo* userInfo; extern "C" _ declspec (dllexport) User* Create (char* name, int age) {User* user = (User*) malloc (sizeof (User)); userInfo = new UserInfo (name, age); strcpy (user- > name, userInfo- > GetName ()); user- > age = userInfo- > GetAge (); return user;}
A structure, including name and age, is declared here, which is used to map to the structure of the C # aspect.
Note: the User* in the code is a pointer, and the return is also an object pointer, to prevent the release of local variables after the end of the method scope.
Strcpy is a function that copies an array of char.
11. Supplement the code in the CPPDLL class in the CSharpDemo project:
[DllImport ("CSharpInvokeCPP.CPPDemo.dll")] public static extern IntPtr Create (string name, int age); [StructLayout (LayoutKind.Sequential)] public struct User {[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 32)] public string Name; public int Age;}
The structure User here corresponds to the User in C++.
twelve。 Supplement the code in Program.cs:
IntPtr ptr = CPPDLL.Create ("Li Ping", 27); CPPDLL.User user = (CPPDLL.User) Marshal.PtrToStructure (ptr, typeof (CPPDLL.User)); Console.WriteLine ("Name: {0}, Age: {1}", user.Name, user.Age)
Note: in the red font section, the structure pointer is first converted to an IntPtr handle, and then to the structure you need through Marshal.PtrToStructrue.
Running result:
At this point, the study on "how Visual Studio 2010 invokes DLL files written by non-C #" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.