关于VB生成标准DLL文件

工程NewLinker.vbp

模块文件NewLinker.bas

Option Explicit

Public Sub Main()
   Dim SpecialLink As Boolean, fCPL As Boolean, fResource As Boolean
   Dim intPos As Integer
   Dim strCmd As String
   Dim strPath As String
   Dim strFileContents As String
   Dim strDefFile As String, strResFile As String
   Dim oFS As New Scripting.FileSystemObject
   Dim fld As Folder
   Dim fil As File
   Dim ts As TextStream, tsDef As TextStream

   strCmd = Command
  
   Set ts = oFS.CreateTextFile(App.Path & "/lnklog.txt")
  
   ts.WriteLine "Beginning execution at " & Date & " " & Time()
   ts.WriteBlankLines 1
   ts.WriteLine "Command line arguments to LINK call:"
   ts.WriteBlankLines 1
   ts.WriteLine "   " & strCmd
   ts.WriteBlankLines 2
  
   ' Determine if .DEF file exists
   '
   ' Extract path from first .obj argument
   intPos = InStr(1, strCmd, ".OBJ", vbTextCompare)
   strPath = Mid(strCmd, 2, intPos + 2)
   intPos = InStrRev(strPath, "/")
   strPath = Left(strPath, intPos - 1)
   ' Open folder
   Set fld = oFS.GetFolder(strPath)
  
   ' Get files in folder
   For Each fil In fld.Files
      If UCase(oFS.GetExtensionName(fil)) = "DEF" Then
         strDefFile = fil
         SpecialLink = True
      End If
      If UCase(oFS.GetExtensionName(fil)) = "RES" Then
         strResFile = fil
         fResource = True
      End If
      If SpecialLink And fResource Then Exit For
   Next
     
   ' Change command line arguments if flag set
   If SpecialLink Then
      ' Determine contents of .DEF file
      Set tsDef = oFS.OpenTextFile(strDefFile)
      strFileContents = tsDef.ReadAll
      If InStr(1, strFileContents, "CplApplet", vbTextCompare) > 0 Then
         fCPL = True
      End If
     
      ' Add module definition before /DLL switch
      intPos = InStr(1, strCmd, "/DLL", vbTextCompare)
      If intPos > 0 Then
         strCmd = Left(strCmd, intPos - 1) & _
               " /DEF:" & Chr(34) & strDefFile & Chr(34) & " " & _
               Mid(strCmd, intPos)
      End If
      ' Include .RES file if one exists
      If fResource Then
         intPos = InStr(1, strCmd, "/ENTRY", vbTextCompare)
         strCmd = Left(strCmd, intPos - 1) & Chr(34) & strResFile & _
                  Chr(34) & " " & Mid(strCmd, intPos)
      End If
     
      ' If Control Panel applet, change "DLL" extension to "CPL"
      If fCPL Then
         strCmd = Replace(strCmd, ".dll", ".cpl", 1, , vbTextCompare)
      End If
     
      ' Write linker options to output file
      ts.WriteLine "Command line arguments after modification:"
      ts.WriteBlankLines 1
      ts.WriteLine "   " & strCmd
      ts.WriteBlankLines 2
   End If
  
   ts.WriteLine "Calling LINK.EXE linker"
   Shell "linklnk.exe " & strCmd
   If Err.Number <> 0 Then
      ts.WriteLine "Error in calling linker..."
      Err.Clear
   End If
  
   ts.WriteBlankLines 1
   ts.WriteLine "Returned from linker call"
   ts.Close
End Sub

以上生成NewLinker.exe 替代VB中的link.exe

-------------------------------------------------------------------------

DLL模块文件,类模块文件

Option Explicit

Public Const DLL_PROCESS_DETACH = 0
Public Const DLL_PROCESS_ATTACH = 1
Public Const DLL_THREAD_ATTACH = 2
Public Const DLL_THREAD_DETACH = 3

Public Function DllMain(hInst As Long, fdwReason As Long, lpvReserved As Long) As Boolean
   Select Case fdwReason
      Case DLL_PROCESS_DETACH
         ' No per-process cleanup needed
      Case DLL_PROCESS_ATTACH
         DllMain = True
      Case DLL_THREAD_ATTACH
         ' No per-thread initialization needed
      Case DLL_THREAD_DETACH
         ' No per-thread cleanup needed
   End Select
End Function

' Return a Fibonacci number.
Public Function Fibo(ByVal N As Integer) As Long
    If N <= 1 Then
        Fibo = 1
    Else
        Fibo = Fibo(N - 1) + Fibo(N - 2)
    End If
End Function

-------------------------------------------------------------------------------

Fibonacci.def文件

NAME MathLib
LIBRARY MathMod
DESCRIPTION "Add-on Library of Mathematical Routines"
EXPORTS DllMain @1
                   Fibo @2

--------------------------------------------------------------------------------

如果觉得麻烦,还是用PowerBasic,它支持VB语法,函数都是Win32API,无需声明

能够生成象VC一样的标准DLL,不依赖MSVBVM60.Dll

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值