C # uses some type conversions of the WIN32 function

Source: Internet
Author: User

C # will need to handle the mapping of C structures to C # structures when accessing the win + API, which can be found on MSDN and many blogs. In Win 32, there are some complex structs that have fixed-length arrays or some special structures, such as those that have char Name[ulen], GUIDs, handle, and so on. In Mprapi, there is a struct named Ras_connection_2, which describes the connection information for remote connection to the server "remote and Routing Access Service", which is a cascading, complex structure definition. Its win 32 is defined as follows:

typedefstruct_ras_connection_2 {HANDLE hconnection; WCHAR Wszusername[unlen+1];  Router_interface_type Dwinterfacetype;  GUID GUID; Ppp_info_2 PppInfo2;} Ras_connection_2,*Pras_connection_2;typedefstruct_ppp_info_2 {ppp_nbfcp_info NBF;  Ppp_ipcp_info2 IP;  Ppp_ipxcp_info IPX;  Ppp_atcp_info at;  Ppp_ccp_info CCP; Ppp_lcp_info LCP;} Ppp_info_2;typedefstruct_ppp_ipcp_info2 {DWORD dwerror; WCHAR Wszaddress[ipaddresslen+1]; WCHAR Wszremoteaddress[ipaddresslen+1];  DWORD dwoptions; DWORD dwremoteoptions;} Ppp_ipcp_info2;

The above lists only one level definition, ras_connection_2, ppp_fino_2->ppp_ipcp_info2, similar to the other levels. In such a structural encapsulation definition, it involves marshaling of fixed-length arrays (c + + to. Net), special type definitions (HANDLE & GUIDs), and struct inclusions. on MSDN [2], the following are found. NET has been encapsulated for special types of interactions.

Special value types
System Value class type IDL type
System.DateTime DATE
System.deimal DECIMAL
System.Guid Guid
System.Drawing.Color Ole_color

The Guid corresponds to System.Guid. Handle cannot correspond to System.Activities.Handle, and this class is independent of intermodulation. Handle general correspondence System.IntPtr, see the following code the first line. The definition of a fixed-length char array (instead of a string pointer) is shown in lines 5th and 6. The structure of line 7th also needs to be self-defined in C #, with the exception of naming others without special attention.

1 [StructLayout (layoutkind.sequential)]2  Public structras_connection_23 {            4          PublicIntPtr hconnection;5[MarshalAs (unmanagedtype.byvaltstr, SizeConst =Unlen)]6          Public stringWszusername;7          PublicRouter_interface_type Dwinterfacetype;8          PublicGUID guid;9          Publicppp_info_2 PppInfo2;Ten};

Reference:

[1] Default marshaling behavior, https://msdn.microsoft.com/zh-cn/library/zah6xy75.aspx

[2] can be straight copy and non-direct copy type, https://msdn.microsoft.com/zh-cn/library/75dwhxf7.aspx

[2] default marshaling of arrays-arrays within structures, https://msdn.microsoft.com/zh-cn/library/z6cfh6e6.aspx

C # uses some type conversions of the WIN32 function

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.