Delphi中编写无输出函数名的DLL文件(有点意思)(400多篇博客)
用 Delphi 用长了,总是发现,有些和 MS 不同的地方。例如,MS 的公开库中,常常隐藏了许多重要函数,这些函数在系统中常常有起着非常巨大的作用。一旦知道如何调用,可以给自己的应用程序提供很强的功能和很大的灵活性。但,这些函数通常又没有函数名(即使用 ExeScope 查看 DLL 文件的导出表也看不出函数意义),仅仅只有一个序号来表示。有时候我又自己想,为什么我在写程序的时候不能学学 MS 隐藏一些自己不希望公开的函数呢?
其实用 Delphi 写 DLL 的时候,使用简单的技巧就可以实现隐藏函数名的效果。让我们来看看下面这个 DLL 源码:
library proDll;
uses
Windows;
{$R *.res}
procedure ShowMessageA(hWnd: HWND); stdcall ;
begin
MessageBox(hWnd, '您调用的是 ShowMessageA 函数', 'DLL 函数信息',
MB_ICONINFORMATION);
end ;
procedure ShowMessageB(hWnd: HWND); stdcall ;
begin
MessageBox(hWnd, '您调用的是 ShowMessageB 函数', 'DLL 函数信息',
MB_ICONINFORMATION);
end ;
exports
ShowMessageA index 1 name '',
ShowMessageB index 2 name '';
begin
end .
注意看 exports 部分,用 index 关键字指定输出函数的序号,后面紧跟一个 name 关键字指明输出函数名称。关键就在这里,name 后面是一个空字符串,这样就给函数生成了一个空字符串名。实际效果既是隐藏了输出函数的名称。是不是很容易呢?
那么我们怎样调用这样的输出函数呢?由于没有了函数名,我们调用起来会显得和以前不一样。其实也不用担心,调用同样非常简单。我下面就静态调用和动态调用制作了两个工程,源码如下:
静态调用例子:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class (TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end ;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure ShowMessageA(hWnd: HWND); stdcall ; external 'proDll.dll' index 1;
procedure ShowMessageB(hWnd: HWND); stdcall ; external 'proDll.dll' index 2;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessageA(Handle);
end ;
procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessageB(Handle);
end ;
end .
动态调用的例子:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class (TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end ;
var
Form2: TForm2;
implementation
{$R *.dfm}
type
TDllShowMessageFunc = procedure (hWnd: HWND); stdcall ;
var
hDllHandle: THandle;
ShowMessageA, ShowMessageB: TDllShowMessageFunc;
procedure LoadFuncDll;
begin
if hDllHandle = 0 then
begin
hDllHandle := LoadLibrary('proDll.dll');
if hDllHandle = 0 then
raise Exception.Create('proDll.dll 加载失败');
try
{
lpProcName: the second argument of function GetProcAddress
Points to a null-terminated string containing the function name,
or specifies the function's ordinal value. If this parameter is
an ordinal value, it must be in the low-order word; the high-order
word must be zero.
}
@ShowMessageA := GetProcAddress(hDllHandle, Pointer(HiWord(0) or LoWord(1)));
if @ShowMessageA = nil then
raise Exception.Create('proDll.dll 中没有输出 ShowMessageA 函数');
@ShowMessageB := GetProcAddress(hDllHandle, Pointer(HiWord(0) or LoWord(2)));
if @ShowMessageB = nil then
raise Exception.Create('proDll.dll 中没有输出 ShowMessageB 函数');
except
FreeLibrary(hDllHandle);
hDllHandle := 0;
raise ;
end ;
end ;
end ;
procedure FreeFuncDll;
begin
if hDllHandle <> 0 then
begin
FreeLibrary(hDllHandle);
hDllHandle := 0;
@ShowMessageA := nil ;
@ShowMessageB := nil ;
end ;
end ;
procedure TForm2.Button1Click(Sender: TObject);
begin
if @ShowMessageA = nil then LoadFuncDll;
ShowMessageA(Handle);
end ;
procedure TForm2.Button2Click(Sender: TObject);
begin
if @ShowMessageB = nil then LoadFuncDll;
ShowMessageB(Handle);
end ;
initialization
// do nothing
finalization
FreeFuncDll;
end .
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/aroc_lo/archive/2010/07/27/5769801.aspx
Delphi中编写无输出函数名的DLL文件(有点意思)(400多篇博客)的更多相关文章
- Javascript Jquery 中的数组定义与操作_子木玲_新浪博客
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- C++中实现对map按照value值进行排序 - 菜鸟变身记 - 51CTO技术博客
C++中实现对map按照value值进行排序 - 菜鸟变身记 - 51CTO技术博客 C++中实现对map按照value值进行排序 2012-03-15 15:32:36 标签:map 职场 休闲 排 ...
- 撰写一篇博客要求讲述四则运算2的设计思想,源程序代码、运行结果截图、编程总结分析,并按照PSP0级的要求记录开发过程中的时间记录日志。
一.撰写一篇博客要求讲述四则运算2的设计思想,源程序代码.运行结果截图.编程总结分析,并按照PSP0级的要求记录开发过程中的时间记录日志. 1.设计思想: ①创建test.jsp建立第一个前端界面,提 ...
- 修复在“Android 在ScrollView中嵌入ViewPage后ViewPage不能很好的工作的问题解决”这篇博客中MyScrollView出现滑动一会就不会上下滑动的问题
在“Android 在ScrollView中嵌入ViewPage后ViewPage不能很好的工作的问题解决”,这篇博客中的大部分问题已经解决了. 唯一遗憾的是,ViewPage随人能够工作了,但是My ...
- 鸿蒙内核源码分析(编译脚本篇) | 如何防编译环境中的牛皮癣 | 百篇博客分析OpenHarmony源码 | v58.01
百篇博客系列篇.本篇为: v58.xx 鸿蒙内核源码分析(环境脚本篇) | 编译鸿蒙原来如此简单 | 51.c.h.o 本篇用两个脚本完成鸿蒙(L1)的编译环境安装/源码下载/编译过程,让编译,调试鸿 ...
- 在Delphi中编写res文件
delphiimagedosinterfaceborland脚本先用记事本编写一个rc的文件. 如内容为: _Comms RCData Comms.jpg Comms.jpg为图片名称, 然后在这个r ...
- Golang调用windows下的dll动态库中的函数 Golang 编译成 DLL 文件
Golang调用windows下的dll动态库中的函数 package main import ( "fmt" "syscall" "time&quo ...
- .net 中使用配置文件需注意引用dll文件
需要用到sqlhelper和配置文件时发现加了using System.Configuration;还是不能用ConfigurationManager. 查了几遍msdn未果,直接百度才发现还需要引用 ...
- Delphi中拖动无边框窗口的5种方法
1.MouseMove事件中加入: // ReleaseCapture;// Perform(WM_SYSCOMMAND, $F017 , 0); 2.MouseDown事件中加入: // POSTM ...
随机推荐
- Ubuntu一些配置和技巧
安装google-chrome wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb sudo d ...
- 简单的方式实现javascript 小数取整
JS: function truncateNumber(n){ return n|0; } 測试: console.log(truncateNumber(12.345)); 浏览器打印出12
- C# - MemoryStream
代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...
- ean128与code128 条形码 算法分析
[code128条形码组成] 除终止符(STOP)由13个模块组成外,其他字符均由11个模块组成 就是说,如果用‘1’表示黑线(实模块),用‘0’表示白线(空模块),那么每表示一个字符就需要11条线, ...
- Net MVC轻量级分页控件
JPager.Net MVC超好用轻量级分页控件 JPager.Net MVC好用的轻量级分页控件,好用到你无法想象,轻量到你无法想象. JPager.Net MVC好用的轻量级分页控件,实现 ...
- ASP.NET 4.5 Bundle组件(捆绑、缩小静态文件)
阅读目录: 1.开篇介绍 2.System.Web.Optimization 组件 3.System.Web.Optimization 组件基本原理 4.扩展自定义类型静态文件 1]开篇介绍 这篇文章 ...
- [WCF]WCF起航
解决方案概览: Client:windows 控制台应用程序. WcfService1: windows 服务应用程序. WCFWebTest:asp.net 空web应用程序. 变量程序命名.结构可 ...
- 14.18 InnoDB Backup and Recovery 备份和恢复:
14.18 InnoDB Backup and Recovery 备份和恢复: 安全数据库管理的关键是 做定期的备份,依赖你的数据卷, MySQL server的数量和数据库的负载,你可以使用那些技术 ...
- 基于visual Studio2013解决C语言竞赛题之1085相邻之和素数
题目 解决代码及点评 /************************************************************************/ /* ...
- html5之拖放简单效果
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title> ...