日期:2012-11-06  浏览次数:20343 次

VB中定制DllRegisterServer、DllUnregisterServer

-阿鬼(heroyin)

VB作为一种简单容易上手的语言,可以让开发者快速上手,开发速度快,效率高。但它过分的封装也给开发者带来诸多不便。

问题的由来
最近本人在开发一个插件结构的项目中就遇到了一个麻烦,我的项目是采用COM架构,框架由DELPHI开发,插件为COM组件,插件可以由其他语言开发,当然也包括VB。每个插件必须注册为一个固定的组件类别(Categories)。在其他语言如VC、DELPHI中实现起来非常简单,只需要重载DllRegisterServer、DllUnregisterServer在两个函数里加入注册注销类别的代码,然后输出就可以了。但到了VB就碰到麻烦了。

VB正常情况下是不能直接开发标准DLL,所以也就不支持自定义导出函数,开发ACTIVE DLL的时候,是由VB自动导出必须的四个函数DllGetClassObject, DllCanUnloadNow, DllRegisterServer, DllUnregisterServer。要加入注册注销类别代码就必须重新导出DllRegisterServer, DllUnregisterServer。

好了,问题就转换成了如何在VB写的DLL中导出函数了

VB编译内幕
VB的编译过程大致如下,当我们在编辑环境中编写完代码后,VB调用C2将所有的模块(包括CLASS)编译成OBJ文件(能够为机器语言识别的代码)。一下是C2的一些编译参数说明(E文):

- the the name of the prefixed one used for the names of the rowscontaining ' precompilato', one

risen of intermediate tails (from which name of the switch) temporary; these rows are 5 and finish withi suffissi GL, SY, FORMER, IN and DB; they contained are not documented

- f the name of the rows to compile

- W3 warning level 3, level of ' attenzione' dedicating to i warnings

- Gy it qualifies the connection to level of function (function-level linking)

- G5 optimization for the Pentium

- Gs4096 it allows not to insert the code for the control of stack (stack probe) if a function does not

use more than 4096 byte of stack

- dos not documented

- Z1 it removes the name of the bookcase of default from the rows.OBJ

- Fofileobj the name of rows OBJ to generate (rows output)

- Qifdiv it puts in action the corrections for the bug of the division of the Pentium (FDIV bug)

- MILILITER it creates rows eseguibile single-threaded

- basic it indicates the compiler C2 the fact that the compilation it happens for a plan basic



C2完成编译后,VB会调用LINK.EXE将所有的OBJ文件连接成EXE文件,完成编译过程。下面是一段命令行演示如何调用LINK.EXE:

LINK C:\Test\Form1.obj C:\Test\Modulo1.obj C:\Test\Progetto1.obj C:\Programmi\Microsoft Visual Studio\VB98\VBAEXE6.lib /ENTRY:__ vbaS /OUT:C:\Test\Progetto1.exe /BASE:0x400000 /SUBSYSTEM:WINDOWS, 4.0 /VERSION:1.0 /DEBUG /DEBUGTYPE:CV /INCREMENTAL:NOT /OPT:REF/MERGE:.rdata =.text /IGNORE:4078
对于我们来说这些参数没有什么意义,用默认的就行了。这段命令行中并没有包括输出函数,如果我们希望输出函数,可以定义一个.def文件,按照格式加入要输出的函数列表,然后在命令行后面加上 “/DEF: 文件名”(当然也可以直接加/ EXPORTS参数),再调用命令行编译,用的denpendency工具查看你就会发现你要输出的函数了。

Def文件格式的定义:



LIBRARY 程序名称

DESCRIPTION "MyDLL - (C) Antonio Giuliana, 2004"

EXPORTS

函数名= ?函数名@函数所在模块名@@AAGXXZ



例:

LIBRARY MyDLL

DESCRIPTION "MyDLL - (C) Antonio Giuliana, 2004"

EXPORTS

DllRegisterServer= ?DllRegisterServer@SymExp@@AAGXXZ

DllUnregisterServer= ?DllUnregisterServer@SymExp@@AAGXXZ



注意:函数名和模块名是区分大小写的



找到了解决方案了,但是,由于VB编译完成后就会自动删除OBJ文件,如何再C2生成完OBJ文件后中断编译过程取得OBJ文件呢,网上有种方法就是替换调VB的LINK文件,然后中断一下,将OBJ文件拷贝出来,在用命令行编译。这是个很好的办法,但不够智能化,本人在国外网站上发现了一个更为有效的方法。

制作VB的辅助编译工具
新建一个工程,命名为LINK,在MAIN过程中加入代码:

Public Sub Main()
Dim cmd As String
Dim fOut As Long
Dim sOut As String
Dim sDef As String

cmd = Command$
If InStr(cmd, "/DLL") > 0 And InStr(cmd, "VBAEXE6.LIB") > 0 Then
fOut = InStr(cmd, "/OUT:")
sOut = Mid(cmd, fOut + 6, InStr(cmd, "/BASE:") - fOut - 8)
sDef = Left(sOut, Len(sOut) - 3) + "def"
If Len(Dir(sDef)) Then
cmd = cmd & "/DEF:"" "& sDef &" "" "
End If
End If