Type conversion when C # calls a C + + version DLL Summary _ Practical Tips
Source: Internet
Author: User
C # frequently encounters type conversions and struct conversions when referring to DLL references for C + +
1. C + + in the char type is 1 bytes, C # char is two bytes, can not be used; You can use the byte correspondence in C #
2. Structtype temp = (structtype) marshal.ptrtostructure (IntPtr, typeof (Structtype)); Note: This conversion is only for structures that contain the basic types of C + +. It is convenient to use a generic function if you have a structure that contains an array of pointers.
3. [StructLayoutAttribute (layoutkind.sequential)] Description: StructLayoutAttribute class enables the user to control the physical layout of a class or struct's data field ; LayoutKind controls the layout of the object when exported to unmanaged code.
4. [MarshalAsAttribute (UNMANAGEDTYPE.LPWSTR)] Indicates how data is marshaled between managed and unmanaged code; Description: UnmanagedType specifies how to marshal parameters or fields to unmanaged code.
Managed codes (managed code)
Code that is executed by the common language runtime environment, not directly by the operating system. Managed code applications can obtain common language runtime services, such as automatic garbage collection, runtime type checking, and security support. These services help provide a unified, platform-and language-independent, managed-code application behavior.
Managed code is code that can be written using more than 20 high-level languages that support the Microsoft. NET Framework, including: C #, J #, Microsoft Visual Basic. NET, Microsoft JScript. NET, and C + + 。 All languages share a unified collection of class libraries and can be encoded into intermediate language (IL). The runtime compiler (Runtime-aware compiler) compiles intermediate language (IL) in a managed execution environment to make it executable locally, and uses array bounds and index checking, exception handling, garbage collection, and so on to ensure type safety.
Using managed code and its compilation in a managed execution environment avoids many of the typical programming errors that lead to secure black holes and unstable programs. Similarly, many unreliable designs are automatically enhanced with security, such as type security checks, memory management, and the release of invalid objects. Programmers can devote more attention to the application logic design of the program and reduce the amount of code written. This means shorter development times and more robust programs.
unmanaged codes (unmanaged code)
Code that is executed directly by the operating system outside of the common language runtime environment. Unmanaged code must provide its own garbage collection, type checking, security support, and other services, unlike managed code, which obtains these services from the common language runtime.
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