Using a DLL written in C/C + +

Source: Internet
Author: User
C++

Using a DLL written in C/C + +

Well, it's time for me to write something. Recently in the forum always see a friend ask

How does the C + + program work in C #? Actually, it's a very good idea. Code reuse. But

How do managed programs use unmanaged code? Think about it, it's natural to associate DLL Dynamic Connection library

To compile the C + + code into a DLL, and then use, here I assume that my C + + code contains a

function, called average (int av[])

Stated as follows:
extern "C" __declspec (dllexport) __cdecl int average (int *av);

It's easier to implement.
__declspec (dllexport) __cdecl int average (int av[])
{
int i=0;
int sum=0;
while (AV)
{
Sum+=av[i];
i++;
}
return sum/i;
Maybe there's something wrong with}//'s writing.

Okay, no matter how much, let's take a look at how C # code is implemented, and I think you should think about

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

DllImport property, that's it, see what it has, I can use it, MSDN is the best choice. See that?

Yes, to be exact, the DllImport property has the following behavior:

It can only be placed on the method declaration.

It has a single positional parameter: Specifies the DllName parameter that contains the DLL name of the method being imported.

It has five named parameters:

The CallingConvention parameter indicates the calling convention for the entry point. If CallingConvention is not specified, the default value is used
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 gives the name of the entry point in the DLL. If entrypoint is not specified, the name of the method itself is used.

The ExactSpelling parameter indicates whether the entrypoint must exactly match the spelling of the indicated entry point. If you do not specify
ExactSpelling, the default value false is used.

The PreserveSig parameter indicates whether the signature of the method should be preserved or converted. When the signature is converted, it is converted to a
HRESULT returns the signature of the value and an additional output parameter named RetVal for the return value. If PreserveSig is not specified, the
Use the default value 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, a method decorated with the DllImport property must have an extern modifier.

Yes, the above is the MSDN original content. All right, let's go on with our example, okay, first compile our C code into a DLL

What's the name, then call Mydlltest.

[DllImport ("MYDLLTest.dll", entrypoint= "average", exactspelling=false,callingconvention=callingconvention.cdecl) ]
static extern int average (int av[]);

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

Such as:

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 so many problems to design

You see in the example above, int is in C # and C + +, but what if it's char *? You might say string.

But you see in C + + these all represent strings (Char*,or wchar_t*,or BSTR, ...) So what do we do?

Think about it, what else? Property? Yes, look. I seem to think of a word. Marshal, uh. Remember it?

Don't tell me you're going to spell it Marshall. haha. A joke is a set of columns. We'll type it on MSDN. What did we find?

Yes, MarshalAs. Look at what the MSDN says? Indicates how data should be marshaled between managed and unmanaged code.

Haha, it looks good. Take a look at the following remark:

You can apply this attribute to parameters, fields, or return values.

This property is an optional property because each data type has a default marshaling behavior. This property is required only if a given type can be marshaled to more than one type. For example, a String can be marshaled as LPSTR, LPWStr, LPTStr, or BStr to unmanaged code. By default, strings are marshaled as BStr to COM methods. You can apply the MarshalAsAttribute property to an individual field or parameter so that the string is marshaled as a LPStr instead of a BStr. For complete details on how to use this property, see Data Marshaling specification.

In most cases, this property simply uses the UnmanagedType enumeration to identify the format of unmanaged data, as shown in the following example.

[C #]
void MyMethod ([MarshalAs (LPSTR)] String s);
Some UnmanagedType enumerations require additional information. For example, additional information is required when UnmanagedType is LPArray. For a complete description of how to use this property, see Data Marshaling specification.

See, that's exactly what we need, right? Well, I'll just write how much, in fact, there's a structure (struct)

That's what you're going to do. I hope it will help you a little. Finish



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.