九美子官网:C Sharp与.net学习笔记(二)
来源:百度文库 编辑:偶看新闻 时间:2024/04/25 19:38:45
看看动态库创建与使用相关的东西
生成与使用(托管的)dll
dll.cs ==> dll.dll
// file: dll.cspublic class Calc{ public static int Add(int a, int b) { return a + b; }}
main.cs ==> main.exe
// file: main.csusing System;class App{ public static void Main() { Console.WriteLine("{0} + {1} = {2}", 123, 456, Calc.Add(123, 456)); }}
编译:
E:\> csc /target:library dll.csE:\> csc /reference:dll.dll main.cs
运行:
E:\> main123 + 456 = 579
动态加载dll
using System;using System.Reflection;class App{ public static void Main() { Assembly dll = Assembly.Load("dll"); Type calcType = dll.GetType("Calc"); object[] parameters = new object[]{123, 456}; object res = calcType.InvokeMember("Add2", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, parameters); Console.WriteLine("{0} + {1} = {2}", 123, 456, (int)res); }}
- 首先,加载我们动态库dll.dll,使用的Assembly.Load,也可以用(相对或绝对路径)
Assembly.LoadFrom("dll.dll");
- 然后,获取需要的类型,存于calcType
最后通过InvokeMember调用该类型中的成员函数。如果调用的不是static成员,还需要创建该类型的实例
Object obj = Activator.CreateInstance(calcType); object res = calcType.InvokeMember("Add", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, parameters);
另一种写法:
MethodInfo addMethod = calcType.GetMethod("Add"); object[] parameters = new object[]{123, 456}; object obj = Activator.CreateInstance(calcType); object res = addMethod.Invoke(obj, parameters);
使用非托管 dll
dll2.cpp ==> dll2.dll
// file: dll2.cppextern "C" int __declspec(dllexport) Add(int a, int b){ return a + b;}
main.cs ==> main.exe
// file: main.csusing System;using System.Runtime.InteropServices;class App{ [DllImport("dll2.dll")] public static extern int Add(int a, int b); public static void Main() { Console.WriteLine("{0} + {1} = {2}", 123, 456, Add(123, 456)); }}
编译运行:
E:\> cl dll2.cpp /LDE:\> csc main.csE:\> main123 + 456 = 579
这个东西被称为Platform Invoke(P/Invoke). DllImport还有一些属性
EntryPoint
CharSet
CallingConvention
SetLastError
[DllImport("dll2.dll", EntryPoint="Add", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Winapi, SetLastError=true)]
动态加载dll
这又需要回到Win32 Api函数LoadLibrary、FreeLibrary这些东西了
// file: main.csusing System;using System.Runtime.InteropServices;class App{ [DllImport("kernel32.dll")] public static extern IntPtr LoadLibrary(string dllFileName); [DllImport("kernel32.dll")] public static extern IntPtr GetProcAddress(IntPtr dllHandle, string functionName); [DllImport("kernel32.dll")] public static extern bool FreeLibrary(IntPtr dllHandle); private delegate int AddDelegate(int a, int b); public static void Main() { IntPtr handle = LoadLibrary("dll2.dll"); IntPtr pAddFunc = GetProcAddress(handle, "Add"); AddDelegate Add = (AddDelegate)Marshal.GetDelegateForFunctionPointer( pAddFunc, typeof(AddDelegate)); Console.WriteLine("{0} + {1} = {2}", 123, 456, Add(123, 456)); FreeLibrary(handle); }}
先用P/Invoke把这3个函数弄进来,然后用它们去处理我们要动态加载的动态库。
直接编译
E:\> csc main.cs
C#.net 事件与委托
.net技术:C#.NET与VB.NET区别
SHARP笔记电脑声卡哪里有卖?
c#.net与visual c#.net有什么区别?
怎样快速学习asp.net (c#)
如何看MSDN学习c#.net啊
谁知道C语言的学习笔记,和C语言的学习软件
请问学习.NET与J2EE那个更好?
什么是c#.net与java那个好??
C++.NET与6.0的区别?
什么是c sharp?用途是什么?
新概念二册笔记
Sharp 赛扬M500 CPU 笔记本该装什么系统好
请问下c#+asp.net相关的学习网站
在绵阳学习 JAVA,C#,.NET,WinForm,sql
没有一点编程基础学习Visual C++.NET 如何开始
有谁能讲讲学习ASP.NET(C#)的一些经验呢
ASP.net(c#)与asp.net(vb.net)的区别是什么?哪个更好一些?
如何做学习笔记
司法鉴定学习笔记范文
学习型班组学习笔记
数据结构C语言版笔记
c#.net??
vb.net的学习与vb6.0关系大吗?