web123456

C# calls custom functions in C language, [Transfer] Call C language functions in C# (statically call Native DLL, Windows & Platform)...

For those who don’t know much about .Net, if they want to know .Net, I must introduce him to P/Invoke. What is P/Invoke? Simply put, it is to call local code in .Net (NativeA solution to code). The so-called "local code" is relative to managed code.

P/Invoke is really awesome feature. Originally, the .Net technology fully demonstrates the various benefits of managed programs, but it is not "bottom-level". But, what does this matter? We have P/Invoke! In this way, the advantages of hosting code and the need to call native APIs are seamlessly integrated.

I often see some newbies in forums: "I just learned C# and I think it is great and convenient. My question is: Can I use it to call the interface API provided by SMS Modem manufacturers? These interface APIs are C++~~"... OK, now once you understand P/Invoke, you can completely eliminate this concern.

Let’s talk less, let’s take a look at our example.

Our example is: Write a C languagefunctionEncapsulate it into a dynamic link library and then call it easily in a C# program.

Implementing such an example is really meaningful to many people, and from now on, you can no longer worry that .Net is not "bottom-level" enough.

Let’s look at our C functions first:

int sum(int a, intb)

{return a +b;

}

Simple enough.

1. Expose the function interface for the dynamic link library

Now we decided to encapsulate it into a dynamic link library. In order to allow it to be encapsulated into the dynamic link library, we add this to the preceding statement of this function:

__declspec(dllexport)

source codeIt became like this:

__declspec(dllexport) int sum(int a, intb)

{return a +b;

}

2. Compile and get the dynamic link library

Then, we useVisualC++'s own command line tools cl and link encapsulate it into a dynamic link library. Assuming the file name is, we want to get the command as follows:

cl /c

link/dll

We can also use gcc to compile. The command is as follows:

gcc -shared -o

Now we get it.

Note: There are many ways to get it. If you want to know more, please read two short articles:

Compile C language source code into dynamic link library /xinyaping/article/details/7284899

Visual C++ 2010 Express Tips: Create dynamic link library with C and C++ /xinyaping/article/details/7288164

3. In C#, sum() method is called in P/Invoke

P/Invoke is simple. Please see the following simple C# code:

//-----------------------------------------------------------------------P/Invoke example.-----------------------------------------------------------------------

namespaceInvoke

{usingSystem;;///

///.Net P/Invoke example.///

internal classProgram

{///

///Entry point of the application.///

/// Console arguments.

internal static void Main(string[] args)

{int result = Sum(2, 3);

("DLL func execute result: {0}", result);

}///

///Call method int sum(int, int) defined in ///

/// parameter a

/// parameter b

/// sum of a and b

[DllImport("", EntryPoint = "sum")]private static extern int Sum(int a, intb);

}

}

The above code is simple enough. Remove comments, console output, and fragmented parts, and this is the rest:

[DllImport("", EntryPoint = "sum")]private static extern int Sum(int a, int b);

This is the famous P/Invoke. Note that here I deliberately used a function name Sum that is different from that in the C language source code. The function name in the C language source code is sum. If C# also uses the function name sum, then the DLLImport can be written like this:

[DllImport("")]

Here is just to show you that when the function name in C# and the function name in DLL are inconsistent, you can use EntryPoint to map (mapping).

Compile and execute this C# program, don't forget to copy it to the execution directory when executing. The result is of course what we expected:

The example is simple, but the meaning is not simple.

References:

Compile C language source code into dynamic link library /xinyaping/article/details/7284899

Visual C++ 2010 Express Tips: Create dynamic link library with C and C++ /xinyaping/article/details/7288164

An Introduction to P/Invoke and Marshaling on the Microsoft .NET Compact Frameworkhttp://msdn./en-us/library/

Essential P/Invoke /Articles/12121/Essential-P-Invoke

What can .Net do /xinyaping/article/details/6722015

---------------------

Author: YapingXin

Source: CSDN

Original text: /yapingxin/article/details/7288325

Copyright Statement: This article is an original article by the blogger. Please attach a link to the blog when reprinting!