使用C#生成dll文件并调用

来源:互联网 发布:程序员 月薪3万以下 编辑:程序博客网 时间:2024/05/16 18:09
有两种方法:
    但是一般这个使用
    打开VS2008,依次点击:菜单->文件->新建项目->项目类型visual C#(这里假设为该项目所取的名字是DllBuild)->类库(注意必须是类库),即新建一个由纯.cs 类库文件组成的程序集,写好代码之后(例如写了一个名为DllTest.cs的类,该类的namespace取名为DllTestNS),再依次点击:菜单->生成->生成DllBuild,这样你的DllBuild/DllBuild/bin/Debug文件夹或者DllBuild/DllBuild/obj/Debug文件夹里便会自动生成 dll文件啦,该文件名称与项目名称一致,即为DllBuild.dll。
加载DLL:
放入程序的BIN目录下或者是添加工具栏选项

 

 

转二

 

使用C#生成dll文件并调用

http://hi.baidu.com/wuxiaoming1733/blog/item/8e6b8c020ea23f074bfb51d5.html

一、创建dll文件:

例如生成一个md5编码判断状态的文件,即,输入一个字符串(string A)和一个32位md5编码(string B),判断此字符串A对应的32位md5编码是否与B相等,如果相等返回true,否则返回false。

打开VS 2005,“文件”--》“新建”--“项目”,选择“Windows 控件库”,命名后点击“确定”,在“UserControl1.cs”中输入以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using System.Text;
using System.Security.Cryptography;

namespace md5
{
    public partial class Program : UserControl
    {
        #region MD5 32位加密:GetMd5Str32
        /// <summary>
        /// 32位MD5加密
        /// </summary>
        /// <param name="strSource">待加密字串</param>
        /// <returns>加密后的字串</returns>
        public static string GetMd5Str32(string strSource)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(strSource);
            byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < 16; i++)
            {
                sb.Append(hashValue[i].ToString("x2"));
            }

