看看动态库创建与使用相关的东西
生成与使用(托管的)dll
// file: dll.cs
public class Calc
{
public static int Add(int a, int b)
{
return a + b;
}
}
// file: main.cs
using System;
class App
{
public static void Main()
{
Console.WriteLine("{0} + {1} = {2}", 123, 456, Calc.Add(123, 456));
}
}
编译:
E:\> csc /target:library dll.cs
E:\> csc /reference:dll.dll main.cs
运行:
E:\> main
123 + 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");
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
// file: dll2.cpp
extern "C" int __declspec(dllexport) Add(int a, int b)
{
return a + b;
}
// file: main.cs
using 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 /LD
E:\> csc main.cs
E:\> main
123 + 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.cs
using 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
分享到:
相关推荐
ASP.Net学习之常用模块整理 .NET C sharp
Some commonly asked interview questions regarding C# and .NET.
cefsharp62 修改版,支持framework4.0.有问题加 微信: hdawin ,请备注
Programming With C Sharp .NET.pdf Programming With C Sharp .NET.pdf
VB.net用于安装CefSharp.WinForms的工具NuGet。大多数人认为CefSharp.WinForms只能被用于C#,实际上VB.net也可以用,而且代码比C#更精简。先上传安装CefSharp.WinForms库必须要用到的NuGet,实例和教程正在编写中。
opencvsharp_ .NET Framework wrapper for OpenCV.pdf
相关说明太少了,做了个vb.net版的例子给大家,希望大家喜欢,其实方法也蛮简单的, 第1步,修改app.config文件。 ;"/> 第2步,在setting里面指定如下三个路径就行了 setting....
VB.net中应用CefSharp.WinForms库构建Chrome内核浏览器实例源代码,环境为Windows 10,Visual Studio 2015,.Net FrameWorks 4.52。未包含CefSharp.WinForms的Packages文件夹以及bin下面的支持文件,请自行安装调试...
visual basic.net和visuak c sharp.net面向对象程序设计
PROGRAMMING THE MS .NET FRAMEWORK WITH MS VISUAL C SHARP .NET DELIVERY GUIDE.pdf
asp.net 基础教程-c sharp案例版asp.net 基础教程-c sharp案例版asp.net 基础教程-c sharp案例版
VS2005 ASP.NET本地化学习笔记&感受 在自定义Server Control中捆绑JS文件 Step by Step 深度解析Asp.Net2.0中的Callback机制 使用 Web 标准生成 ASP.NET 2.0 Web 站点 ASP.NET 2.0基于SQLSERVER 2005的aspnetdb.mdf...
2.5 使用SharpDevelop构建.NET应用程序 37 2.6 使用Visual C# 2008 Express构建.NET应用程序 38 2.7 使用Visual Studio 2008构建.NET应用程序 40 2.8 其他.NET开发工具 49 2.9 小结 50 第二部分 C#核心...
资源名:C#使用S7.Net,Sharp7连接读写PLC实例源码 资源类型:程序源代码 源码说明: 里面有S7.Net.dll以及使用说明英文版的,不用区网上下载了。实现C#连接PLC1200,并读写DB块数据。项目工程师完整的,可以直接...
里面有S7.Net.dll以及使用说明英文版的,不用区网上下载了。实现C#连接PLC1200,并读写DB块数据。项目工程师完整的,可以直接学习使用。 更新:内部添加sharp7连接PLC增加读写bool变量,增加读PLC string类型,...
VB.Net to C Sharp Converter 是一款将vb.net代码转化为c#代码的程序。在转化过程中,保留了原有的注释和变量名。 VB.Net to C# Converter是一款代码转换软件,将用户的VB.Net项目转换为C#语言。与别的转换软件不同...
Programming With C Sharp .NET (VBL)
c #.net 开 发 者 手册c #.net 开 发 者 手册
C-Sharp and Asp.Net Interview Questions 主要解决c#与asp.net 注意要点
Visual CSharp .NET Programming Visual CSharp .NET Programming Visual CSharp .NET Programming