How to create and call DLL using C #
First, write in front of the C # language is a simple but powerful programming language for writing enterprise applications. C # language evolved from C and C language, using many C features in terms of statements, expressions, and operators. The C # language has made considerable improvements and innovations in terms of type security, version conversion, events, and garbage collection. The C # language provides access to common API styles (such as .NET framework, COM, automation, and C style API, etc.). What is a dynamic link library? DLL three letters are very familiar with you, it is the abbreviation of Dynamic Link Library, the dynamic link library (DLL) is an executable file as a shared function library. Dynamic links provide a method that allows the process to call a function that does not belong to its executable code. The executable code of the function is located in one DLL, which contains one or more functions that have been compiled, link and separately stored with their processes. The DLL also helps share data and resources. Multiple applications can access the contents of a single DLL copy in the memory. Like most programmers, you must use the DLL. It also feels that it brings you a brigade design and coding. Today I want to explore one topic today: How to create and call the DLL in C #, in fact, in a great sense, DLL Let me have a more flexible organization to write our app, as a software designer, can reach a high code reusability according to it. Let me introduce how to create and call the DLL in C #. Second, preparing for us to make a simple introduction to what we have to do, in this article, we will create a dynamic link library named mydll.dll using the C # language. We will provide two in this dynamic link library file. A feature is a value that swaps their values for two parameters, and the other function is to find the maximum number of conventions of the two parameters. Then create an application to use this DLL. Run and output the result. Third, create a DLL to create the following three C # code files: 1, myswap.cs using system; namespace mymethods {public class swapclass {public static Bool Swap (Ref long i, ref ring j) {i = i j; j = IJ; i = IJ; return true;}}} 2, mymaxcd.cs}} 2, mymaxcd.cs}} 2, mymaxcd.cs}} 2, mymaxcd.cs}} 2, mymaxcd.cs}} 2, mymaxcd.cs {public class maxcdclass {public static long max (long i, long j) {long A, b, temp; if (i > j) {= i; b = j;} else {b = i; a = j;} temp = a% b; while (temp! = 0) {a = b; b = Temp; TEMP = a% b;} return b;}}}} Need to note: We can use Visual Studio.net or other text editor when making these two files, even if notepad can be. Although these two files are not in the same file, they are the same namespace (namespace). This is convenient for us to use these two methods.
Of course they can also belong to different namespaces, which is complete, but just need to reference two different namespaces when we apply them, so the author is recommended or written in a name space below. The next task is to turn the two CS files into the DLL file we need. The method is this: On the operating system installed in Microsoft .NET Framework, we can find the Microsoft.Net directory in the directory where Windows is located. The C # compiler is provided below this directory, csc.exe runs: CSC / target: library /out:maxcd.dll myswap.cs mymaxcd.cs can find the MyDLL.dll file we just generated below this directory / Target: library compiler Options Inform the compiler to output a DLL file rather than an Exe file. The / OUT compiler option after the file is used to specify the DLL file name. If the / out does not use the first file (mysWap.cs) as the DLL file name with the file name compiler. The generated file is the myswap.dll file OK! We created the task of the dynamic link library file, now we are time we enjoy the results, let me introduce how to use the dynamic link library files we created. Fourth, using the DLL we simply write a small program to test whether the two methods we have just written correctly, ok, come with me: myclient.cs using system; using mymethods; // Here we reference the namespace just defined, If we are just two files written in two different namespace class myclient {public static void main (string [] args) {if (args.length! = 2) {Console.Writeline ("Usage: myclient