C # And memory pointer

Source: Internet
Author: User

C # And memory pointer
Source: qqread Author: Source: qiaoqiao reading
Key word: serverword.net
 
Though. in the runtime era of net, C is still an indelible pillar, including the existence of pointer types in most of the APIS seen in msdn, so how should we use pointers in C #, the first part of C?
In fact, C # can directly use structured variables to reference the API, but after all, it is not a pointer to some memory operations that involve storing Structured arrays, for example, I want to use an API of the structure array (this API is not required to be understood. We do not mean the API ):
Net_api_status NetShareEnum (
Lpwstr servername,
DWORD level,
Lpbyte * bufptr,
DWORD prefmaxlen,
Lpdword entriesread,
Lpdword totalentries,
Lpdword resume_handle
);

And the structure to be used (set level to 1 ):

Typedef struct _ share_info_1 {
Lpwstr shiw.netname;
DWORD shihei type;
Lpwstr shihei remark;
} Pai_info_1, * p1__info_1, * lp1__info_1;

In the third parameter lpbyte * bufptr, It is a pointer, rather than an array of C-type traditional languages, it can be directly declared as REF pai_info_1 [] bufptr, but I do not recommend this because C # does not seem to support structure array blocks, and it does not have array-type structure parameter overloading. For the moment, I will not talk about how to implement it. I want to implement it using the C operation method-pointer, So I converted it into C # and declared it as follows:

[Structlayout (layoutkind. Sequential)]
Protected struct pai_info_1 {
[Financialas (unmanagedtype. lpwstr)] Public String shimo-netname;
[Financialas (unmanagedtype. U4)] public uint shihei type;
[Financialas (unmanagedtype. lpwstr)] Public String shi1_remark;
}

[Dllimport ("netapi32.dll", entrypoint = "NetShareEnum")]
Protected static extern int NetShareEnum (
[Financialas (unmanagedtype. lpwstr)] string servername,
[Financialas (unmanagedtype. U4)] uint level,
Ref intptr bufptr,
[Financialas (unmanagedtype. U4)] uint prefmaxlen,
[Financialas (unmanagedtype. U4)] ref uint entriesread,
[Financialas (unmanagedtype. U4)] ref uint totalentries,
[Financialas (unmanagedtype. U4)] ref uint resume_handle
);

After the preceding function is executed, a data block will exist at the position indicated by the pointer bufptr. The data format of this memory block is arranged in the pai_info_1 structure (assuming level is 1 ), in addition, entriesread indicates the number of structures of the block. How can we convert it into a real available type to the variable pai_info_1 [] shareinfo, in fact, C # provides a kind of Marshal. ptrtostructure: it specifically moves the unmanaged memory block pointed by the pointer to the specified structure in the managed memory, but it does not support structure arrays (currently. net
1.1 has not been implemented by this overload), so it is better to move one by one (and one by one is more vivid). The example is as follows:

Int32 PTR = bufptr. toint32 ();
For (INT I = 0; I <entriesread; I ++ ){
// Start to move the memory block referred to by PTR to the memory block of the share_info_1 structure and sort the data in the share_info_1 format to the share_info_1 variable shareinfo.
Pai_info_1 paiinfo = (pai_info_1) Marshal. ptrtostructure (New intptr (PTR), typeof (pai_info_1 ));
PTR + = marshal. sizeof (partition info); // This is to move the pointer back to a structure bit because a structure has been moved

// Todo
}

The above example shows that bufptr is a pointer. In this example, the memory block (unmanaged memory) referred to by the pointer in the memory is moved in the size and arrangement format of a specified structure. net memory (managed memory) in a variable, and then move the pointer to a position (PTR + = marshal. sizeof (partition info), which is added by the current position of the pointer plus partition Al. sizeof (partition info) is obtained by the size already moved. It means to move the pointer to the end of the migrated data to prepare to move the structure block.

This example only describes the usage of pointers. It does not mean that the API does not require additional research on the API and its parameters.

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.