C# 动态调用DLL
来源:互联网 发布:区域增长算法改进 编辑:程序博客网 时间:2024/05/21 01:48
本来是想实现控制台程序运行时自动全屏,但是只找到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
{
public class DllInvoke
{
Win API
private IntPtr hLib;
public DllInvoke(String DLLPath)
{
hLib = LoadLibrary(DLLPath);
}
~DllInvoke()
{
FreeLibrary(hLib);
}
//将要执行的函数转换为委托
public Delegate Invoke (string APIName,Type t)
{
IntPtr api = GetProcAddress(hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace feiyun0112.cnblogs.com
{
public class DllInvoke
{
Win API
private IntPtr hLib;
public DllInvoke(String DLLPath)
{
hLib = LoadLibrary(DLLPath);
}
~DllInvoke()
{
FreeLibrary(hLib);
}
//将要执行的函数转换为委托
public Delegate Invoke (string APIName,Type t)
{
IntPtr api = GetProcAddress(hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
}
}
}
下面是使用的例子:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using feiyun0112.cnblogs.com;
namespace ConsoleApplication1
{
class Program
{
Win API
public delegate bool SetConsoleDisplayMode(IntPtr hOut, int dwNewMode, out int lpdwOldMode);
static void Main(string[] args)
{
DllInvoke dll = new DllInvoke("kernel32.dll");
int dwOldMode;
//标准输出句柄
IntPtr hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//调用Win API,设置屏幕最大化
SetConsoleDisplayMode s = (SetConsoleDisplayMode)dll.Invoke("SetConsoleDisplayMode", typeof(SetConsoleDisplayMode));
s(hOut, 1, out dwOldMode);
Console.WriteLine("********************Full Screen Mode********************");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using feiyun0112.cnblogs.com;
namespace ConsoleApplication1
{
class Program
{
Win API
public delegate bool SetConsoleDisplayMode(IntPtr hOut, int dwNewMode, out int lpdwOldMode);
static void Main(string[] args)
{
DllInvoke dll = new DllInvoke("kernel32.dll");
int dwOldMode;
//标准输出句柄
IntPtr hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//调用Win API,设置屏幕最大化
SetConsoleDisplayMode s = (SetConsoleDisplayMode)dll.Invoke("SetConsoleDisplayMode", typeof(SetConsoleDisplayMode));
s(hOut, 1, out dwOldMode);
Console.WriteLine("********************Full Screen Mode********************");
Console.ReadLine();
}
}
}
转自:http://www.cnblogs.com/lizi/archive/2012/02/22/2363090.html
- C# 动态调用DLL
- C# 动态调用DLL
- C# DLL动态调用
- C#动态调用C++DLL
- C++调用C#动态dll
- C# 动态调用DLL库
- C#静态调用C++ dll,动态调用C++ dll和C++调用C# dll
- C#调用C++动态链接库(dll)
- C#动态调用非托管DLL(一)
- C#动态调用非托管DLL(二)
- C#程序实现动态调用DLL
- C#动态调用非托管DLL
- C#动态调用c++DLL的方法
- C#实现动态调用Windows DLL
- C#动态调用C++的Dll
- C#调用C++动态库(dll)
- C#动态调用c++DLL的方法
- C#调用C++动态库dll
- vs2008+Qt 编译MySQL驱动
- 查看所有的端口
- 学习SDH技术十三个必看要点
- 在c#中调用并调试C++的DLL
- 逆时针输出回形矩阵图(Java版本)
- C# 动态调用DLL
- MFC对话框下面加上时间状态栏
- silverlight post
- ORA-06502: PL/SQL: 数字或值错误 : 字符串缓冲区太小 错误分析
- Spring JDBC编程式事务+详细实例总结
- Xvfb+YSlow+ShowSlow搭建前端性能测试框架
- jfreechart柱状图
- JSP中的EL表达式详细介绍
- Unity3d 简单显示汉字