Dllzz written in C # using C/C ++

Source: Internet
Author: User

DLL written in C # using C/C ++

Haha... it's time for me to write something again. Recently I 've seen some friends asking me on the forum.

C/C ++ProgramHow to use it in C? In fact, this is a good idea.CodeReuse.

How do managed programs use unmanaged code? Think about it. Naturally, we think of the DLL dynamic Connection Library.

Compile the C/C ++ code into a DLL and use it. Here I assume that my C/C ++ code contains

Function, called average (int av [])

Statement:
Extern "C" _ declspec (dllexport) _ cdecl int average (int * AV );

Implementation is simpler.
_ Declspec (dllexport) _ cdecl int average (int av [])
{
Int I = 0;
Int sum = 0;
While (AV)
{
Sum + = Av [I];
I ++;
}
Return sum/I;
} // Handwriting may be a problem

Well, no matter how much code you need, let's take a look at how the C # code is implemented.

What is it? Guess, OK. I think you can think of at least two English words DLL import. Yes, you guessed it.

The dllimport attribute is it. Let's see what it has and I can use it. msdn is the best choice. Have you seen it?

Yes, to be precise, the dllimport attribute has the following behavior:

It can only be placed on the method declaration.

It has a single positioning parameter: Specify the dllname parameter that contains the DLL name of the imported method.

It has five naming parameters:
 

The callingconvention parameter indicates the call convention of the entry point. Use the default value if no callingconvention is specified
Callingconvention. winapi.

The charset parameter indicates the character set used in the entry point. If charset is not specified, the default value charset. Auto is used.

The entrypoint parameter specifies the name of the DLL entry point. If entrypoint is not specified, the method name is used.

The exactspelling parameter indicates whether the entrypoint must exactly match the spelling of the indicated entry point. If not specified
Exactspelling, the default value is false.

The preservesig parameter indicates whether the method signature should be retained or converted. When the signature is converted, it is converted to
The hresult return value and the signature of an additional output parameter named retval returned value. If preservesig is not specified
The default value is true.

The setlasterror parameter indicates whether the method retains Win32 "previous error ". If setlasterror is not specified, the default value is used.

False.

It is a one-time attribute class.

In addition, methods that are modified with the dllimport attribute must have the extern modifier.

Yes, the above is the original msdn content. Well, let's continue with our example. Well, compile the above C code into a DLL first.

What is it? So call mydlltest.

[Dllimport ("mydlltest. dll", entrypoint = "average", exactspelling = false, callingconvention = callingconvention. cdecl)]
Static extern int average (int av []);

Okay, it's time for you to find a place to write a sentence.

For example:

Int [5] I = {1, 2, 3, 4, 5 };
Int J = average (I );

 

Well, you see, that's it, but in fact it's not as simple as you think, because there are many problems with design.

In the above example, int exists in both C # and C/C ++, but what if it is char? Maybe you will say string

But in C/C ++, all these represent strings (char *, or wchar_t *, or BSTR,...). What should we do?

Think about what else? Attribute? Yes, look for it. By the way, I seem to think of a word. Thank Al. Do you remember it?

Don't tell me, you'll make it Marshall. Haha. A joke is the column set. Let's enter it on msdn to see what we found?

Yes, financialas. What do you mean on msdn? "Indicates how data should be sent between hosted code and unmanaged code"

Haha. It looks good. Let's take a look at the following remarks:

This attribute can be applied to parameters, fields, or return values.

This attribute is optional because each data type has a default sending and processing behavior. This attribute is required only when you can mail a given type to multiple types. For example, string may be sent to unmanaged code as lpstr, lpwstr, lptstr, or BSTR. By default, strings are sent to com as BSTR messages. You can apply the marshalasattribute to individual fields or parameters so that the string is sent as lpstr rather than BSTR. For more information about how to use this attribute, see the data mail handling specifications ".

In most cases, this attribute only uses the unmanagedtype enumeration to identify the format of unmanaged data, as shown in the following example.

[C #]
Void mymethod ([financialas (lpstr)] string S );
Some unmanagedtype enumeration requires additional information. For example, when unmanagedtype is lparray, additional information is required. For a complete description of how to use this attribute, see "Data mail handling specifications ".

You see, is it exactly what we need? Well, I will write more about it. In fact, there is something about the structure (struct ).

This is what you need. I hope it will be helpful to you)

Related Article

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.