Skip to content

Instantly share code, notes, and snippets.

@xxdoc
Forked from wqweto/mdMain.bas
Created December 10, 2016 10:30
Show Gist options
  • Select an option

  • Save xxdoc/cf6b775f9b931616cd41c3e756e8fda9 to your computer and use it in GitHub Desktop.

Select an option

Save xxdoc/cf6b775f9b931616cd41c3e756e8fda9 to your computer and use it in GitHub Desktop.
VB6 surrogate linker
Attribute VB_Name = "mdMain"
Option Explicit
'=========================================================================
' API
'=========================================================================
Private Const INVALID_FILE_ATTRIBUTES As Long = -1
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
'=========================================================================
' Constants and member variables
'=========================================================================
Private Const STR_COBJ_EXT As String = "cobj"
Private Const STR_ORIGINAL_LINKER As String = "vblink.exe"
'=========================================================================
' Functions
'=========================================================================
Public Function PathCombine(sPath As String, sFile As String) As String
PathCombine = sPath & IIf(LenB(sPath) <> 0 And Right$(sPath, 1) <> "\", "\", vbNullString) & sFile
End Function
Public Function SplitArgs(sText As String) As Variant
Dim oMatches As Object
Dim vRetVal As Variant
Dim lIdx As Long
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = """([^""]*(?:""""[^""]*)*)""|([^ ]+)"
Set oMatches = .Execute(sText)
If oMatches.Count > 0 Then
ReDim vRetVal(0 To oMatches.Count - 1) As String
For lIdx = 0 To oMatches.Count - 1
With oMatches(lIdx)
vRetVal(lIdx) = Replace$(.SubMatches(0) & .SubMatches(1), """""", """")
End With
Next
Else
vRetVal = Split(vbNullString)
End If
End With
SplitArgs = vRetVal
End Function
Public Function ShellWait(sCommand As String, Optional ByVal WindowStyle As VbAppWinStyle = vbNormalFocus) As Long
ShellWait = CreateObject("WScript.Shell").Run(sCommand, WindowStyle, True)
End Function
Public Sub Main()
Dim sCommand As String
Dim vElem As Variant
Dim lPos As Long
Dim sFile As String
sCommand = Command$()
For Each vElem In SplitArgs(sCommand)
Select Case Left$(vElem, 1)
Case "-", "/"
Case Else
lPos = InStrRev(vElem, ".")
If lPos > InStrRev(vElem, "\") Then
sFile = Left$(vElem, lPos) & STR_COBJ_EXT
If GetFileAttributes(sFile) <> INVALID_FILE_ATTRIBUTES Then
sCommand = Replace(sCommand, vElem, sFile)
End If
End If
End Select
Next
Call ExitProcess(ShellWait("""" & PathCombine(App.Path, STR_ORIGINAL_LINKER) & """ " & sCommand, vbHide))
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment