C# 动态调用DLL
2006-04-21 17:29
615 查看
本来是想实现控制台程序运行时自动全屏,但是只找到VC下的实现方法(http://www.vckbase.com/bbs/prime/viewprime.asp?id=347)。
其中要使用两个未公开的Win32 API函数来存取控制台窗口,这就需要使用动态调用的方法,动态调用中使用的Windows API函数主要有三个,即:Loadlibrary,GetProcAddress和Freelibrary。步骤如下:
1. Loadlibrary: 装载指定DLL动态库
2. GetProcAddress:获得函数的入口地址
3. Freelibrary: 从内存中卸载动态库
但是C#中是没有函数指针,无法直接使用GetProcAddress返回的入口地址。后来找到资料,其实.NET 2.0新增了Marshal.GetDelegateForFunctionPointer 方法可以满足这个要求,MSDN里的解释是:将非托管函数指针转换为委托。
后面的事就简单啦,我把它编成了一个类来方便调用。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace feiyun0112.cnblogs.com
下面是使用的例子:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using feiyun0112.cnblogs.com;
namespace ConsoleApplication1
其中要使用两个未公开的Win32 API函数来存取控制台窗口,这就需要使用动态调用的方法,动态调用中使用的Windows API函数主要有三个,即:Loadlibrary,GetProcAddress和Freelibrary。步骤如下:
1. Loadlibrary: 装载指定DLL动态库
2. GetProcAddress:获得函数的入口地址
3. Freelibrary: 从内存中卸载动态库
但是C#中是没有函数指针,无法直接使用GetProcAddress返回的入口地址。后来找到资料,其实.NET 2.0新增了Marshal.GetDelegateForFunctionPointer 方法可以满足这个要求,MSDN里的解释是:将非托管函数指针转换为委托。
后面的事就简单啦,我把它编成了一个类来方便调用。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace feiyun0112.cnblogs.com
下面是使用的例子:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using feiyun0112.cnblogs.com;
namespace ConsoleApplication1
相关文章推荐
- C#,动态加载DLL,通过反射,调用参数,方法,窗体
- C#中动态调用DLL动态链接库
- C#动态调用c++DLL的方法
- 利用C#的反射机制动态调用DLL类库 (2009-11-21 18:41:43)
- C#程序实现动态调用DLL的研究
- C#动态地调用Win32 DLL中导出的函数
- C#利用反射动态加载DLL(C#)并调用其中的接口
- C#程序实现动态调用DLL的研究
- C#程序实现动态调用DLL的研究
- C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用
- C# 动态调用DLL
- C#程序实现动态调用DLL的研究
- C#程序实现动态调用DLL的研究
- C#程序实现动态调用DLL的研究[转]
- C#程序实现动态调用DLL的研究
- 笔记备忘: 在 C# 中进行 win32 dll 的动态加载, 调用, 和释放
- C#程序实现动态调用DLL的研究(2)
- C#程序实现动态调用DLL的研究
- C#动态调用标准DLL和vb(非标准)DLL
- C#动态调用DLL中的函数