            return sb.ToString().ToUpper();
        }
        #endregion

        #region 核对md5编码是否一致:CheckMd5String()
       
        /// <summary>
        /// 核对md5编码是否一致
        /// </summary>
        /// <param name="ConvertString"></param>
        /// <returns>如果一致返回true,否则返回false</returns>
        ///
        public static bool CheckMd5String(string str1, string str2)
        {
            string md5String = str1;              //需要验证的字符串
            string md5DbString = str2;            //需要核对的32位md5编码

            int result = string.Compare(md5.Program.GetMd5Str32(str1), md5DbString, true);
            if (result == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}

修改“UserControl1.Designer.cs”中的命名空间为“md5”,方法为“Program”,即可生成dll文件。

在...\bin\Debug文件假下,可以找到相应的dll文件。

二、部署dll流程:

首先把dll文件放到应用程序...\bin\Debug\下;
然后在解决方案中添加引用:右键鼠标-->添加引用-->浏览-->选择dll放置路径后点击“确定”。
注意:要在应用文件头处使用using md5;命令。

测试应用程序代码,如下:Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using md5;

 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str1 = textBox1.Text.ToString();
            string md5String = textBox2.Text.ToString();

            textBox3.Text = md5.Program.GetMd5Str32(str1);
            textBox4.Text = md5.Program.CheckMd5String(str1, md5String).ToString();        
           
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

 

三、注意点:

1、在C#应用程序开发过程中,加载dll文件时,报错“未能加载文件或程序集“md5, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。”,请指点一下是什么原因?
解决:这是因为加载dll的路径问题,正确加载方式为:在“解决方案”的“引用”文件上右击鼠标,选择“添加引用”---》在“浏览”选项卡中添加引用(注意:自己定义的dll文件不能在“.NET”选项卡中添加。)

 

-------------------------------------------------------------------------------------------------------------------------------
关于 Asp.net 调用C#生成的DLL问题

生成以后,有2个文件:/bin/debug/xxx.dll和xxx.pdb, 这2个文件有什么用?  
   
我把2个文件copy到   asp.net   /bin/下  
然后引用该dll,  
.cs中使用   using   xxx;成功  
可是在创建新类的时候:yyy   myxx=new   yyy();//xxx.yyy()  
已经可以看到   myxx的属性和方法了(myxx.有提示),可是却报以下错,请教各位!  
   
发生类型为   System.StackOverflowException   的异常。    
说明:   执行当前   Web   请求期间,出现未处理的异常。
请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。     
异常详细信息:   System.StackOverflowException:发生类型为 System.StackOverflowException 的异常。
源错误:    
执行当前   Web   请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。      
堆栈跟踪:   
[StackOverflowException: 发生类型为 System.StackOverflowException 的异常。]
--------------------------------------------------------------------------------  
版本信息:   Microsoft   .NET   框架版本:1.0.3705.288;   ASP.NET   版本:1.0.3705.288
--
dll文件是你服务器端脚本编译后生成的组件,也就是说一但编译成dll后,
软件发行后,你对应页面的服务器端脚本文件.aspx.cs就不需要发布了,
因为代码已经封装在工程名.dll文件里了.而.pdb文件据我理解,可能是带上了一些资源类的文件吧,
所以文件要比相应的dll文件大,至于你说的这个问题,我没碰到过,帮不了你
--
我是在类库项目中生成的DLL,然后在asp.net项目中调用。  
btw:我刚才测试了一个新的C#生成的DLL,没有引入其它dll,
一样的操作不会报错,楼上的那个dll引用了其它dll,请问和这个有关吗?
----
我只能说有可能
---
那这样说你自己也发现了答案呢,把间接引用到的DLL也加入到工程中试试看
----
比如有一个别人写的aa.dll,在我的类库中引用了,还需要在asp.net工程中引用吗?
----
一般StackOverflowException是由于无限循环等原因引起的,看看你的代码会不会有这个问题。
----
比如有一个别人写的aa.dll,在我的类库中引用了,还需要在asp.net工程中引用吗?    
我猜想还应该用的吧,你自己做个小的程序来试试看。  
我觉得你的类库虽然引用了那个DLL,但并没有把那个DLL也编译进你自己的DLL中(有没有静态编译?)
----
静态编译?不是很明白我是通过vs.net生成的
----
debug会生成两个文件
pdf负责调试工作  
发布的时候应该使用realease版本
-------------------------------------------------------------------------------------------------------------------------------

c#生成DLL文件,内部函数的问题

用C#编写一组处理XML文档的代码,由于要求生成DLL文件,并由外部的其他工具访问动态库中的文件,
但是用Dependency Walker检测我生成的这个DLL文件没有显示任何的函数,以前没做过这方面的东西,求教了

代码如下:
using System;
using System.IO;
using System.Xml;
public class Sample
{
    public static void Main()
    {
        DeleteArg();
    }
     static void DeleteArg()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(@"c:\\data1.xml");
        XmlNode root = doc.DocumentElement;
        XmlNode Node1;
        XmlNodeList nodeList = doc.SelectSingleNode("/Entity/Columns").ChildNodes;
        foreach (XmlNode xn in nodeList)
        {
            XmlElement xe = (XmlElement)xn;
            if (xe.GetAttribute("Name") == "SysModuleID")
            {
                xe.RemoveAll();
                //xe.RemoveAttribute("Name");//删除Name属性
            }
        }
        doc.Save("c:\\data1.xml");//保存这个文档到文件中
    }
}

以上代码实现删除XML文件中某一节点的功能,如何在生成DLL后能够使用检测工具检测出DeleteArg函数,
使用Dependency Walker没检测出该函数是不是以为着这个动态库文件不能被调用.
----
因为.net的程序不是这样把函数放在导出表的, 我记得.net做的dll只导出了一个_CorDllMain的方法,
所以用Dependency Walker是看不出来的. 如果你想看.net做的dll导出了什么内容,可以用反射查看元数据
----
生成这个DLL库文件,是想要别的工具运行这个动态库文件,实现DELETEARG()这个函数的功能
----
可以的
----
你上面的代码不是生成DLL的,而是一个控制台应用程序.

要想创建动态库(DLL),在新建项目窗口中选择"类库", 默认的代码是这样的:

using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary2
{
    public class Class1
    {
    }
}


// 然后添加你的代码.最后代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ClassLibrary2
{
    public class Class1
    {
        public void DeleteArg()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"c:\\data1.xml");
            XmlNode root = doc.DocumentElement;
            XmlNode Node1;
            XmlNodeList nodeList = doc.SelectSingleNode("/Entity/Columns").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("Name") == "SysModuleID")
                {
                    xe.RemoveAll();
                    //xe.RemoveAttribute("Name");//删除Name属性
                }
            }
            doc.Save("c:\\data1.xml");//保存这个文档到文件中
        }
    }
}

最后编译一下就可以,
在Debug文件夹下回产生一个dll文件,最后在需要的工程里,将这个dll文件引进进去就可以用.
例如:
using ClassLibrary2;

......
Class1 class = new Class1();
class.DeleteArg();

好了!结束!

原创粉丝点击