-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJVPToolProcs2.bas
380 lines (316 loc) · 14.1 KB
/
JVPToolProcs2.bas
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
Attribute VB_Name = "JVPToolProcs"
'-----------------------------------------------------------------------------------
' JVP ToolProcedures V2.00 by Jens Vang Petersen (VB6 Edition)
'-----------------------------------------------------------------------------------
' Discs and files:
' Boolan = jExistFile(Filename) Check if a file exist
' String = BuildFileName(Path,File) Build a full filename (adds \ to path if needed)
' String = GetDiscVolume(RootPath) Get the Volumename of a disc (HD, FLOPPY, CD, etc.)
' Long = GetDiscSerial(RootPath) Get the Serialnumber of a disc
' ExecuteFile(File) Execute another program.
' ViewFile(CallingForm,File) Open a file in it's default program.
' (Normaly you'll use "Me" for CallingForm)
'
' Sound (Remember to do a WAVStop before you leave the program):
' WAVPlay(File) Play a Wav sound
' WAVLoop(File) Play a Wav sound and repeat it forever
' WAVStop(File) Stop the sound playing (if any)
'
' Strings
' String = Removechar(Source,Remove) Removes part of a string like a$=a$-"STR" in Amos
' String = Replacechar(Src,Rem,New) Replaces 'Rem' with 'new' in Src string.
'
'-----------------------------------------------------------------------------------
' IMPORTAINT: Some of these functions and procedures make calls to other in the
' module. Especialy the string functions are called often by other functions.
'-----------------------------------------------------------------------------------
' The JVP ToolProcedures is something I began making in my old Amiga-Amos days, And
' I've now moved them onto my new world in VB5.0. The Idea is basicaly to have a
' shared module of procedures (I usualy include the SAME module in all my projects,
' so if I find a bug it's automaticaly adjusted in all other projects.) doing stuff
' I often need to do, and so I don't have to invent the same twice, or go looking
' through older projects to try and find the needed piece of code. Especialy when
' one's used to working with AMOS and the many extensions from there VB seems a bit
' limited in functions, so many of these 'tools' is in fact a remake of some old
' but usefull functions from other coding systems.
'-----------------------------------------------------------------------------------
' Please note that some of these functions are based on methods I've found on the
' web, if I've been able to find the original author he'll be give credit in the
' proper procedure..
'-----------------------------------------------------------------------------------
Public Declare Function ShellExecute Lib "Shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Declare Function FindExecutableA Lib "Shell32.dll" _
(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Private Declare Function SetVolumeLabelA Lib "kernel32" (ByVal lpRootPathName As String, ByVal lpVolumeName As String) As Long
'Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" (ByVal lpRootPathName As String, lpFreeBytesAvailableToCaller As LARGE_INTEGER, lpTotalNumberOfBytes As LARGE_INTEGER, lpTotalNumberOfFreeBytes As LARGE_INTEGER) As Long
Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public Const SND_SYNC = &H0
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10
Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Const GWL_WNDPROC = (-4)
Private MsgID_QueryCancelAutoPlay As Long, AROldProc As Long
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
' Retrieves the ID of a special folder.
Private Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
' Converts an item identifier list to a file system path.
Private Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Public Function fGetSpecialFolder(vFormHwnd As Long, CSIDL As Long) As String
Dim sPath As String
Dim IDL As ITEMIDLIST
'
' Retrieve info about system folders such as the
' "Recent Documents" folder. Info is stored in
' the IDL structure.
'
fGetSpecialFolder = ""
If SHGetSpecialFolderLocation(vFormHwnd, CSIDL, IDL) = 0 Then
'
' Get the path from the ID list, and return the folder.
'
sPath = Space$(260)
If SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath) Then
fGetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\"
End If
End If
End Function
'
' Functions regarding numbers
'
Public Function RandomNumber(wFirst, wLast) As Long
RandomNumber = Int((wLast - wFirst + 1) * Rnd + wFirst)
End Function
Public Function jRndPick(ParamArray wValues() As Variant) As Variant
Debug.Print UBound(wValues)
jRndPick = wValues(Int((UBound(wValues) + 1) * Rnd))
End Function
Public Function SecondsToTime(wSec As Long) As String
Wa = Int(wSec / 3600)
Wc = wSec Mod 3600
Wb = Int(Wc / 60)
Wc = Wc Mod 60
If Wa > 0 Then
SecondsToTime = Format(Wa, "00") + ":" + Format(Wb, "00") + ":" + Format(Wc, "00")
Else
SecondsToTime = Format(Wb, "00") + ":" + Format(Wc, "00")
End If
End Function
Public Sub ExecuteFile(ByVal FilePath As String)
'
' Execute a file
' Based on code from planet-source-code.com
'
On Error GoTo 0
ret = Shell("rundll32.exe url.dll,FileProtocolHandler " & (FilePath), vbNormalFocus)
End Sub
Public Sub ViewFile(ByVal wCaller As Form, ByVal wFile As String)
rc = ShellExecute(wCaller.hwnd, "Open", wFile, vbNullString, App.Path, vbNormalFocus)
End Sub
Public Function FindExecutable(ByVal s As String) As String
Dim i As Integer, s2 As String
s2 = String(1024, 31)
i = FindExecutableA(s, vbNullString, s2)
s2 = Trim(s2)
If i > 32 Then
'FindExecutable = Left$(s2, InStr(s2, Chr$(0)) - 1)
FindExecutable = s2
Else
FindExecutable = ""
End If
Debug.Print s2
End Function
Public Sub InitStopAutoRun(ByVal wCaller As Form)
'
' We ask for a message from the system just before autorun is called
' Then we store the normal eventhandler and setup our own..
'
MsgID_QueryCancelAutoPlay = RegisterWindowMessage(ByVal "QueryCancelAutoPlay")
AROldProc = GetWindowLong(wCaller.hwnd, GWL_WNDPROC)
SetWindowLong wCaller.hwnd, GWL_WNDPROC, AddressOf StopAutoRun
End Sub
Private Function StopAutoRun(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'
' First we check to see if we've got the right event, if not we call the
' normal eventhandler..
'
If wMsg = MsgID_QueryCancelAutoPlay Then
sText = Format(wMsg) + " - " + Format(wparm) + " - " + Format(lparm)
Form1.Text1 = Form1.Text1 & sText & vbCrLf
StopAutoRun = 1
Else
StopAutoRun = CallWindowProc(AROldProc, hwnd, wMsg, wParam, lParam)
End If
End Function
Public Sub CloseStopAutoRun(ByVal wCaller As Form)
SetWindowLong wCaller.hwnd, GWL_WNDPROC, AROldProc
End Sub
'-----------------------------------------------------------------------------------
'
' Stuff for strings
'
'-----------------------------------------------------------------------------------
Public Function jRandomName(ByVal minLen As Integer, ByVal maxLen As Integer) As String
Dim Wa As Integer, Wb As Integer
Randomize
Wa = Int((maxLen - minLen + 1) * Rnd) + minLen:
jRandomName = ""
For Wb = 1 To Wa
jRandomName = jRandomName + Chr(Int(Rnd * 26) + 97)
Next Wb
End Function
Public Function jRandomString(ByVal minWords As Integer, ByVal maxWords As Integer) As String
Dim Wa As Integer
Wa = Int((maxWords - minWords + 1) * Rnd) + minWords:
For Wb = 1 To Wa - 1
jRandomString = jRandomString + jRandomName(1, 14) + " "
Next Wb
jRandomString = jRandomString + jRandomName(2, 18)
End Function
Public Function jAscUL(ByVal wStr As String) As Double
wStr = wStr + ChrB(0) + ChrB(0) + ChrB(0) + ChrB(0)
jAscUL = jAscUW(wStr) + jAscUW(Mid(wStr, 3)) * 65536
End Function
Public Function jAscbL(ByVal wStr As String) As Long
Dim W1 As Long, W2 As Long, W3 As Long, W4 As Long
'
' Take a 4 byte string into a Long number (Intel format with Lo-byte first)
'
wStr = wStr + ChrB(0) + ChrB(0) + ChrB(0) + ChrB(0)
W1 = AscB(wStr): W2 = AscB(MidB(wStr, 2)): W3 = AscB(MidB(wStr, 3)): W4 = AscB(MidB(wStr, 4))
'Debug.Print W1, W2, W3, W4
If W4 > 127 Then
jAscbL = W1 - 256 + (W2 - 255) * 256 + (W3 - 255) * 65536 + (W4 - 255) * 16777216
Else
jAscbL = W1 + W2 * 256 + W3 * 65536 + W4 * 16777216
End If
End Function
Public Function jChrbL(ByVal wVal As Long) As String
Dim W1 As Integer, W2 As Integer, W3 As Integer, W4 As Integer
If wVal < 0 Then
W4 = Int(wVal / 16777216) + 255: wVal = wVal Mod 16777216
W3 = Int(wVal / 65536) + 255: wVal = wVal Mod 65536
W2 = Int(wVal / 256) + 255: wVal = wVal Mod 256
W1 = wVal + 256
Else
W4 = Int(wVal / 16777216): wVal = wVal Mod 16777216
W3 = Int(wVal / 65536): wVal = wVal Mod 65536
W2 = Int(wVal / 256): wVal = wVal Mod 256
W1 = wVal
End If
'Debug.Print W1, W2, W3, W4
jChrbL = ChrB(W1) + ChrB(W2) + ChrB(W3) + ChrB(W4)
End Function
Public Function jAscUW(ByVal wStr As String) As Double
wStr = wStr + ChrB(0) + ChrB(0)
jAscUW = AscB(wStr) + AscB(MidB(wStr, 2)) * 256
End Function
Public Function jAscW(ByVal wStr As String) As Double
Dim W1 As Long, W2 As Long
'
' Take a 4 byte string into a Long number (Intel format with Lo-byte first)
'
wStr = wStr + ChrB(0) + ChrB(0)
W1 = AscB(wStr): W2 = AscB(MidB(wStr, 2))
'Debug.Print W1, W2
If W2 > 127 Then
jAscW = W1 - 256 + (W2 - 255) * 256
Else
jAscW = W1 + W2 * 256
End If
End Function
Public Function jChrbW(ByVal wVal As Long) As String
Dim W1 As Integer, W2 As Integer
Debug.Print wVal
If wVal < 65536 And wVal > -32769 Then
If wVal < 0 Then
W2 = Int(wVal / 256) + 255: wVal = wVal Mod 256
W1 = wVal + 256
Else
W2 = Int(wVal / 256): wVal = wVal Mod 256
W1 = wVal
End If
'Debug.Print W1, W2
jChrbW = ChrB(W1) + ChrB(W2)
End If
End Function
Public Function jMakeString(ByVal wStr As String)
Dim Wa As Integer
For Wa = 1 To LenB(wStr)
If AscB(MidB(wStr, Wa)) = 0 Then Exit For
jMakeString = jMakeString + Chr(AscB(MidB(wStr, Wa)))
Next Wa
End Function
Public Function jRange(ByVal wMin As Double, ByVal wVal As Double, ByVal wMax As Double) As Double
'
' Range function..
'
Dim Wa As Double
If wMin > wMax Then Wa = wMin: wMin = wMax: wMax = Wa
If wVal > wMax Then wVal = wMax
If wVal < wMin Then wVal = wMin
jRange = wVal
End Function
Public Function jMin(ByRef wVal1 As Variant, ByRef wVal2 As Variant) As Variant
jMin = wVal1
If wVal2 < wVal1 Then jMin = wVal2
End Function
Public Function jMax(ByRef wVal1 As Variant, ByRef wVal2 As Variant) As Variant
jMax = wVal1
If wVal2 > wVal1 Then jMax = wVal2
End Function
Public Function jBetween(ByVal wMin As Variant, ByVal wVal As Variant, ByVal wMax As Variant) As Boolean
jBetween = False
If wMin < wVal And wVal < wMax Then jBetween = True
End Function
Public Sub WAVStop()
Call WAVPlay(vbNullString)
End Sub
Public Sub WAVLoop(ByVal File)
Dim SoundName As String
SoundName$ = File
wFlags% = &HB
X = sndPlaySound(SoundName$, wFlags%)
End Sub
Public Sub WAVPlay(ByVal File)
Dim SoundName As String, wFlags As Long
SoundName = File
wFlags = &H3
X = sndPlaySound(SoundName, wFlags)
End Sub
Public Function jFrmIsLoaded(wFormName As String) As Boolean
'This function returns true if a form is loaded
'
'Parameters:
' Formname: Name of the form
'
Dim wi As Integer
wFormName = UCase$(wFormName)
jFrmIsLoaded = False
For wi = 0 To (Forms.Count - 1)
If UCase$(Forms(wi).Name) = wFormName Then
jFrmIsLoaded = True
Exit For
End If
Next wi
End Function
Public Function jSecsToTime(wSecs As Long) As Date
'
End Function