VB-控件注册 - 利用资源文件将dll、ocx打包进exe文件

很多时候自定义或者引用控件都需要注册才能使用,但是如何使要注册的dll或ocx打包到exe中,使用户下载以后看到的只是一个exe,点击直接运行呢?就像很多安全控件,如支付宝的aliedit.exe那样。前面已经发布了C#版控件注册的例子。

 

    现在介绍一下使用资源文件,将dll、ocx打包进exe,点击直接注册的例子: 

 

    首先,新建一个工程,在“外接程序”->“外接程序管理器”->“VB 6 资源管理器”   添加资源文件,可以讲图标、image等文件添加进去,这里我们添加的是ocx。添加后保存会生成一个.reg文件,就是我们的资源文件。

    接下来,新建一个模块,注册ocx的函数,代码如下:

[vb]  view plain copy
  1. '新建一个模块  
  2. Option Explicit  
  3.   
  4. Private Declare Function GetSystemDirectory Lib "kernel32" Alias _  
  5. "GetSystemDirectoryA" (ByVal lpBuffer As String, _  
  6. ByVal nSize As LongAs Long  
  7.   
  8.   
  9.   
  10. Public Sub RegsvrFile(ByVal ResourceID As Integer, strFormat As String, strFileName As String)  
  11.     Dim bytArr() As Byte  
  12.     Dim strSystemPath As String  
  13.     Dim Filenum As Long  
  14.       
  15.     'on error resume next  
  16.     strSystemPath = String(255, 0)  
  17.     GetSystemDirectory strSystemPath, 256  
  18.     strSystemPath = Left(strSystemPath, InStr(1, strSystemPath, Chr(0)) - 1)  
  19.       
  20.     bytArr = LoadResData(ResourceID, strFormat)  
  21.       
  22.     Filenum = FreeFile  
  23.     Open strSystemPath & "/" & strFileName For Binary As #Filenum  
  24.     Put #1, , bytArr  
  25.     Close #Filenum  
  26.       
  27.     Call Shell("regsvr32  " + strFileName, vbHide)  
  28. End Sub  

    最后,在主窗体中调用:

[vb]  view plain copy
  1. Option Explicit  
  2.   
  3. Private Sub Form_Load()  
  4.     RegsvrFile 101, "CUSTOM""dsoframer.ocx"  
  5.     RegsvrFile 102, "CUSTOM""dsoframerctl.ocx"  
  6.       
  7.     Unload Me  
  8. End Sub  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值