How to manipulate C + + structure directly in C # __c++
Source: Internet
Author: User
How to manipulate C + + structures directly in C # calling C + + or system DLLs in C # is a more common operation.
For example, the following structural bodies are defined in C + +:
struct Rcestruct {
int Event;
int Flag;
Char user[40];
} ; There is also a public method:
extern "C" __declspec WORD CALLBACK getstruct (rcestruct* peventstruc);
We compile it as MyCppDll.DLL
So we can directly define the same structure and reference getstruct in C #:
[StructLayout (LayoutKind.Sequential)]
public struct Rcestruct {
public int Event;
public int Flag;
Public char[40] User;
}
[DllImport ("MyCppDll.dll", CharSet = CharSet.Auto)]
public static extern int getstruct (rcestruct rce);
Note that the structure defined in C # should be the same as defined in C + +. If this is a public string user there may be an error (I have not tried, I do not know whether C # will automatically convert char[] to string, and also note that in C #, the length should not exceed 40 when assigning a value to the user).
In this way we can pass or get the structure to C + +. But one limitation is that the getstruct () must be actively invoked on the C # side.
Another situation, in contrast to the previous one, is that instead of calling C + + class libraries in C #, we want to invoke our already-written C # class library in the C + + class library. This is achievable in managed C + +. One of the application cases is when writing C + + Plug-ins for Third-party systems, we must actively invoke the C # class library on the plug-in side (provided we need to use it, unless we write this plugin entirely with C + + code).
So we should be exposing methods in C # class libraries, such as:
public struct Rcestruct {
public int Event;
public int Flag;
public string User;
}
public void dosomething (Rcestruct rce) {
Rce. flag++;
Suppose compiled into MyCSharpDll.DLL
C + + END code is as follows:
# using < mscorlib.dll >
# using < CuteSuProc.dll >
void SomeMethod (Rcestruct * peventstruc) {
Assigning C + + structure body to C # structural body
mycsharpdll::rcestruct* csstruct;
Csstruct->event = peventstruc.event;
Csstruct->flag = Peventstruc.flag;
Csstruct->user?? Converts char to string and how it is handled in C + +.
Mycsharpdll::D osomething (csstruct);
Assigning C # structure body to C + + structure body
Since the Peventstruc is introduced by the outside world and is modified by the DoSomething method, the outside world may still need to know
Peventstruc->event = csstruct.event;
Peventstruc->flag = Csstruct.flag;
Peventstruc->user?? Convert string to char[]
Managed C + + is processing. NET class library, some details are very cumbersome, let people feel a little dizzy. For example, many places have to add __gc modifiers. And like arrays, string conversions are tricky. So there may be some minor bugs in the code above. But that's roughly what it means. Obviously, such a practice is very troublesome. Before the operation of the structure, we do a assignment, the operation, and then the assignment.
Is there any way to directly let C # operate the original structure? As with C # in C + +, there is no need to pass a middleman. Can you just do this:
# using < mscorlib.dll >
# using < CuteSuProc.dll >
void SomeMethod (Rcestruct * peventstruc) {
Mycsharpdll::D osomething (PEVENTSTRUC);
The answer is in the negative. We have no way to directly convert rcestruct in C + + into C # rcestruct.
Then there is one way to do this is to directly manipulate the memory. Because they are structures, they must be kept in contiguous memory space.
Let's take a look at how memory is manipulated in C #, which is unmanaged data. This requires referencing the System.Runtime.InteropServices namespace. Some of the static methods of Marshal under this namespace provide such functionality:
Marshal.readint32 ()//Read 4 bits from the specified memory address
Marshal.ptrtostringansi ()//reads a string from the specified memory address
Marshal.writeint32 ()///write integer to specified memory address
Marshal.writebyte ()//writes the string to the specified memory address let's look at the specific code:
Using System;
Using System.Text;
Using System.Runtime.InteropServices;
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