C#編寫和調用動態鏈接庫


一、創建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();

好了!結束!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM