In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the problems of enabling CLR to let C# call C++". In daily operation, I believe many people have doubts about what problems there are when enabling CLR to let C# call C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "enabling CLR to let C# call C++". Next, please follow the editor to study!
Existing problems
The previous method makes it possible for C# to call CCompact +, but it has many disadvantages, mainly in the following aspects:
The use of extern "C" _ declspec (dllexport) declaration can only define C functions, can not directly use C++ classes, the function is not powerful enough.
Parameter passing is very troublesome, especially when passing in an array, there is often the wrong parameter type or incorrect array length, which is very inflexible.
You need to write repetitive code, you need to repeatedly declare the functions in the DLL written in C # code, and if you define the structure in the C # code, you also need to repeat the declaration in C #, so it is troublesome to deal with parameter types.
You need to manually copy DLL to the directory of the C# program. If you forget to copy it, the program will report an error that DLL did not find at run time.
It is very difficult to debug, and breakpoint tracking debugging cannot be done in the Candlespace + code. At the same time, you need to copy DLL to the C # program directory after modifying and compiling the code, otherwise the C # program calls the DLL before it is modified.
Some time ago, I developed an application that needs to control two data acquisition cards (SP Divece ADQ and SDR). The official C and C++ drivers can be used to control the data acquisition card. At the beginning, I used the way I previously declared the export function to develop, implemented in C language, but with the complexity of the function and the increase of code, the above series of problems become more and more serious.
Discovery of the New World (common language runtime compilation)
While struggling to solve all kinds of Bug, I suddenly found common language runtime compilation during a search. The so-called common language runtime compilation is to allow applications and components to use the functions of the common language runtime (CLR). Find the relevant documentation on MSDN:
/ clr (common language runtime compilation).
Mixed (native and managed) assemblies.
How to: compile MFC and ATL Code using / clr.
With the common language runtime compilation, the DLL developed by C++ can be referenced in the C # assembly, and the classes using C++ are the same as using C # classes, and you can also debug at breakpoints directly, all of the above problems are solved.
Soon, I rewrote the code I had written in C into C++ code, enabled CLR, and deleted the repeated code in C #.
Pointer problem
Pointers are often used in C++ development, but C # does not have pointers (in general, C # does have pointers, but it is turned off by default). There are at least two ways to pass a pointer in C#:
Use stackalloc to allocate blocks of memory on the stack, which is similar to C's malloc and C++ 's new (there are differences, of course).
The fixed statement is used to fix the pointer of the variable. The reason why pointers are not allowed in C # is that the garbage collection mechanism will cause the variable to be relocated, and after the variable is relocated, the previous pointer will no longer point to the variable, so C # should prohibit the use of pointers in this case. The fixed statement prohibits the garbage collector from relocating a movable variable and "pinning" the variable during the execution of the statement. After fixing the position of the variable, you can use the pointer.
At this point, the study on "what are the problems in enabling CLR to let C # call C++" is over. I hope I can 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.