C # Summary of notes for type conversion when calling C ++ dll

Source: Internet
Author: User

C # type conversion and struct conversion often occur when C ++ dll is referenced.

1. the Char type in C ++ is 1 byte, And the Char type in c # Is two bytes, which cannot be used. You can use the byte type in c #.

2. structType temp = (structType) Marshal. ptrToStructure (IntPtr, typeof (structType); Note: This method is only applicable to struct containing the c ++ basic type. If the struct contains the pointer array, it is more convenient to use generic functions.

3. [StructLayoutAttribute (LayoutKind. Sequential)] Description: The StructLayoutAttribute class allows you to control the physical layout of data fields of a class or structure. LayoutKind controls the layout of objects when exported to unmanaged code.

4. [financialasattribute (UnmanagedType. LPWStr)] indicates how to mail data between hosted and unmanaged code. Description: UnmanagedType specifies how to mail parameters or fields to unmanaged code.


Managed code)

Code executed by the Common Language Runtime Library environment (rather than directly by the operating system. Hosted code applications can obtain the Common Language Runtime library service, such as automatic garbage collection, Runtime Library type check, and security support. These services help provide unified managed code application behavior independent of platform and language.

Managed code supports more than 20 types of Microsoft. NET Framework, including C #, J #, Microsoft Visual Basic. NET, Microsoft JScript.. NET, and C ++. All languages share a unified set of class libraries and can be encoded as intermediate languages (IL ). Runtime-aware compiler compiles the intermediate language (IL) in the Managed execution environment to make it executable locally, and checks array boundaries and indexes to handle exceptions, garbage collection and other measures to ensure the security of the type.

Using managed code and its compilation in a managed execution environment can avoid many typical programming errors that cause security black holes and unstable programs. Similarly, many unreliable designs automatically enhance security, such as type security checks, memory management, and invalid object release. Programmers can spend more time focusing on application logic design and reduce the amount of code written. This means shorter development time and more robust programs.


Unmanaged code)
Code that is directly executed by the operating system outside the public Language Runtime Library environment. The unmanaged code must provide its own services such as garbage collection, type check, and security support. Unlike the hosted code, the latter obtains these services from the public Language Runtime Library.

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.