-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodUTypes.bas
More file actions
145 lines (112 loc) · 3.92 KB
/
Copy pathmodUTypes.bas
File metadata and controls
145 lines (112 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
Attribute VB_Name = "modUTypes"
'Author: David Zimmer <dzzie@yahoo.com>
'AI: Claude.ai
'Site: http://sandsprite.com
'License: MIT
Option Explicit
Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)
Public hUtypes As Long
Public uTypesPath As String
Function ensureUTypes() As Boolean
On Error Resume Next
If hUtypes <> 0 Then
ensureUTypes = True
Exit Function
End If
Dim pth As String, b() As Byte, f As Long
Dim thisDll As String, pd(), parentDir
thisDll = GetDllPath("vbUtypes.dll")
If Len(thisDll) > 0 Then push pd, GetParentFolder(thisDll)
push pd, App.path
push pd, Environ("WinDir")
For Each parentDir In pd
pth = parentDir & "\UTypes.dll"
If Not FileExists(pth) Then pth = parentDir & "\UTypes.dll"
If Not FileExists(pth) Then pth = parentDir & "\..\UTypes.dll"
If Not FileExists(pth) Then pth = parentDir & "\..\..\UTypes.dll"
If Not FileExists(pth) Then pth = parentDir & "\..\..\..\UTypes.dll"
If FileExists(pth) Then Exit For
Next
' If Not FileExists(pth) Then
' pth = App.path & "\UTypes.dll"
' b() = LoadResData("UTYPES", "DLLS")
' If AryIsEmpty(b) Then
' MsgBox "Failed to find UTypes.dll in resource?"
' Exit Function
' End If
'
' f = FreeFile
' Open pth For Binary As f
' Put f, , b()
' Close f
'MsgBox "Dropped utypes.dll to: " & pth & " - Err: " & Err.Number
' End If
hUtypes = LoadLibrary(pth)
If hUtypes = 0 Then Exit Function
uTypesPath = pth
ensureUTypes = True
End Function
Public Function GetDllPath(Optional dll As String = "vbUtypes.dll") As String
Dim h As Long, ret As String
ret = Space(500)
h = GetModuleHandle(dll)
h = GetModuleFileName(h, ret, 500)
If h > 0 Then ret = Mid(ret, 1, h)
GetDllPath = ret
End Function
Sub push(ary, value) 'this modifies parent ary object
On Error GoTo Init
Dim X
X = UBound(ary)
ReDim Preserve ary(X + 1)
If isObject(value) Then
Set ary(X + 1) = value
Else
ary(X + 1) = value
End If
Exit Sub
Init:
ReDim ary(0)
If isObject(value) Then
Set ary(0) = value
Else
ary(0) = value
End If
End Sub
Function GetParentFolder(path, Optional levelUp = 1)
Dim tmp() As String
Dim my_path
Dim ub As String, i As Long
On Error GoTo hell
If Len(path) = 0 Then Exit Function
If levelUp < 1 Then levelUp = 1
my_path = path
While Len(my_path) > 0 And Right(my_path, 1) = "\"
my_path = Mid(my_path, 1, Len(my_path) - 1)
Wend
tmp = Split(my_path, "\")
If levelUp > UBound(tmp) Then levelUp = UBound(tmp)
For i = 0 To levelUp - 1
If InStr(tmp(UBound(tmp) - i), ":") < 1 Then tmp(UBound(tmp) - i) = Empty
Next
my_path = Join(tmp, "\")
While Len(my_path) > 0 And Right(my_path, 1) = "\"
my_path = Mid(my_path, 1, Len(my_path) - 1)
Wend
GetParentFolder = my_path
Exit Function
hell:
GetParentFolder = Empty
End Function
Function FileExists(path) As Boolean
On Error GoTo hell
If Len(path) = 0 Then Exit Function
If Right(path, 1) = "\" Then Exit Function
If InStr(path, Chr(0)) > 0 Then Exit Function
If Dir(path, vbHidden Or vbNormal Or vbReadOnly Or vbSystem) <> "" Then FileExists = True
Exit Function
hell: FileExists = False
End Function