-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathInformation.vb
More file actions
592 lines (440 loc) · 16.3 KB
/
Copy pathInformation.vb
File metadata and controls
592 lines (440 loc) · 16.3 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.
Imports System
Imports Community.VisualBasic.CompilerServices
Imports Community.VisualBasic.CompilerServices.ExceptionUtils
Imports Community.VisualBasic.CompilerServices.Utils
Namespace Global.Community.VisualBasic
Public Module Information
'QBColorTable below consists of :
'&H0I, ' 0 - black
'&H800000I, ' 1 - blue
'&H8000I, ' 2 - green
'&H808000I, ' 3 - cyan
'&H80I, ' 4 - red
'&H800080I, ' 5 - magenta
'&H8080I, ' 6 - yellow
'&HC0C0C0I, ' 7 - white
'&H808080I, ' 8 - gray
'&HFF0000I, ' 9 - light blue
'&HFF00I, ' 10 - light green
'&HFFFF00I, ' 11 - light cyan
'&HFFI, ' 12 - light red
'&HFF00FFI, ' 13 - light magenta
'&HFFFFI, ' 14 - light yellow
'&HFFFFFFI, ' 15 - bright white
Private ReadOnly QBColorTable() As Integer = {&H0I, &H800000I, &H8000I, &H808000I,
&H80I, &H800080I, &H8080I,
&HC0C0C0I, &H808080I, &HFF0000I,
&HFF00I, &HFFFF00I, &HFFI,
&HFF00FFI, &HFFFFI, &HFFFFFFI}
Friend Const COMObjectName As String = "__ComObject"
'============================================================================
' Error functions.
'============================================================================
Public Function Err() As ErrObject
Dim oProj As ProjectData
oProj = ProjectData.GetProjectData()
If oProj.m_Err Is Nothing Then
oProj.m_Err = New ErrObject
End If
Err = oProj.m_Err
End Function
Public Function Erl() As Integer
Dim oProj As ProjectData
oProj = ProjectData.GetProjectData()
Erl = oProj.m_Err.Erl
End Function
Public Function IsArray(VarName As Object) As Boolean
If VarName Is Nothing Then
Return False
End If
Return (TypeOf VarName Is System.Array)
End Function
Public Function IsDate(Expression As Object) As Boolean
If Expression Is Nothing Then
Return False
End If
If TypeOf Expression Is Date Then
Return True
Else
Dim stringExpression As String = TryCast(Expression, String)
If stringExpression IsNot Nothing Then
Dim convertedDate As DateTime
Return Conversions.TryParseDate(stringExpression, convertedDate)
End If
End If
Return False
End Function
Public Function IsDBNull(Expression As Object) As Boolean
If Expression Is Nothing Then
Return False
ElseIf TypeOf Expression Is System.DBNull Then
Return True
Else
Return False
End If
End Function
Public Function IsNothing(Expression As Object) As Boolean
Return (Expression Is Nothing)
End Function
Public Function IsError(Expression As Object) As Boolean
If Expression Is Nothing Then
Return False
End If
Return (TypeOf Expression Is Exception)
End Function
Public Function IsReference(Expression As Object) As Boolean
Return Not (TypeOf Expression Is System.ValueType)
End Function
Public Function LBound(Array As System.Array, Optional Rank As Integer = 1) As Integer
If (Array Is Nothing) Then
Throw VbMakeException(New ArgumentNullException(NameOf(Array)), VbErrors.OutOfBounds)
ElseIf (Rank < 1) OrElse (Rank > Array.Rank) Then
Throw New RankException(SR.Format(SR.Argument_InvalidRank1, NameOf(Rank)))
End If
Return Array.GetLowerBound(Rank - 1)
End Function
Public Function UBound(Array As System.Array, Optional Rank As Integer = 1) As Integer
If (Array Is Nothing) Then
Throw VbMakeException(New ArgumentNullException(NameOf(Array)), VbErrors.OutOfBounds)
ElseIf (Rank < 1) OrElse (Rank > Array.Rank) Then
Throw New RankException(SR.Format(SR.Argument_InvalidRank1, NameOf(Rank)))
End If
Return Array.GetUpperBound(Rank - 1)
End Function
Friend Function TypeNameOfCOMObject(VarName As Object, bThrowException As Boolean) As String
Dim Result As String = COMObjectName
If OperatingSystem.IsWindows Then
Dim pTypeInfo As UnsafeNativeMethods.ITypeInfo = Nothing
Dim hr As Integer
Dim ClassName As String = Nothing
Dim DocString As String = Nothing
Dim HelpContext As Integer
Dim HelpFile As String = Nothing
Do
Dim pProvideClassInfo As UnsafeNativeMethods.IProvideClassInfo = TryCast(VarName, UnsafeNativeMethods.IProvideClassInfo)
If pProvideClassInfo IsNot Nothing Then
Try
pTypeInfo = pProvideClassInfo.GetClassInfo()
hr = pTypeInfo.GetDocumentation(-1, ClassName, DocString, HelpContext, HelpFile)
If hr >= 0 Then
Result = ClassName
Exit Do
End If
pTypeInfo = Nothing
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
'Ignore the error
End Try
End If
Dim pDispatch As UnsafeNativeMethods.IDispatch = TryCast(VarName, UnsafeNativeMethods.IDispatch)
If pDispatch IsNot Nothing Then
' Try using IDispatch
hr = pDispatch.GetTypeInfo(0, UnsafeNativeMethods.LCID_US_ENGLISH, pTypeInfo)
If hr >= 0 Then
hr = pTypeInfo.GetDocumentation(-1, ClassName, DocString, HelpContext, HelpFile)
If hr >= 0 Then
Result = ClassName
Exit Do
End If
End If
End If
Loop While (False)
End If
If Result.Chars(0) = "_"c Then
Result = Result.Substring(1)
End If
Return Result
End Function
Public Function QBColor(Color As Integer) As Integer
If (Color And &HFFF0I) <> 0 Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, NameOf(Color)), NameOf(Color))
End If
Return QBColorTable(Color)
End Function
Public Function RGB(Red As Integer, Green As Integer, Blue As Integer) As Integer
If (Red And &H80000000I) <> 0 Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, NameOf(Red)), NameOf(Red))
ElseIf (Green And &H80000000I) <> 0 Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, NameOf(Green)), NameOf(Green))
ElseIf (Blue And &H80000000I) <> 0 Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, NameOf(Blue)), NameOf(Blue))
End If
' VB2 treats any value > 255 as 255
If (Red > 255) Then
Red = &HFFI
End If
If (Green > 255) Then
Green = &HFFI
End If
If (Blue > 255) Then
Blue = &HFFI
End If
Return ((Blue * &H10000I) + (Green * &H100I) + Red)
End Function
Public Function VarType(VarName As Object) As VariantType
If VarName Is Nothing Then
Return VariantType.Object
End If
Return VarTypeFromComType(VarName.GetType())
End Function
Friend Function VarTypeFromComType(typ As System.Type) As VariantType
If typ Is Nothing Then
Return VariantType.Object
End If
If typ.IsArray() Then
typ = typ.GetElementType()
If typ.IsArray Then
Return CType(VariantType.Array Or VariantType.Object, VariantType)
End If
Dim result As VariantType = VarTypeFromComType(typ)
If (result And VariantType.Array) <> 0 Then
'Element type is also an array, so just return "array of objects"
Return CType(VariantType.Array Or VariantType.Object, VariantType)
End If
Return CType(result Or VariantType.Array, VariantType)
ElseIf typ.IsEnum() Then
typ = System.Enum.GetUnderlyingType(typ)
End If
If typ Is Nothing Then
Return VariantType.Empty
End If
Select Case Type.GetTypeCode(typ)
Case TypeCode.String
Return VariantType.String
Case TypeCode.Int32
Return VariantType.Integer
Case TypeCode.Int16
Return VariantType.Short
Case TypeCode.Int64
Return VariantType.Long
Case TypeCode.Single
Return VariantType.Single
Case TypeCode.Double
Return VariantType.Double
Case TypeCode.DateTime
Return VariantType.Date
Case TypeCode.Boolean
Return VariantType.Boolean
Case TypeCode.Decimal
Return VariantType.Decimal
Case TypeCode.Byte
Return VariantType.Byte
Case TypeCode.Char
Return VariantType.Char
Case TypeCode.DBNull
Return VariantType.Null
End Select
If (typ Is GetType(System.Reflection.Missing)) OrElse
(typ Is GetType(System.Exception)) OrElse
(typ.IsSubclassOf(GetType(System.Exception))) Then
Return VariantType.Error
ElseIf typ.IsValueType() Then
Return VariantType.UserDefinedType
Else
Return VariantType.Object
End If
End Function
Friend Function IsOldNumericTypeCode(TypCode As System.TypeCode) As Boolean
Select Case TypCode
Case TypeCode.Int16,
TypeCode.Int32,
TypeCode.Int64,
TypeCode.Single,
TypeCode.Double,
TypeCode.Boolean,
TypeCode.Decimal,
TypeCode.Byte
Return True
Case Else
Return False
End Select
End Function
Public Function IsNumeric(Expression As Object) As Boolean
Dim valueInterface As IConvertible
Dim valueTypeCode As TypeCode
valueInterface = TryCast(Expression, IConvertible)
If valueInterface Is Nothing Then
Dim charArray As Char() = TryCast(Expression, Char())
If charArray IsNot Nothing Then
Expression = CStr(charArray)
Else
Return False
End If
End If
valueTypeCode = valueInterface.GetTypeCode()
If (valueTypeCode = TypeCode.String) OrElse (valueTypeCode = TypeCode.Char) Then
'Convert to double, exception thrown if not a number
Dim dbl As Double
Dim i64Value As Int64
Dim value As String
value = valueInterface.ToString(Nothing)
Try
If IsHexOrOctValue(value, i64Value) Then
Return True
End If
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Return False
End Try
Return DoubleType.TryParse(value, dbl)
End If
Return IsOldNumericTypeCode(valueTypeCode)
End Function
Friend Function OldVBFriendlyNameOfTypeName(typename As String) As String
Dim ArraySuffix As String = Nothing
Dim Name As String
Dim LastChar As Integer = typename.Length - 1
If typename.Chars(LastChar) = "]"c Then
Dim pos As Integer
pos = typename.IndexOf("["c)
If pos + 1 = LastChar Then
ArraySuffix = "()"
Else
ArraySuffix = typename.Substring(pos, LastChar - pos + 1).Replace("["c, "("c).Replace("]"c, ")"c)
End If
typename = typename.Substring(0, pos)
End If
Name = OldVbTypeName(typename)
If Name Is Nothing Then
Name = typename
End If
If ArraySuffix Is Nothing Then
Return Name
End If
Return Name & AdjustArraySuffix(ArraySuffix)
End Function
Public Function TypeName(VarName As Object) As String
Dim Result As String
Dim bIsArray As Boolean
Dim typ As System.Type
Dim ArrayType As System.Type
If VarName Is Nothing Then
Return "Nothing"
End If
typ = VarName.GetType()
If typ.IsArray Then
bIsArray = True
ArrayType = typ
typ = ArrayType.GetElementType()
End If
If typ.IsEnum() Then
Result = typ.Name
GoTo UnmangleName
Else
Dim tc As TypeCode
tc = Type.GetTypeCode(typ)
Select Case tc
Case TypeCode.DBNull : Result = "DBNull"
Case TypeCode.Int16 : Result = "Short"
Case TypeCode.Int32 : Result = "Integer"
Case TypeCode.Single : Result = "Single"
Case TypeCode.Double : Result = "Double"
Case TypeCode.DateTime : Result = "Date"
Case TypeCode.String : Result = "String"
Case TypeCode.Boolean : Result = "Boolean"
Case TypeCode.Decimal : Result = "Decimal"
Case TypeCode.Byte : Result = "Byte"
Case TypeCode.Char : Result = "Char"
Case TypeCode.Int64 : Result = "Long"
Case Else
Result = typ.Name
If (typ.IsCOMObject AndAlso (System.String.CompareOrdinal(Result, COMObjectName) = 0)) Then
Result = LegacyTypeNameOfCOMObject(VarName, True)
End If
UnmangleName:
Dim i As Integer
i = Result.IndexOf("+"c)
If i >= 0 Then
Result = Result.Substring(i + 1)
End If
End Select
End If
If bIsArray Then
Dim ary As Array
ary = CType(VarName, Array)
If ary.Rank = 1 Then
Result &= "[]"
Else
Result = Result & "[" & (New String(","c, ary.Rank - 1)) & "]"
End If
Result = OldVBFriendlyNameOfTypeName(Result)
End If
Return Result
End Function
Public Function SystemTypeName(VbName As String) As String
Select Case Trim(VbName).ToUpperInvariant()
Case "OBJECT" : Return "System.Object"
Case "SHORT" : Return "System.Int16"
Case "INTEGER" : Return "System.Int32"
Case "SINGLE" : Return "System.Single"
Case "DOUBLE" : Return "System.Double"
Case "DATE" : Return "System.DateTime"
Case "STRING" : Return "System.String"
Case "BOOLEAN" : Return "System.Boolean"
Case "DECIMAL" : Return "System.Decimal"
Case "BYTE" : Return "System.Byte"
Case "CHAR" : Return "System.Char"
Case "LONG" : Return "System.Int64"
Case Else : Return Nothing
End Select
End Function
Public Function VbTypeName(UrtName As String) As String
Return OldVbTypeName(UrtName)
End Function
Friend Function OldVbTypeName(UrtName As String) As String
UrtName = Trim(UrtName).ToUpperInvariant()
If Left(UrtName, 7) = "SYSTEM." Then
UrtName = Mid(UrtName, 8)
End If
Select Case UrtName
Case "OBJECT" : Return "Object"
Case "INT16" : Return "Short"
Case "INT32" : Return "Integer"
Case "SINGLE" : Return "Single"
Case "DOUBLE" : Return "Double"
Case "DATETIME" : Return "Date"
Case "STRING" : Return "String"
Case "BOOLEAN" : Return "Boolean"
Case "DECIMAL" : Return "Decimal"
Case "BYTE" : Return "Byte"
Case "CHAR" : Return "Char"
Case "INT64" : Return "Long"
Case Else
Return Nothing
End Select
End Function
Friend Function LegacyTypeNameOfCOMObject(VarName As Object, bThrowException As Boolean) As String
Dim Result As String = COMObjectName
If OperatingSystem.IsWindows Then
Dim pTypeInfo As UnsafeNativeMethods.ITypeInfo = Nothing
Dim hr As Integer
Dim ClassName As String = Nothing
Dim DocString As String = Nothing
Dim HelpContext As Integer
Dim HelpFile As String = Nothing
Dim pDispatch As UnsafeNativeMethods.IDispatch = TryCast(VarName, UnsafeNativeMethods.IDispatch)
If pDispatch IsNot Nothing Then
hr = pDispatch.GetTypeInfo(0, UnsafeNativeMethods.LCID_US_ENGLISH, pTypeInfo)
If hr >= 0 Then
hr = pTypeInfo.GetDocumentation(-1, ClassName, DocString, HelpContext, HelpFile)
If hr >= 0 Then
Result = ClassName
End If
End If
End If
End If
If Result.Chars(0) = "_"c Then
Result = Result.Substring(1)
End If
Return Result
End Function
End Module
End Namespace