[Windows question-3] C # converting to a DLL file

Source: Internet
Author: User

[Windows question-3] C # converting to a DLL file

Problem:This article consists of two parts: the first part describes how to convert a C # program file into a DLL dynamic link library file, and the second part describes how to use the generated DLL file in the program.

Part 1:

1. Create a new C # file "show. cs", which is the source file to be converted to the DLL file. The file content is as follows:

using System;namespace Dll{    public class Show    {        public String hello = "hello world\n";        public String Message()        {            return "Welcom C# dll\n";        }    }}

2. Convert the CEN file into a DLL file. Use the csc.exe file that comes with Windows. Generally, the path of this file is "C: \ Windows \ Microsoft. NET \ Framework \ v2.0.50727 \ csc.exe ". For ease of use, we add this PATH to the PATH environment variable of the system. The specific method is "computer", right-click "properties", "Advanced System settings", "environment variables", "system variables", select "Path", and "edit ", add the preceding path to the end of the string. NOTE: If there is no semicolon at the end of the string, you must manually add an English semicolon.


3. Open cmd.exe and enter csc/t: library/r: System. Web. dll/out: d: \ Test \ show. dll d: \ Test \ show. cs in the command line.

PS:

/T: library: This indicates that we want to convert the c # file into a class library.

/R: System. Web. dll l: This dll is referenced in the cs file.

/Out: d: \ Test \ show. dll: directory of the output dll file

D: \ Test \ show. cs: Location of the C # file to be converted

After the command is executed, the show. dll file is generated at the specified position.

Part 2:

1. Create a new C # file test. cs with the following content:

using System;using System.Data;using Dll;public class GetMessage{    public static void Main() {        Show sw = new Show();Console.Write(sw.Message());Console.Write(sw.hello);                Console.Read();    }}

Note: The using Dll Declaration uses the Dll namespace, which is defined in show. cs.

Console. Read (); to prevent the Dos debugging window from flashing.

2. Enter csc/r: show. dll/r: system. dll test. cs in the cmd command line.

This command allows test.csto use the show.dll file and generate a test.exe executable file.


3.double-click the test.exe file to view the corresponding execution result.



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.