Write DLL written in C / C in C #
Ha ha .. I have to write something when I want to write something. Recently, I have seen friends when I have seen it.
How does the C / C program use in C #? In fact, this is a very good idea. Code multiplexing. But
How does the hosting program use a non-hosting code? Think about it, naturally, Lenovo, DLL dynamic connection library
Compile C / C code into a DLL, then use it, here I assume that my C / C code contains one
Function called Average (int av [])
Decliminate as follows: Extern "C" __declspec (dllexport) __cdecl int average (int * av);
What is the implementation is more simple __declspec (dllexport) __cdecl int averge (int av []) {INT i = 0; int sum = 0; while (av) {SUM = AV [i]; i ;} Return SUM / I } // Writing may have a little problem
Well, no matter how much, let's take a look at how the C # code is implemented, I think you should think
What? Guess, ok, I think you can at least think of two English words DLL import, you guessed
Dllimport attribute is it, look at it, I can use, MSDN is the best choice. Have you seen it?
Yes, it is accurately that the DLLIMPORT attribute has the following acts:
It can only be placed on the method statement.
It has a single positioning parameter: specify a dllname parameter containing the DLL name of the imported method.
It has five named parameters:
The CallingConvention parameter indicates the invocation of the entry point. If you do not specify a callingconvention, use the default value CALLINGCONVENTION.WINAPI.
The Charset parameter indicates the character set in the entry point. If a charset is not specified, use the default value charset.Auto.
The EntryPoint parameter gives the name of the DLL in the mouthpoint. If EntryPoint is not specified, the name of the method itself is used.
The Exactspelling parameter indicates whether EntryPoint must match the spelling of the indicated entry point. If you do not specify EXACTSPELLING, use the default value false.
The signature of the PRESERVESIG parameter indication method should be retained or converted. When the signature is converted, it is converted into a signature of an additional output parameter named RETVAL with the HRESULT return value and the return value. If PRESERVESIG is not specified, use the default true true.
SetLASTERROR parameter indicates whether Win32 "Previous Error" is retained. Use the default value if setLastError is not specified
False.
It is a disposable property class.
In addition, the method of modifying the DLLIMPORT attribute must have an extern modifier.
Yes, the above is MSDN's original content. Well, continue our example, okay, first compile us the C code above into a DLL
What is it? That is called mydlltest.
[DLLIMPORT ("Mydlltest.dll", entrypoint = "average", exactspelling = false, callingconvention = calingconvention.cdecl)] static extern int average (int av []);
Ok, it's time when you find a place to write a sentence.
Such as:
INT [5] i = {1, 2, 3, 4, 5}; int J = average (i);
Ok, you have seen it, it is like this, but in fact, it doesn't seem to be as simple as you think. Because you have a lot of problems
In the example of the above, int is in C # and C / C , but if it is char *? Maybe you will say String
But you see these all of C / C represent strings (char *, or wchar_t *, or bstr, ..) Then should we do? Think about it, what else? What is the property? Yes, find it, I seem to think about a word .Marshal. Remember it?
Don't tell me, you will take it into Marshall. Haha .. Opening a joke is a column. We entered the MSDN to see what is found?
Yes, Marshalas. What do you say on MSDN? "Indicates how to get data between managed code and non-hosting code."
Haha. It seems good. Take a look at the following:
This property can be applied to parameters, fields, or return values.
This property is optional attribute because each data type has a default encapsulation behavior. This property is required only when a given type is sealed to multiple types. For example, String may be served as LPSTR, LPWSTR, LPTSTR or BSTR. By default, strings are served as BSTR to COM methods. The Marshalasattribute property can be applied to individual fields or parameters to make the string as LPSTR instead of BSTR. For complete details on how to use this property, see "Data Sending Processing Specification".
In most cases, this property is just a format that enumerates the unmanagedType enumeration, as shown in the example below.
[C #] void mymethod ([MARSHALAS (LPSTR)] String S); some unmanagedType enumeration needs additional information. For example, additional information is required when unmanagedType is LPARRAY. For a complete description of this property, see "Data Sending Processing Specification."
I saw it, is what we need? Ok, how much I wrote, in fact, there is a struct
This needs, you have done it. I hope to help you. (End)