明晨网络

电话: 136-6532-7492 QQ: 给我发送消息 8507-0741

VB自动注册dll

明晨网络原创,2009-06-12 18:09, 文章标签: VB DLL

    VB写出来的Activex DLL,发布到客户端后,必须注册才能使用,一般使用批处理来实现,比如 regsvr32  d:\project\mydll.dll,实际上我们可以在VB程序内部来自动注册这种Activex DLL。具体代码在下面给出来。
    注册DLL,RegDll "dll的绝对路径",True;反注册DLL ,RegDll "dll的绝对路径",False
    明晨网络Mingchennet.com整理于2007-3-2,发布于2009-6-12
   

代码如下
  1. Option Explicit
  2.    
  3.    '========================================
  4.    '声明作用:注册dll
  5.    '========================================
  6. Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
  7. Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
  8. Private Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByVal lParameter As Long, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
  9. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  10. Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
  11. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  12.  
  13. '========================================
  14. '函数名称:RegDll
  15. '函数作用:注册dll
  16. '========================================
  17. Function RegDll(DllPath As String, Action As Boolean) As Boolean
  18.     On Error GoTo ErrHandle:
  19.     'Dll载入句柄
  20.    Dim lLib As Long
  21.     'Dll注册、反注册入口点
  22.    Dim lpDLLEntryPoint As Long
  23.     '线程句柄
  24.    Dim lpThreadID As Long
  25.     '线程创建句柄
  26.    Dim mThread As Long
  27.     Dim mresult As Long
  28.    
  29.     lLib = LoadLibrary(DllPath)
  30.    
  31.     If Action Then
  32.         lpDLLEntryPoint = GetProcAddress(lLib, "DllRegisterServer")
  33.     Else
  34.         lpDLLEntryPoint = GetProcAddress(lLib, "DllUnregisterServer")
  35.     End If
  36.    
  37.     mThread = CreateThread(ByVal 0, 0, ByVal lpDLLEntryPoint, ByVal 0, 0, lpThreadID)
  38.     mresult = WaitForSingleObject(mThread, 10000)
  39.     CloseHandle mThread
  40.    
  41.     FreeLibrary lLib
  42.     RegDll = True
  43.    
  44.     Exit Function
  45. ErrHandle:
  46. End Function

文章源自:明晨网络,明晨网络原创,《VB自动注册dll》,http://www.mingchennet.com/tec/code/vb/21.htm