为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > [生活]C 如何动态调用DLL中的函数

[生活]C 如何动态调用DLL中的函数

2018-02-21 23页 doc 49KB 13阅读

用户头像

is_841159

暂无简介

举报
[生活]C 如何动态调用DLL中的函数[生活]C 如何动态调用DLL中的函数 C#如何动态调用DLL中的函数 主 题: 如何动态调用dll中的函数(急~在线等待) 作 者: wltj (武林天骄) 等 级: 信 誉 值: 100 所属论坛: .NET技术 C# 问题点数: 20 回复次数: 12 发表时间: 2004-3-19 10:27:51 我的意思是事先不知道dll的名字,也就是说不晓得调用哪个dll中的函数。但函数名是统一的 回复人: Ninputer(装配脑袋) ( ) 信誉:85 2004-3-19 10:40:37 得分:0 可...
[生活]C 如何动态调用DLL中的函数
[生活]C 如何动态调用DLL中的函数 C#如何动态调用DLL中的函数 主 题: 如何动态调用dll中的函数(急~在线等待) 作 者: wltj (武林天骄) 等 级: 信 誉 值: 100 所属论坛: .NET技术 C# 问题点数: 20 回复次数: 12 发时间: 2004-3-19 10:27:51 我的意思是事先不知道dll的名字,也就是说不晓得调用哪个dll中的函数。但函数名是统一的 回复人: Ninputer(装配脑袋) ( ) 信誉:85 2004-3-19 10:40:37 得分:0 可以,是.NET的DLL吗, Top 回复人: mfc2003(编程浪子) ( ) 信誉:100 2004-3-19 10:46:51 得分:0 好象不可以吧 如果两个DLL里都有相同的函数,而且 参数个数和类型是一样的~~~~~~~~~~ Top 回复人: fq_ln(冬天) ( ) 信誉:100 2004-3-19 10:55:13 得 分:0 动态链接库在本质上是不同的 1如果你调用用VC编写的动态链接库,不知道里面的函数名是无法使用的,如果他能够提供头文件就可以。 2如果你调用的是Active dll,那么,首先应该注册,然后引用com就可以使用了。 3如果你调用的是用.net编写的dll,那么直接引用就可以了 Top 回复人: wltj(武林天骄) ( ) 信誉:100 2004-3-19 11:17:34 得分:0 我调用的是.net的dll,直接引用好像不行。因为事先不知道dll的文件名。 Top 回复人: wltj(武林天骄) ( ) 信誉:100 2004-3-19 12:06:16 得 分:0 现在的问题变成了,事先不知道dll名称(但是程序中可以得到该名称,不过是储存在变量里面的),如何生成该dll中的一个类的实例呢, Top 回复人: Ninputer(装配脑袋) ( ) 信誉:85 2004-3-19 13:23:44 得分:15 原来是.NET的DLL,完全可以做到~ 你需要借助反射。可以用Assembly读取程序集(就是你的DLL)信息,然后再依次深入,读取类型、方法(如果你知道要调用何方法就更好了)、属性的信息,然后动态创建对象,用反射执行方法,OK 用VB更方便,因为VB封装了一部分反射的代码,尤其是处理后期绑定问题的时候比C#简便得多。 Top 回复人: wltj(武林天骄) ( ) 信誉:100 2004-3-19 13:46:15 得分:0 楼上的你的意思是使用CreateInstance函数吗,好像这个函数的返回类型都是object,但是对象的类型是一个变量啊。我已经写到了 Assembly assm = Assembly.LoadFrom("F:\\dll\\"+dllname+".dll"); Type TypeToLoad=assm.GetType(dllname+".Class1"); 然后接着的不知道改怎么写了。 dllname是dll文件名,Class1是dll文件中的一个类 Top 回复人: dahuzizyd(你就是我心中的女神) ( ) 信誉:105 2004-3-19 19:16:02 得分:0 private void button1_Click(object sender, System.EventArgs e) { Assembly assm = Assembly.LoadFrom("e:\\WindowsApplication5.dll"); Type TypeToLoad= assm.GetType("WindowsApplication5.Form1"); object obj; obj = Activator.CreateInstance(TypeToLoad); Form formToShow = null; formToShow = (Form)obj; formToShow.Show(); } Top 回复人: rroo(天之痕) ( ) 信誉:105 2004-3-19 19:51:51 得分:5 我知道你要什麼,和我聯繫,我給你你要的 janewu@msik.com.cn 我就是用這個Dll來管理大型數據庫系統的開發, 強烈建議CSDN可以上傳文件. Top 回复人: rroo(天之痕) ( ) 信誉:105 2004-3-19 19:56:42 得分:0 /*ISDServer.cs*/ using System; using System.Data; using System.Data.OleDb; //========================================================= ===== //Module : MES //Created : 11/05/2003 //Author : Jane //Email : janewu@msik.com.cn //========================================================= ===== namespace MES { /// /// ISDServer Class include some interfaces /// public class ISDServer { public ISDServer() { } /// /// All Transaction class's parent /// public interface ITransaction { int Execute(ISDServer.IProcess oContext ,string sServiceID,TxType otype,string sParam); } /// /// All Retrieval class's parent /// public interface IRetrieval { DataSet Execute(ISDServer.IProcess oContext,string sServiceID,string sParam); } /// /// Class MyProcess's parent /// public interface IProcess { int TxExecute(string sServiceID,TxType oType,string sParam); DataSet RvExecute(string sServiceID,string sParam); OleDbConnection CreateConnection(); OleDbCommand CreateCommand(); string GetConStr(); void Open(); void Close(); void TxBegin(); void TxCommit(); void TxRollBack(); } } } /*ISDClient.cs*/ using System; using System.Data; //========================================================= ===== //Module : MES //Created : 11/05/2003 //Author : Jane //Email : janewu@msik.com.cn //========================================================= ===== namespace MES { public class ISDClient { /// /// User use this class to call assembly /// private int nSERVER_ID=-1; private int nLevel = 0; private MyProcess p; public ISDClient(int SERVER_ID) { nSERVER_ID=SERVER_ID; p=new MyProcess(nSERVER_ID,nLevel); } #region //*****TxExecute***** /// /// Execute Assembly's Transaction's Operation /// /// Function's location /// Operation's type /// Operation's params /// Operate result public int TxExecute(string sServiceID,TxType oType,string sParam) { //*********************************** return p.TxExecute(sServiceID,oType,sParam); //*********************************** } #endregion #region //*****RxExecute /// /// Execute Assembly's Retrieval's Operation /// /// Function's location /// Operation's params /// Operate result public DataSet RxExecute(string sServiceID,string sParam) { //****************************** MyProcess p=new MyProcess(nSERVER_ID , nLevel); return p.RvExecute(sServiceID,sParam); //****************************** } #endregion } } Top 回复人: rroo(天之痕) ( ) 信誉:105 2004-3-19 19:57:48 得 分:0 /*MyProcess.cs*/ using System; using System.Data; using System.Data.OleDb; using System.Xml; using System.Xml.XPath; using System.Reflection; //========================================================= ===== //Module : MES //Created : 11/05/2003 //Author : Jane //Email : janewu@msik.com.cn //========================================================= ===== namespace MES { public class MyProcess:ISDServer.IProcess { public int nSERVER_ID = -1; public OleDbConnection con; public OleDbCommand cmd; public OleDbTransaction tr; public string strCon = ""; private int Level = -1; public MyProcess(int SERVER_ID,int nLevel) { nSERVER_ID = SERVER_ID; Level = nLevel; } #region //*****CreateConnectionStr***** /// /// Create connection string from configure.xml at directory system32 /// /// Server ID /// Connection string public string CreateConnectionStr(int nID) { try { XmlDocument xml = new XmlDocument(); xml.Load(System.Environment.GetFolderPath(System.Environment.SpecialFolder.System)+"//configure.Xml"); XmlNode n1 = xml.SelectSingleNode("//configure/Server[@ID='"+nID.ToStrin g()+"']"); if(n1 == null) { throw new ISDException(true,"Can not found server in configure file at directory system32"); } string sTemp = n1.SelectSingleNode("Provider").InnerText.Trim(); string sDataSource = n1.SelectSingleNode("ServerName").InnerText.Trim(); string sDBName = n1.SelectSingleNode("DBName").InnerText.Trim(); string sUserID = n1.SelectSingleNode("LoginID").InnerText.Trim(); string sPassword = n1.SelectSingleNode("Password").InnerText.Trim(); switch(sTemp) { case "0": { strCon = "Provider=sqloledb;Data Source=" + sDataSource +";Initial Catalog="+sDBName+";User Id="+sUserID+";Password="+sPassword+";"; break; } case "1": { break; } case "2": { strCon = "Provider=OraOleDB.Oracle.1;"+"Data Source="+sDataSource+";"+"User Id="+sUserID+";"+"Password="+sPassword+";"; break; } } return strCon; } catch(XmlException) { throw new ISDException(true,"Can not load config file"); } catch(XPathException) { throw new ISDException(true,"Can not find node path"); } } #endregion #region //*****GetConStr***** /// /// Get this instance's connection string /// /// Connection string public string GetConStr() { return strCon; } #endregion #region //*****Open***** /// /// Open connection /// public void Open() { if(Level == 0) { con.Open(); } Level ++; } #endregion #region //*****CreateConnection***** /// /// Create OleDbConnection /// /// OleDbConnection public OleDbConnection CreateConnection() { if(Level == 0) { con=new OleDbConnection(CreateConnectionStr(nSERVER_ID)); } return con; } #endregion #region //*****CreateCommand***** /// /// Create OleDbCommand /// /// OleDbCommand public OleDbCommand CreateCommand() { if(Level == 0) { cmd = new OleDbCommand(); cmd.Connection = this.con; } return cmd; } #endregion #region //*****Close***** /// /// Close connection /// public void Close() { if(Level == 1) { con.Close(); } Level --; } #endregion #region //*****TxBegin***** /// /// Begin Transaction /// public void TxBegin() { if(Level == 1) { tr = con.BeginTransaction(); cmd.Transaction = tr; } } #endregion #region //*****TxCommit***** /// /// Commit Transaction /// public void TxCommit() { if(Level == 1) { tr.Commit(); } } #endregion #region //*****TxRollBack***** /// /// Roolback Transaction /// public void TxRollBack() { if(Level == 1) { tr.Rollback(); } } #endregion #region //*****TxExecute***** /// /// Execute Assembly's Transaction's Operation /// /// Function's location /// Operation's type /// Operation's params /// Operate result public int TxExecute(string sServiceID,TxType oType,string sParam) { try { string AssemblyName = sServiceID.Substring(0,sServiceID.IndexOf(".")); Assembly MyAssembly = Assembly.Load(AssemblyName); ISDServer.ITransaction Tx = (ISDServer.ITransaction)(MyAssembly.CreateInstance(Assembly Name+".Transaction")); return Tx.Execute((ISDServer.IProcess)this,sServiceID,oType,sParam ); } catch(Exception Ex) { throw new ISDException(Ex.Message); } } #endregion #region //*****RxExecute****** /// /// Execute Assembly's Retrieval's Operation /// /// Function's location /// Operation's params /// Operate result public DataSet RvExecute(string sServiceID,string sParam) { try { string AssemblyName = sServiceID.Substring(0,sServiceID.IndexOf(".")); Assembly MyAssembly = Assembly.Load(AssemblyName); ISDServer.IRetrieval Rv = (ISDServer.IRetrieval)(MyAssembly.CreateInstance(AssemblyNa me+".Retrieval")); return Rv.Execute((ISDServer.IProcess)this,sServiceID,sParam); } catch(Exception Ex) { throw new ISDException(Ex.Message); } //******************************* } #endregion } } /*MES.cs*/ using System; using System.Diagnostics; //========================================================= ===== //Module : MES //Created : 11/05/2003 //Author : Jane //Email : janewu@msik.com.cn //========================================================= ===== namespace MES { /// /// Transaction Type /// public enum TxType { Insert, Change, Delete, Update } /// /// MES Exception Class /// public class ISDException : System.ApplicationException { public ISDException(string message) : base (message) { } public ISDException(string ServiceID , string message) : base (message) { } public ISDException(int nFlag , string ServiceID , string message) : base (message) { } public ISDException(bool bLog , string message) : base (message) { if(bLog) { if(!EventLog.SourceExists("MES")) { EventLog.CreateEventSource("MES", "Application"); } EventLog myLog = new EventLog(); myLog.Source = "MES"; myLog.WriteEntry(message + " at " + DateTime.Now); } } public ISDException(bool bLog , string EventLogName , string message) :base (message) { if(bLog) { if(!EventLog.SourceExists(EventLogName)) { EventLog.CreateEventSource(EventLogName, EventLogName); } EventLog myLog = new EventLog(); myLog.Source = "MES"; myLog.WriteEntry(message+ " at " + DateTime.Now); } } public ISDException(bool bLog , string ServiceID ,string EventLogName , string message) :base (message) { if(bLog) { if(!EventLog.SourceExists(EventLogName)) { EventLog.CreateEventSource(EventLogName, EventLogName); } EventLog myLog = new EventLog(); myLog.Source = "MES"; myLog.WriteEntry( ServiceID + "\n" + message+ "\n" + DateTime.Now); } } } } Top 回复人: wltj(武林天骄) ( ) 信誉:100 2004-3-20 15:13:37 得 分:0 这个问题我已经找到解决的方法了。我的代码如下: ssembly assm = Assembly.LoadFrom(@"F:\dll\"+dllname+".dll"); Type TypeToLoad=assm.GetType(dllname+".Class1"); MethodInfo method = TypeToLoad.GetMethod("GetProductParameter"); object obj; obj=Activator.CreateInstance(TypeToLoad); cpcs=(DataTable)method.Invoke(obj,null); 实现的是调用dll文件中的Class1里面的GetProductParameter方法,返回类型是DataTable. 感谢大家的热心帮忙~
/
本文档为【[生活]C 如何动态调用DLL中的函数】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索