One, type conversion
Below is a list of common types of conversions.
C + + Types |
C # types |
Remark description |
Int |
Int16, Int32 |
No suspense, direct conversion |
Uint |
UInt16, Uint32, int |
In the program, it is not clear that you can directly correspond to an int |
Long |
Int32 |
Long relative int is stereotyped, corresponding to Int32 |
DWORD (unsigned long) |
Uint32 |
|
WORD (unsigned short) |
Uint16 |
This is the perception of word. |
Byte (Unsigned Char) |
Byte |
In |
DECIMAL |
Decimal |
Number of digits conversion |
BOOL |
bool |
|
Char |
Char |
This does not have the pointer, is relatively easy, directly corresponds to seated |
Handle (void *) |
Intptr |
function is a window handle, C # is the default for IntPtr |
Hmodule |
Intptr |
Ditto |
Histance |
Intptr |
Ditto |
INT *, long * |
ref int, ref long |
This shaping pointer, in the program is for reference, so in C # corresponds to ref, so add ref before the keyword. |
Int &.long & |
ref int, ref long |
Explanation Ibid, of course, added in the keyword out, is also possible |
char * (LPSTR, Pchar), const char * (LPCSTR), |
String |
In order to get the data, string is referenced in the application in C #, so it is changed directly to string. IntPtr can also, do not advocate. |
Byte * |
Ref Byte, byte[] |
The program byte * is to get the type of byte data, and in C # it uses a byte array to get the stored data. String can also, but does not advocate, the key to look at the data obtained, what to do with |
Guid |
Guid |
|
Char[],byte[],in[] |
can correspond to the conversion of a pointer type to one |
Char[],byte[],int, when applied in C + +, initializes the array, then defines a pointer to the data address and then accesses it, so the conversion is, in the corresponding char* "string, Int[]>int * > IntPtr |
Char **,byte * * |
Intptr |
This double-pointer call is generally accessed by a two-bit array, so we deal directly with IntPtr, of course IntPtr and not before, need to handle, can see the example description. |
struct * variable name, struct & variable name. |
REF struct-Body variable name, IntPtr |
In the struct reference, or the incoming value C + + is generally used as a pointer, in C #, with ref instead. Of course IntPtr is also possible, but not very convenient. |
Typically, you should work as soon as you choose to find the matching CLR (Common language runtime, responsible for resource management: memory allocation and recycling, and the necessary separation between the application and the underlying operating system) in the Win32 run environment. Of course there are exceptions: BOOL is found in C + + in fact int, so it is converted to int instead of BOOL.
Pointer parameters, which are used as one or more parameters in WINAPI many functions. The function of a pointer is to store the address of the data, not the data. The addition of pointers increases the complexity of the data while increasing the flexibility of the data, enabling the data to be passed in and out, if only the value type is applied only to the incoming data. If there is no pointer in the app, you can pass the data directly through the value thread stack. With pointers, you can pass data by reference, push the memory address of the data into the thread stack, and the function accesses the data indirectly through the memory address. In C # The keyword is defined as a pointer-like function with ref, out. Out is a parameter specification for ref, in fact they produce the same machine code in the run, the out function in order to let the caller understand that the data is only outgoing, ref indicates that the data is also obtained. Ref, out parameters in managed code another good one is that you can provide an address for invocation as a struct, class, and array. A more complex CLR type is encapsulated only if the ref or out parameter is found to be out of compliance.
In the Windows API there will be a window handle to get or assign a value, its methods are not transparent, such as handle, void *, histance, and so on.
In rare cases, API functions also define opaque pointers as PVOID or LPVOID types. In the definition of Windows API, these types mean that the pointer has no type. When an opaque pointer is returned to your application (or if your application expects an opaque pointer), you should marshal the parameter or return value to a special type of-system.intptr in the CLR. When you use the INTPTR type, the out or ref parameters are usually not used, because IntPtr means to hold the pointer directly.
The
IntPtr in the CLR type system is a special property that does not have a fixed size and is then bound at run time, depending on the normal pointer of the operating system. This means that in 32-bit Windows, the width of the INTPTR variable is 32 bits, whereas in 64-bit Windows, the code compiled by the real-time compiler will treat the INTPTR value as a 64-bit value. This auto-sizing feature is useful when you are marshaling opaque pointers between managed and unmanaged code. However, when using Windows API functions, because pointers should be opaque, they cannot be used in addition to storing and passing to external methods. The two exceptions to this "store-and-pass-only" rule are when you need to pass a null pointer value to an external method and you need to compare the INTPTR value with a null value. To do this, you cannot cast 0 to System.IntPtr, and you should use the Int32.zero static public field on the IntPtr type to obtain a null value for comparison or assignment.
Marshal text, mainly refers to when the data is obtained, the data may be stored in a char array, if we receive with a string, it is possible to garbled. So during a function call, when Char *,char[] is as input data, it can be changed to string. When it comes out as data, you have to think about it, sometimes you need to change to char []. In the c+ program, the string in C is actually just an array of character values, usually null, and most Windows API functions follow the ANSI as an array of character values (more commonly used), and for Unicode, as a wide-character array of values. Sometimes when the data obtained is garbled, it may be necessary to convert to Unicode, it is resolved. Most Windows API functions have LPTSTR or LPCTSTR values. They are both modifiable and non-modifiable buffers that contain a null-terminated array of characters. "C" stands for const, meaning that data is not passed outside the function. "T" means that the parameter can be Unicode and ANSI, depending on the character set you choose and the character set of the underlying operating system in the CLR run: So when the function is declared, add DllImportAttribute as CharSet.Auto. If the string argument is used only as input, the System.String type is used. In managed code, strings are immutable, and are suitable for buffers that are not changed by native API functions. If the string parameter can be used as input and/or output, the System.stringbuilder type is used. The StringBuilder type is a useful class library type that can help you build strings efficiently, and you can pass buffers to native functions, which populate string data for you by native functions. Once the function call returns, you only need to invoke the ToString of the StringBuilder object to get a String object.
The effects of the charset variables on char and char[] are as follows:
Ansi:char and char[] account for one byte
Auto:char and char[] accounted for two bytes
Unicode:char and char[] accounted for two bytes
The general principles can be summed up as:
1. The basic types commonly used in C + + (numeric type, byte type) are converted directly to numeric types in C #. (The principle is the number of bytes, OK.)
2. The pointer type (numeric type *, byte type *) commonly used in C + + translates to a ref value type, a ref byte type, in C #. However, when used for char *, it is converted to string.
3, in C + + common construction type (struct, array, enum type, common body) row pair is more complex. Enumerations and pooled direct replication can be used, and the struct's declaration then focuses on when the function is called, if the struct * variable name is a ref struct variable name. Array in function call, can write directly to the array name, can also be written as IntPtr.
Second, the structural body
1, the redefinition of the structural body.
In C + + there will be a lot of structures, the structure of the body has a variety of data types, so it involves the conversion of data types, but also in the structure through the acquisition of data, it involves the encoding conversion problem.
There are many grammatical similarities between struct types and class types, both of which are data structures that can include members of a database and methods.
struct and class differences:
1, the struct is a value type, it allocates space in the stack, the class is the reference type, he allocates the space in the heap, the stack stores the reference.
2. struct type stores member data directly, the data type in the class exists in the heap, and then accesses the data by referencing it in the stack. Because structs are value types, they are stored directly, so the use of structs is more efficient when the primary members of the object are data-cut.
3, the structure directly contains its own data, each structure holds a copy of the data, in the program declaration two structure object, change one, the other data is unchanged, but the class is a reference, the other data will change.
4. The struct is a value type and cannot be initialized to NULL, and the data is copied when copied. The copy in the class is a reference copy, and when the data is large, the structure copy is not very good.
Applications for structural and class analysis:
1, when the stack space is very limited, and there are a large number of logical objects, the creation of the class is better than the creation of the structure;
2. For lightweight objects such as points, rectangles, and colors, if you want to declare an array with many color objects, the CLR needs to allocate memory for each object, in which case the cost of using the structure is lower;
3. Classes are the best choice when performing abstract and multi-level object hierarchies, because structs do not support inheritance.
4, in most cases, the target type only contains some data, or data-based.
The declaration, initialization, and reference of a struct are still important in C #, which is analyzed according to the code.
[CPP]View Plaincopyprint?
- C + + structural body
[CPP]View Plaincopyprint?
- Video Index list file
- typedef struct Tagindex_info
- {
- DWORD Dwstarttime; //Recording start time
- DWORD Dwendtime; //Recording stop time
- BYTE Btfiletype; //File type
- BYTE Btfilestatus; //File status
- BYTE reserved[2]; //Reservation, LMC to NVR request playback Reserved[0] Identity NVR send speed, reserved[1] store video inverted flag
- BYTE btmac[6]; //Device MAC address
- WORD Wchan; //Device channel
- DWORD dwIP1; //Equipment IP1
- DWORD dwIP2; //Device IP2, in public mode, stores the IP of the NVR where this file resides
- DWORD dwIP3; //device ip3,v3061 used to store video footage in the offset of the recording file, do not move
- DWORD dwIP4; //device ip4,v3061 used to record file name, do not move
- DWORD Dwfileoffset; //file offset for file download with breakpoint continuation and location
- DWORD dwreserved; //reserved, 3070 useful, don't touch it.
- }index_info, *lpindex_info;
C # struct BODY
[CPP]View Plaincopyprint?
Video Index list file
[StructLayout (layoutkind.sequential, CharSet = Charset.ansi,pack = 1)]
public struct Net_index_info
{
Public UInt32 Dwstarttime; Recording Start time
Public UInt32 Dwendtime; Recording Stop Time
public byte Btfiletype; File type
public byte Btfilestatus; File status
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 2)]
public byte [] Reserved; Reserved, LMC to NVR request playback when reserved[0] identity NVR send speed, reserved[1] store video inverted flag
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 6)]
public byte [] btmac; Device MAC Address
Public UInt16 Wchan; Device Channel
Public UInt32 dwIP1; Equipment IP1
Public UInt32 dwIP2; Device IP2, public network mode, the IP of the NVR where this file is stored
Public UInt32 dwIP3; Device ip3,v3061 is used to store video footage in the offset of the recording file, do not move
Public UInt32 dwIP4; Device ip4,v3061 used to record file name, do not move
Public UInt32 Dwfileoffset; File offset for file download with breakpoint continuation and location
Public UInt32 dwreserved; Reserved, 3070 useful, don't move it
}
The above member must be preceded by a public, since the default is private.
2. StructLayout features the common language runtime uses StructLayoutAttribute to control the physical layout of the data fields of a class or structure in managed memory, where classes or structs need to be arranged in a certain way. If you are passing a class to unmanaged code that needs to specify a layout, it is important to explicitly control the class layout. A new instance of the StructLayoutAttribute class is initialized with the LayoutKind value in its constructor. LayoutKind.Sequential is used to force the layout of members sequentially in the order in which they appear. The System.Runtime.InteropServices.StructLayout allowed values are Structlayout.auto structlayout.sequential structlayout.explicit. In the application in order to pass the data smoothly, it is generally used structlayout.sequential, which means that the structure of the body data into the pattern according to the declaration. Structlayout.explicit needs to set the location of each member with FieldOffset () so that the functionality of a common body like C can be implemented. [StructLayout (LAYOUTKIND.EXPLICIT)]
struct S1
{
[FieldOffset (0)]
int A;
[FieldOffset (0)]
int b;
}
So that A and B have the same address in memory
The StructLayout feature supports three additional fields: CharSet, Pack, Size.
· CharSet defines how string members in a struct are arranged when the structure is passed to the DLL. Can be Unicode, ANSI, or Auto.
The default is auto, which in win NT/2000/XP indicates that strings are arranged in a Unicode string, and in win 95/98/me, they are arranged in ANSI strings.
· Pack defines the package size of the structure. Can be 1, 2, 4, 8, 16, 32, 64, 128, or special value 0. A special value of 0 indicates the default compression size for the current operating platform.
3.MarshalAs uses the MarshalAs property to indicate how data is marshaled between managed and unmanaged code. [MarshalAs (UnmanagedType UnmanagedType, named parameter)]
Common UnmanagedType Enumeration values: (More on MSDN)
The BStr length prefix is a double-byte Unicode string;
LPStr a single-byte, null-terminated ANSI string. ;
LPWSTR a 2-byte, null-terminated Unicode string;
ByValArray the inline fixed-length character array that appears in the structure, always use the MarshalAsAttribute sizeconst field to indicate the size of the array. What is commonly used is that the array corresponds to the byavlarray,string corresponding to the BYVALSTR.
C # vs. C + + Data transformation