From: Nobody on
"Nobody" <nobody(a)nobody.com> wrote in message
news:Oxaa5GGaKHA.4920(a)TK2MSFTNGP04.phx.gbl...
> Unfortunately, CConvert functions seem to be implemented in VB6 as
> function calls.

The above is incorrect. It seems that VB use the most "efficient" method to
convert from the given data type. For example, if you provide an integer
values to CInteger or CLng, little or no conversion is done. But if you
supply a String to CInteger or CLng, a function call is made to do the
conversion. This seems like C++ function templates and/or function
overloading where one or more version of the same function is written to
support each data type, and the compiler picks the one that match the data
type exactly, so the least amount of code is generated, and in cases where
the data types are identical or very similar, a direct assignment is used,
as if you didn't use the conversion function. VB compiler seems to translate
the code to intermediate code that is common with C++. VB's run time error
handling is similar to C++ exception handling, they behave mostly in the
same way.

See this article if you want to know what C++ function templates are:

http://en.wikipedia.org/wiki/C%2B%2B_Templates

Here are some situations that I tested. First the summary, followed by the
source code and assembly output. In the assembly output, skip the first few
lines which is code that VB generates to setup the error handler, even if
the source doesn't have error handler, so it shows the default error handler
message box. Watch for wrapped lines.

Situation 1: Using CLng(7) to convert to Long.
Result 1: Using CLng() has no impact.

Situation 2: Using CLng("7") to convert to Long.
Result 2: VB calls vbaI4Str() to do the conversion. "I4" in "vbaI4Str" seems
to stand for "4 bytes integer", which is Long in VB.

Situation 3: Using CLng(7) to convert to Currency.
Result 3: VB calls vbaCyI4() to do the conversion.

Situation 4: Same as above, but using Long variable instead of 7.
Result 4: Same result as above.

Situation 5: Assigning number 7 to a Currency variable.
Result 5: VB calls vbaCyI2() to do the conversion. "I2" in "vbaCyI2" seems
to stand for "2 bytes integer", which is Long in VB.

Situation 6: Assigning number 33333 to a Currency variable.
Result 6: VB calls vbaCyI4() to do the conversion. Note this has "I4" in the
function name, not "I2" like above. Constants that you use in code without
any type letter are treated as Integer if they fall in the range -32768 to
+32767. If you use "&", VB would treat the number as Long(4 bytes). This is
the same way C++ work, BTW.

Situation 7: Same as Situation 5, but using Long variable instead of 7.
Result 7: Same result as Situation 6.

Situation 8: Assigning number 7 declared as Currency Const to a Currency
variable.
Result 8: VB assigns &H11170 to the variable. &H11170 = 70000. Currency
values are scaled by a factor of 10000, so 70000 means 7.

Situation 9: Using the following formula:

Private Const MAXDWORD As Currency = 4294967296

<CurrencyVar> = MAXDWORD * <LongVar1> + <LongVar2>

Result 9: VB converts <LongVar1> to Currency using vbaCyI4, then multiply it
by MAXDWORD using vbaCyMul function call, then adds <LongVar2> to it using
vbaCyAdd function call.

Situation 10: Using LSet as suggested in this article:

How To Do 64-bit Arithmetic in VBA
http://support.microsoft.com/kb/189862

Sample code:

Private Type MungeCurr
Value As Currency
End Type

Private Type Munge2Long
LoValue As Long
HiValue As Long
End Type

Dim Src As Munge2Long
Dim Dest As MungeCurr

Src.LoValue = <LongVar1>
Src.HiValue = <LongVar2>
LSet Dest = Src
Dest.Value = Dest.Value * 10000


Result 10: VB assigns the Long variables, then calls vbaCopyBytes to perform
what "LSet" does. Then calls vbaCyMulI2 to multiply by 10000.



Situation 1: Using CLng(7) to convert to Long:
==============================================


Option Explicit

Dim i As Long
Dim j As Long
Dim k As Long

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
i = 3
j = CLng(7)
k = 9
End Sub



24: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
00401980 55 push ebp
00401981 8B EC mov ebp,esp
00401983 83 EC 0C sub esp,0Ch
00401986 68 B6 10 40 00 push offset ___vbaExceptHandler
(004010b6)
0040198B 64 A1 00 00 00 00 mov eax,fs:[00000000]
00401991 50 push eax
00401992 64 89 25 00 00 00 00 mov dword ptr fs:[0],esp
00401999 83 EC 08 sub esp,8
0040199C 53 push ebx
0040199D 56 push esi
0040199E 57 push edi
0040199F 89 65 F4 mov dword ptr [ebp-0Ch],esp
004019A2 C7 45 F8 A0 10 40 00 mov dword ptr [ebp-8],offset
__imp___CIexp+34h (004010a0)
004019A9 8B 75 08 mov esi,dword ptr [Me]
004019AC 8B C6 mov eax,esi
004019AE 83 E0 01 and eax,1
004019B1 89 45 FC mov dword ptr [ebp-4],eax
004019B4 83 E6 FE and esi,0FFFFFFFEh
004019B7 56 push esi
004019B8 89 75 08 mov dword ptr [Me],esi
004019BB 8B 0E mov ecx,dword ptr [esi]
004019BD FF 51 04 call dword ptr [ecx+4]
25: i = 3
004019C0 C7 46 34 03 00 00 00 mov dword ptr [esi+34h],3
26: j = CLng(7)
004019C7 C7 46 38 07 00 00 00 mov dword ptr [esi+38h],7
27: k = 9
004019CE C7 46 3C 09 00 00 00 mov dword ptr [esi+3Ch],9
28: End Sub
004019D5 C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
$L26:
004019DC 8B 45 08 mov eax,dword ptr [Me]
004019DF 50 push eax
004019E0 8B 10 mov edx,dword ptr [eax]
004019E2 FF 52 08 call dword ptr [edx+8]
004019E5 8B 45 FC mov eax,dword ptr [ebp-4]
004019E8 8B 4D EC mov ecx,dword ptr [ebp-14h]
004019EB 5F pop edi
004019EC 5E pop esi
004019ED 64 89 0D 00 00 00 00 mov dword ptr fs:[0],ecx
004019F4 5B pop ebx
004019F5 8B E5 mov esp,ebp
004019F7 5D pop ebp
004019F8 C2 14 00 ret 14h

Situation 2: Using CLng("7") to convert to Long:
================================================


Option Explicit

Dim i As Long
Dim j As Long
Dim k As Long

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
i = 3
j = CLng("7")
k = 9
End Sub



24: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
004019A0 55 push ebp
004019A1 8B EC mov ebp,esp
004019A3 83 EC 0C sub esp,0Ch
004019A6 68 B6 10 40 00 push offset ___vbaExceptHandler
(004010b6)
004019AB 64 A1 00 00 00 00 mov eax,fs:[00000000]
004019B1 50 push eax
004019B2 64 89 25 00 00 00 00 mov dword ptr fs:[0],esp
004019B9 83 EC 08 sub esp,8
004019BC 53 push ebx
004019BD 56 push esi
004019BE 57 push edi
004019BF 89 65 F4 mov dword ptr [ebp-0Ch],esp
004019C2 C7 45 F8 A0 10 40 00 mov dword ptr [ebp-8],offset
__imp___CIexp+30h (004010a0)
004019C9 8B 75 08 mov esi,dword ptr [Me]
004019CC 8B C6 mov eax,esi
004019CE 83 E0 01 and eax,1
004019D1 89 45 FC mov dword ptr [ebp-4],eax
004019D4 83 E6 FE and esi,0FFFFFFFEh
004019D7 56 push esi
004019D8 89 75 08 mov dword ptr [Me],esi
004019DB 8B 0E mov ecx,dword ptr [esi]
004019DD FF 51 04 call dword ptr [ecx+4]
25: i = 3
26: j = CLng("7")
004019E0 68 68 16 40 00 push offset ___vba(a)03EE01E0 (00401668)
004019E5 C7 46 34 03 00 00 00 mov dword ptr [esi+34h],3
004019EC FF 15 54 10 40 00 call dword ptr [__imp____vbaI4Str
(00401054)]
004019F2 89 46 38 mov dword ptr [esi+38h],eax
27: k = 9
004019F5 C7 46 3C 09 00 00 00 mov dword ptr [esi+3Ch],9
28: End Sub
004019FC C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
$L26:
00401A03 8B 45 08 mov eax,dword ptr [Me]
00401A06 50 push eax
00401A07 8B 10 mov edx,dword ptr [eax]
00401A09 FF 52 08 call dword ptr [edx+8]
00401A0C 8B 45 FC mov eax,dword ptr [ebp-4]
00401A0F 8B 4D EC mov ecx,dword ptr [ebp-14h]
00401A12 5F pop edi
00401A13 5E pop esi
00401A14 64 89 0D 00 00 00 00 mov dword ptr fs:[0],ecx
00401A1B 5B pop ebx
00401A1C 8B E5 mov esp,ebp
00401A1E 5D pop ebp
00401A1F C2 14 00 ret 14h


Situation 3: Using CLng(7) to convert to Currency:
==================================================

Option Explicit

Dim i As Long
Dim j As Long
Dim k As Currency
Dim m As Long

Private Sub Form_Load()
i = 7
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
j = 3
k = CLng(7)
m = 9
End Sub





29: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
00401A30 55 push ebp
00401A31 8B EC mov ebp,esp
00401A33 83 EC 0C sub esp,0Ch
00401A36 68 B6 10 40 00 push offset ___vbaExceptHandler
(004010b6)
00401A3B 64 A1 00 00 00 00 mov eax,fs:[00000000]
00401A41 50 push eax
00401A42 64 89 25 00 00 00 00 mov dword ptr fs:[0],esp
00401A49 83 EC 08 sub esp,8
00401A4C 53 push ebx
00401A4D 56 push esi
00401A4E 57 push edi
00401A4F 89 65 F4 mov dword ptr [ebp-0Ch],esp
00401A52 C7 45 F8 A8 10 40 00 mov dword ptr [ebp-8],offset
__imp___CIexp+38h (004010a8)
00401A59 8B 75 08 mov esi,dword ptr [Me]
00401A5C 8B C6 mov eax,esi
00401A5E 83 E0 01 and eax,1
00401A61 89 45 FC mov dword ptr [ebp-4],eax
00401A64 83 E6 FE and esi,0FFFFFFFEh
00401A67 56 push esi
00401A68 89 75 08 mov dword ptr [Me],esi
00401A6B 8B 0E mov ecx,dword ptr [esi]
00401A6D FF 51 04 call dword ptr [ecx+4]
30: j = 3
31: k = CLng(7)
00401A70 6A 07 push 7
00401A72 C7 46 38 03 00 00 00 mov dword ptr [esi+38h],3
00401A79 FF 15 28 10 40 00 call dword ptr [__imp_@__vbaCyI4
(00401028)]
00401A7F 89 46 3C mov dword ptr [esi+3Ch],eax
00401A82 89 56 40 mov dword ptr [esi+40h],edx
32: m = 9
00401A85 C7 46 44 09 00 00 00 mov dword ptr [esi+44h],9
33: End Sub
00401A8C C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
$L32:
00401A93 8B 45 08 mov eax,dword ptr [Me]
00401A96 50 push eax
00401A97 8B 10 mov edx,dword ptr [eax]
00401A99 FF 52 08 call dword ptr [edx+8]
00401A9C 8B 45 FC mov eax,dword ptr [ebp-4]
00401A9F 8B 4D EC mov ecx,dword ptr [ebp-14h]
00401AA2 5F pop edi
00401AA3 5E pop esi
00401AA4 64 89 0D 00 00 00 00 mov dword ptr fs:[0],ecx
00401AAB 5B pop ebx
00401AAC 8B E5 mov esp,ebp
00401AAE 5D pop ebp
00401AAF C2 14 00 ret 14h


Situation 4: Same as above, but using Long variable instead of 7:
=================================================================


Option Explicit

Dim i As Long
Dim j As Long
Dim k As Currency
Dim m As Long

Private Sub Form_Load()
i = 7
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
j = 3
k = CLng(i)
m = 9
End Sub




29: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
00401A30 55 push ebp
00401A31 8B EC mov ebp,esp
00401A33 83 EC 0C sub esp,0Ch
00401A36 68 B6 10 40 00 push offset ___vbaExceptHandler
(004010b6)
00401A3B 64 A1 00 00 00 00 mov eax,fs:[00000000]
00401A41 50 push eax
00401A42 64 89 25 00 00 00 00 mov dword ptr fs:[0],esp
00401A49 83 EC 08 sub esp,8
00401A4C 53 push ebx
00401A4D 56 push esi
00401A4E 57 push edi
00401A4F 89 65 F4 mov dword ptr [ebp-0Ch],esp
00401A52 C7 45 F8 A8 10 40 00 mov dword ptr [ebp-8],offset
__imp___CIexp+38h (004010a8)
00401A59 8B 75 08 mov esi,dword ptr [Me]
00401A5C 8B C6 mov eax,esi
00401A5E 83 E0 01 and eax,1
00401A61 89 45 FC mov dword ptr [ebp-4],eax
00401A64 83 E6 FE and esi,0FFFFFFFEh
00401A67 56 push esi
00401A68 89 75 08 mov dword ptr [Me],esi
00401A6B 8B 0E mov ecx,dword ptr [esi]
00401A6D FF 51 04 call dword ptr [ecx+4]
30: j = 3
31: k = CLng(i)
00401A70 8B 56 34 mov edx,dword ptr [esi+34h]
00401A73 C7 46 38 03 00 00 00 mov dword ptr [esi+38h],3
00401A7A 52 push edx
00401A7B FF 15 28 10 40 00 call dword ptr [__imp____vbaCyI4
(00401028)]
00401A81 89 46 3C mov dword ptr [esi+3Ch],eax
00401A84 89 56 40 mov dword ptr [esi+40h],edx
32: m = 9
00401A87 C7 46 44 09 00 00 00 mov dword ptr [esi+44h],9
33: End Sub
00401A8E C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
$L32:
00401A95 8B 45 08 mov eax,dword ptr [Me]
00401A98 50 push eax
00401A99 8B 08 mov ecx,dword ptr [eax]
00401A9B FF 51 08 call dword ptr [ecx+8]
00401A9E 8B 45 FC mov eax,dword ptr [ebp-4]
00401AA1 8B 4D EC mov ecx,dword ptr [ebp-14h]
00401AA4 5F pop edi
00401AA5 5E pop esi
00401AA6 64 89 0D 00 00 00 00 mov dword ptr fs:[0],ecx
00401AAD 5B pop ebx
00401AAE 8B E5 mov esp,ebp
00401AB0 5D pop ebp
00401AB1 C2 14 00 ret 14h

Situation 5: Assigning number 7 to a Currency variable:
=======================================================

Option Explicit

Dim i As Long
Dim j As Long
Dim k As Currency
Dim m As Long

Private Sub Form_Load()
'i = 7
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
j = 3
k = 7
m = 9
End Sub

29: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
00401A30 55 push ebp
00401A31 8B EC mov ebp,esp
00401A33 83 EC 0C sub esp,0Ch
00401A36 68 B6 10 40 00 push offset ___vbaExceptHandler
(004010b6)
00401A3B 64 A1 00 00 00 00 mov eax,fs:[00000000]
00401A41 50 push eax
00401A42 64 89 25 00 00 00 00 mov dword ptr fs:[0],esp
00401A49 83 EC 08 sub esp,8
00401A4C 53 push ebx
00401A4D 56 push esi
00401A4E 57 push edi
00401A4F 89 65 F4 mov dword ptr [ebp-0Ch],esp
00401A52 C7 45 F8 A8 10 40 00 mov dword ptr [ebp-8],offset
__imp___CIexp+38h (004010a8)
00401A59 8B 75 08 mov esi,dword ptr [Me]
00401A5C 8B C6 mov eax,esi
00401A5E 83 E0 01 and eax,1
00401A61 89 45 FC mov dword ptr [ebp-4],eax
00401A64 83 E6 FE and esi,0FFFFFFFEh
00401A67 56 push esi
00401A68 89 75 08 mov dword ptr [Me],esi
00401A6B 8B 0E mov ecx,dword ptr [esi]
00401A6D FF 51 04 call dword ptr [ecx+4]
30: j = 3
31: k = 7
00401A70 6A 07 push 7
00401A72 C7 46 38 03 00 00 00 mov dword ptr [esi+38h],3
00401A79 FF 15 28 10 40 00 call dword ptr [__imp_@__vbaCyI2
(00401028)]
00401A7F 89 46 3C mov dword ptr [esi+3Ch],eax
00401A82 89 56 40 mov dword ptr [esi+40h],edx
32: m = 9
00401A85 C7 46 44 09 00 00 00 mov dword ptr [esi+44h],9
33: End Sub
00401A8C C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
$L32:
00401A93 8B 45 08 mov eax,dword ptr [Me]
00401A96 50 push eax
00401A97 8B 10 mov edx,dword ptr [eax]
00401A99 FF 52 08 call dword ptr [edx+8]
00401A9C 8B 45 FC mov eax,dword ptr [ebp-4]
00401A9F 8B 4D EC mov ecx,dword ptr [ebp-14h]
00401AA2 5F pop edi
00401AA3 5E pop esi
00401AA4 64 89 0D 00 00 00 00 mov dword ptr fs:[0],ecx
00401AAB 5B pop ebx
00401AAC 8B E5 mov esp,ebp
00401AAE 5D pop ebp
00401AAF C2 14 00 ret 14h

Situation 6: Assigning number 33333 to a Currency variable:
===========================================================

Option Explicit

Dim i As Long
Dim j As Long
Dim k As Currency
Dim m As Long

Private Sub Form_Load()
'i = 7
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
j = 3
k = 33333
m = 9
End Sub




29: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
00401A30 55 push ebp
00401A31 8B EC mov ebp,esp
00401A33 83 EC 0C sub esp,0Ch
00401A36 68 B6 10 40 00 push offset ___vbaExceptHandler
(004010b6)
00401A3B 64 A1 00 00 00 00 mov eax,fs:[00000000]
00401A41 50 push eax
00401A42 64 89 25 00 00 00 00 mov dword ptr fs:[0],esp
00401A49 83 EC 08 sub esp,8
00401A4C 53 push ebx
00401A4D 56 push esi
00401A4E 57 push edi
00401A4F 89 65 F4 mov dword ptr [ebp-0Ch],esp
00401A52 C7 45 F8 A8 10 40 00 mov dword ptr [ebp-8],offset
__imp___CIexp+38h (004010a8)
00401A59 8B 75 08 mov esi,dword ptr [Me]
00401A5C 8B C6 mov eax,esi
00401A5E 83 E0 01 and eax,1
00401A61 89 45 FC mov dword ptr [ebp-4],eax
00401A64 83 E6 FE and esi,0FFFFFFFEh
00401A67 56 push esi
00401A68 89 75 08 mov dword ptr [Me],esi
00401A6B 8B 0E mov ecx,dword ptr [esi]
00401A6D FF 51 04 call dword ptr [ecx+4]
30: j = 3
31: k = 33333
00401A70 68 35 82 00 00 push 8235h
00401A75 C7 46 38 03 00 00 00 mov dword ptr [esi+38h],3
00401A7C FF 15 28 10 40 00 call dword ptr [__imp_@__vbaCyI4
(00401028)]
00401A82 89 46 3C mov dword ptr [esi+3Ch],eax
00401A85 89 56 40 mov dword ptr [esi+40h],edx
32: m = 9
00401A88 C7 46 44 09 00 00 00 mov dword ptr [esi+44h],9
33: End Sub
00401A8F C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
$L32:
00401A96 8B 45 08 mov eax,dword ptr [Me]
00401A99 50 push eax
00401A9A 8B 10 mov edx,dword ptr [eax]
00401A9C FF 52 08 call dword ptr [edx+8]
00401A9F 8B 45 FC mov eax,dword ptr [ebp-4]
00401AA2 8B 4D EC mov ecx,dword ptr [ebp-14h]
00401AA5 5F pop edi
00401AA6 5E pop esi
00401AA7 64 89 0D 00 00 00 00 mov dword ptr fs:[0],ecx
00401AAE 5B pop ebx
00401AAF 8B E5 mov esp,ebp
00401AB1 5D pop ebp
00401AB2 C2 14 00 ret 14h


Situation 7: Same as Situation 5, but using Long variable instead of 7:
=======================================================================

Option Explicit

Dim i As Long
Dim j As Long
Dim k As Currency
Dim m As Long

Private Sub Form_Load()
i = 7
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
j = 3
k = i
m = 9
End Sub





29: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
00401A30 55 push ebp
00401A31 8B EC mov ebp,esp
00401A33 83 EC 0C sub esp,0Ch
00401A36 68 B6 10 40 00 push offset ___vbaExceptHandler
(004010b6)
00401A3B 64 A1 00 00 00 00 mov eax,fs:[00000000]
00401A41 50 push eax
00401A42 64 89 25 00 00 00 00 mov dword ptr fs:[0],esp
00401A49 83 EC 08 sub esp,8
00401A4C 53 push ebx
00401A4D 56 push esi
00401A4E 57 push edi
00401A4F 89 65 F4 mov dword ptr [ebp-0Ch],esp
00401A52 C7 45 F8 A8 10 40 00 mov dword ptr [ebp-8],offset
__imp___CIexp+38h (004010a8)
00401A59 8B 75 08 mov esi,dword ptr [Me]
00401A5C 8B C6 mov eax,esi
00401A5E 83 E0 01 and eax,1
00401A61 89 45 FC mov dword ptr [ebp-4],eax
00401A64 83 E6 FE and esi,0FFFFFFFEh
00401A67 56 push esi
00401A68 89 75 08 mov dword ptr [Me],esi
00401A6B 8B 0E mov ecx,dword ptr [esi]
00401A6D FF 51 04 call dword ptr [ecx+4]
30: j = 3
31: k = i
00401A70 8B 56 34 mov edx,dword ptr [esi+34h]
00401A73 C7 46 38 03 00 00 00 mov dword ptr [esi+38h],3
00401A7A 52 push edx
00401A7B FF 15 28 10 40 00 call dword ptr [__imp_@__vbaCyI4
(00401028)]
00401A81 89 46 3C mov dword ptr [esi+3Ch],eax
00401A84 89 56 40 mov dword ptr [esi+40h],edx
32: m = 9
00401A87 C7 46 44 09 00 00 00 mov dword ptr [esi+44h],9
33: End Sub
00401A8E C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
$L32:
00401A95 8B 45 08 mov eax,dword ptr [Me]
00401A98 50 push eax
00401A99 8B 08 mov ecx,dword ptr [eax]
00401A9B FF 51 08 call dword ptr [ecx+8]
00401A9E 8B 45 FC mov eax,dword ptr [ebp-4]
00401AA1 8B 4D EC mov ecx,dword ptr [ebp-14h]
00401AA4 5F pop edi
00401AA5 5E pop esi
00401AA6 64 89 0D 00 00 00 00 mov dword ptr fs:[0],ecx
00401AAD 5B pop ebx
00401AAE 8B E5 mov esp,ebp
00401AB0 5D pop ebp
00401AB1 C2 14 00 ret 14h


Situation 8: Assigning number 7 declared as Currency Const to a Currency
variable:
==================================================================================

Option Explicit

Private Const C As Currency = 7

Dim i As Long
Dim j As Long
Dim k As Currency
Dim m As Long

Private Sub Form_Load()
'i = 7
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
j = 3
k = C
m = 9
End Sub


31: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
00401A10 55 push ebp
00401A11 8B EC mov ebp,esp
00401A13 83 EC 0C sub esp,0Ch
00401A16 68 B6 10 40 00 push offset ___vbaExceptHandler
(004010b6)
00401A1B 64 A1 00 00 00 00 mov eax,fs:[00000000]
00401A21 50 push eax
00401A22 64 89 25 00 00 00 00 mov dword ptr fs:[0],esp
00401A29 83 EC 08 sub esp,8
00401A2C 53 push ebx
00401A2D 56 push esi
00401A2E 57 push edi
00401A2F 89 65 F4 mov dword ptr [ebp-0Ch],esp
00401A32 C7 45 F8 A8 10 40 00 mov dword ptr [ebp-8],offset
__imp___CIexp+3Ch (004010a8)
00401A39 8B 75 08 mov esi,dword ptr [Me]
00401A3C 8B C6 mov eax,esi
00401A3E 83 E0 01 and eax,1
00401A41 89 45 FC mov dword ptr [ebp-4],eax
00401A44 83 E6 FE and esi,0FFFFFFFEh
00401A47 56 push esi
00401A48 89 75 08 mov dword ptr [Me],esi
00401A4B 8B 0E mov ecx,dword ptr [esi]
00401A4D FF 51 04 call dword ptr [ecx+4]
32: j = 3
33: k = C
00401A50 33 C0 xor eax,eax
00401A52 C7 46 3C 70 11 01 00 mov dword ptr [esi+3Ch],11170h
00401A59 C7 46 38 03 00 00 00 mov dword ptr [esi+38h],3
00401A60 89 46 40 mov dword ptr [esi+40h],eax
34: m = 9
00401A63 C7 46 44 09 00 00 00 mov dword ptr [esi+44h],9
35: End Sub
00401A6A 89 45 FC mov dword ptr [ebp-4],eax
$L32:
00401A6D 8B 45 08 mov eax,dword ptr [Me]
00401A70 50 push eax
00401A71 8B 10 mov edx,dword ptr [eax]
00401A73 FF 52 08 call dword ptr [edx+8]
00401A76 8B 45 FC mov eax,dword ptr [ebp-4]
00401A79 8B 4D EC mov ecx,dword ptr [ebp-14h]
00401A7C 5F pop edi
00401A7D 5E pop esi
00401A7E 64 89 0D 00 00 00 00 mov dword ptr fs:[0],ecx
00401A85 5B pop ebx
00401A86 8B E5 mov esp,ebp
00401A88 5D pop ebp
00401A89 C2 14 00 ret 14h


Situation 9: Using a formula:
=============================

Option Explicit

Private Const MAXDWORD As Currency = 4294967296#
Private Const C As Currency = 7

Dim i As Long
Dim j As Long
Dim k As Currency
Dim m As Long
Dim n As Long

Private Sub Form_Load()
i = 7
j = 3
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
n = 5
k = MAXDWORD * i + j
m = 9
End Sub


34: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
00401A70 55 push ebp
00401A71 8B EC mov ebp,esp
00401A73 83 EC 0C sub esp,0Ch
00401A76 68 B6 10 40 00 push offset ___vbaExceptHandler
(004010b6)
00401A7B 64 A1 00 00 00 00 mov eax,fs:[00000000]
00401A81 50 push eax
00401A82 64 89 25 00 00 00 00 mov dword ptr fs:[0],esp
00401A89 83 EC 08 sub esp,8
00401A8C 53 push ebx
00401A8D 56 push esi
00401A8E 57 push edi
00401A8F 89 65 F4 mov dword ptr [ebp-0Ch],esp
00401A92 C7 45 F8 A8 10 40 00 mov dword ptr [ebp-8],offset
__imp___CIexp+30h (004010a8)
00401A99 8B 75 08 mov esi,dword ptr [Me]
00401A9C 8B C6 mov eax,esi
00401A9E 83 E0 01 and eax,1
00401AA1 89 45 FC mov dword ptr [ebp-4],eax
00401AA4 83 E6 FE and esi,0FFFFFFFEh
00401AA7 56 push esi
00401AA8 89 75 08 mov dword ptr [Me],esi
00401AAB 8B 0E mov ecx,dword ptr [esi]
00401AAD FF 51 04 call dword ptr [ecx+4]
35: n = 5
36: k = MAXDWORD * i + j
00401AB0 8B 56 38 mov edx,dword ptr [esi+38h]
00401AB3 8B 3D 30 10 40 00 mov edi,dword ptr [__imp_@__vbaCyI4
(00401030)]
00401AB9 52 push edx
00401ABA C7 46 48 05 00 00 00 mov dword ptr [esi+48h],5
00401AC1 FF D7 call edi
00401AC3 52 push edx
00401AC4 50 push eax
00401AC5 8B 46 34 mov eax,dword ptr [esi+34h]
00401AC8 50 push eax
00401AC9 FF D7 call edi
00401ACB 52 push edx
00401ACC 50 push eax
00401ACD 68 10 27 00 00 push 2710h
00401AD2 6A 00 push 0
00401AD4 FF 15 08 10 40 00 call dword ptr [__imp_@__vbaCyMul
(00401008)]
00401ADA 52 push edx
00401ADB 50 push eax
00401ADC FF 15 18 10 40 00 call dword ptr [__imp____vbaCyAdd
(00401018)]
00401AE2 89 46 3C mov dword ptr [esi+3Ch],eax
00401AE5 89 56 40 mov dword ptr [esi+40h],edx
37: m = 9
00401AE8 C7 46 44 09 00 00 00 mov dword ptr [esi+44h],9
38: End Sub
00401AEF C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
$L32:
00401AF6 8B 45 08 mov eax,dword ptr [Me]
00401AF9 50 push eax
00401AFA 8B 08 mov ecx,dword ptr [eax]
00401AFC FF 51 08 call dword ptr [ecx+8]
00401AFF 8B 45 FC mov eax,dword ptr [ebp-4]
00401B02 8B 4D EC mov ecx,dword ptr [ebp-14h]
00401B05 5F pop edi
00401B06 5E pop esi
00401B07 64 89 0D 00 00 00 00 mov dword ptr fs:[0],ecx
00401B0E 5B pop ebx
00401B0F 8B E5 mov esp,ebp
00401B11 5D pop ebp
00401B12 C2 14 00 ret 14h



Situation 10: Using LSet
========================


Option Explicit

Private Const MAXDWORD As Currency = 4294967296#
Private Const c As Currency = 7

Private Type MungeCurr
Value As Currency
End Type

Private Type Munge2Long
LoValue As Long
HiValue As Long
End Type

Dim i As Long
Dim j As Long
Dim k As Currency
Dim m As Long
Dim n As Long

Private Sub Form_Load()
i = 7
j = 3
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
Dim Src As Munge2Long
Dim Dest As MungeCurr

n = 5
Src.LoValue = i
Src.HiValue = j
LSet Dest = Src
Dest.Value = Dest.Value * 10000
m = 9
End Sub



43: Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
00401A70 55 push ebp
00401A71 8B EC mov ebp,esp
00401A73 83 EC 0C sub esp,0Ch
00401A76 68 B6 10 40 00 push offset ___vbaExceptHandler
(004010b6)
00401A7B 64 A1 00 00 00 00 mov eax,fs:[00000000]
00401A81 50 push eax
00401A82 64 89 25 00 00 00 00 mov dword ptr fs:[0],esp
00401A89 83 EC 18 sub esp,18h
00401A8C 53 push ebx
00401A8D 56 push esi
00401A8E 57 push edi
00401A8F 89 65 F4 mov dword ptr [ebp-0Ch],esp
00401A92 C7 45 F8 A8 10 40 00 mov dword ptr [ebp-8],offset
__imp___CIexp+34h (004010a8)
00401A99 8B 75 08 mov esi,dword ptr [Me]
00401A9C 8B C6 mov eax,esi
00401A9E 83 E0 01 and eax,1
00401AA1 89 45 FC mov dword ptr [ebp-4],eax
00401AA4 83 E6 FE and esi,0FFFFFFFEh
00401AA7 56 push esi
00401AA8 89 75 08 mov dword ptr [Me],esi
00401AAB 8B 0E mov ecx,dword ptr [esi]
00401AAD FF 51 04 call dword ptr [ecx+4]
44: Dim Src As Munge2Long
45: Dim Dest As MungeCurr
46:
47: n = 5
48: Src.LoValue = i
00401AB0 8B 4E 34 mov ecx,dword ptr [esi+34h]
00401AB3 33 C0 xor eax,eax
00401AB5 89 45 DC mov dword ptr [Src],eax
00401AB8 33 D2 xor edx,edx
00401ABA 89 45 E0 mov dword ptr [ebp-20h],eax
00401ABD 89 55 E4 mov dword ptr [Dest],edx
00401AC0 89 4D DC mov dword ptr [Src],ecx
49: Src.HiValue = j
50: LSet Dest = Src
00401AC3 8D 45 DC lea eax,[Src]
00401AC6 8D 4D E4 lea ecx,[Dest]
00401AC9 89 55 E8 mov dword ptr [ebp-18h],edx
00401ACC 8B 56 38 mov edx,dword ptr [esi+38h]
00401ACF 50 push eax
00401AD0 51 push ecx
00401AD1 6A 08 push 8
00401AD3 C7 46 48 05 00 00 00 mov dword ptr [esi+48h],5
00401ADA 89 55 E0 mov dword ptr [ebp-20h],edx
00401ADD FF 15 10 10 40 00 call dword ptr [__imp_@__vbaCopyBytes
(00401010)]
51: Dest.Value = Dest.Value * 10000
00401AE3 8B 55 E8 mov edx,dword ptr [ebp-18h]
00401AE6 8B 45 E4 mov eax,dword ptr [Dest]
00401AE9 52 push edx
00401AEA 50 push eax
00401AEB 68 10 27 00 00 push 2710h
00401AF0 FF 15 50 10 40 00 call dword ptr [__imp_@__vbaCyMulI2
(00401050)]
00401AF6 89 45 E4 mov dword ptr [Dest],eax
00401AF9 89 55 E8 mov dword ptr [ebp-18h],edx
52: m = 9
00401AFC C7 46 44 09 00 00 00 mov dword ptr [esi+44h],9
53: End Sub
00401B03 C7 45 FC 00 00 00 00 mov dword ptr [ebp-4],0
$L34:
00401B0A 8B 45 08 mov eax,dword ptr [Me]
00401B0D 50 push eax
00401B0E 8B 08 mov ecx,dword ptr [eax]
00401B10 FF 51 08 call dword ptr [ecx+8]
00401B13 8B 45 FC mov eax,dword ptr [ebp-4]
00401B16 8B 4D EC mov ecx,dword ptr [ebp-14h]
00401B19 5F pop edi
00401B1A 5E pop esi
00401B1B 64 89 0D 00 00 00 00 mov dword ptr fs:[0],ecx
00401B22 5B pop ebx
00401B23 8B E5 mov esp,ebp
00401B25 5D pop ebp
00401B26 C2 14 00 ret 14h



From: Schmidt on

<bart.smissaert(a)gmail.com> schrieb im Newsbeitrag
news:903c92a8-06e1-4fe0-bdbd-a8e99c356509(a)d5g2000yqm.googlegroups.com...

> OK, can't remember getting wrong results with the code
> I posted last, but maybe I missed that particular range.
No need, that was right already...

> I take it it still makes sense to go with FindFirstFileW
Yep, definitely - this way you avoid unnecessary
WString-to-ANSI-to-WString conversions (before and
after the API-Call), VB-Strings *are* already "wide",
so with StrPtr (...) you're passing the right thing already.

Also just checked, if Ians multiply-version is faster
than the "copy-over" - in the IDE (or within VBA) it
is not faster - native compiled it is, but only a little bit.
What I forgot (since the Procedure-local type is a quite
large) - you can gain more than with the direct multiply,
if you make WFD static within the Function.
But all these effects are not that large compared with
the FindFirstFile-CallOverhead - you can measure
the differences of the "post-processing-lines" only,
if you comment out the FindFirstxxx related stuff,
setting only dummy-values on WFD.

Last thing (already mentioned by Nobody) -
LSet is a bit faster than CopyMemory, with that
change you will have a bit faster code, if native
compiled (compared with CopyMemory) - but if you
use the function mainly within VBA, then this change
does not worth it.

Olaf


From: Karl E. Peterson on
mayayana wrote:
> If it's worth being extremely efficient, couldn't
> you also skip the check of the nFileSizeLow? I
> assume it can't be < 0.

No, it definitely can, because it's unsigned. So anything between 2-4GB is
"negative" to VB.
--
..NET: It's About Trust!
http://vfred.mvps.org


From: Ralph on

"Nobody" <nobody(a)nobody.com> wrote in message
news:OsB16BHaKHA.5300(a)TK2MSFTNGP02.phx.gbl...
> "Nobody" <nobody(a)nobody.com> wrote in message
> news:Oxaa5GGaKHA.4920(a)TK2MSFTNGP04.phx.gbl...
> > Unfortunately, CConvert functions seem to be implemented in VB6 as
> > function calls.
>
> The above is incorrect. It seems that VB use the most "efficient" method
to
> convert from the given data type. For example, if you provide an integer
> values to CInteger or CLng, little or no conversion is done. But if you
> supply a String to CInteger or CLng, a function call is made to do the
> conversion. This seems like C++ function templates and/or function
> overloading where one or more version of the same function is written to
> support each data type, and the compiler picks the one that match the data
> type exactly, so the least amount of code is generated, and in cases where
> the data types are identical or very similar, a direct assignment is used,
> as if you didn't use the conversion function. VB compiler seems to
translate
> the code to intermediate code that is common with C++. VB's run time error
> handling is similar to C++ exception handling, they behave mostly in the
> same way.
>


It is good to see someone get curious and go peeking. (However, be prepared
to lose some hair if you plan to continue. <g>)

Here are some hints.

Don't confuse VB's handling of Variants with C++ templates. Your observation
that it appears similar is just that - an 'appearance'. Templates used by
C++ are code constructs that can used at compile time to instruct the
compiler on how to create a specific object. "Types" and how they are to be
handled is determined in VB/VBA when parsed and converted to opcode.

VB opcode is stack-oriented, not function-oriented - ie, there is far more
Forth in VB than C++. (Dusting off some Forth books is not a bad idea.)

Viewing VB generated code via the native code output is going to give a "C
view", since opcode is converted to preprocessed C before being submitted to
the C2 compiler for conversion to native code. You will see better what is
going on if you use several sources simultaneously.
1) Compile to P-Code and examine the output
2) Temporarily replace the C2.exe, second pass compiler, with an executable
that captures the command line and then exits.
This will leave "OBJ" files in the project folder. These are the
preprocessed C files created by VB.exe, less tainted by the C compiler's
additional passes. Gives a much better view of the multiple structs, caches
and dictionaries VB uses.
3) Load up WinDbg and view the actual "pcode" within the VB.exe. You can
also view pcode caches and dictionaries in the /temp folder. This is real
fun. <g>

There is a lot of interesting information available at the various Crack
sites. Most of the archives will deal with VB 3, as that is the last version
of VB it was practical to de-compile, but learning how pcode worked,
pre-VBA, is very useful. (In fact you might consider installing VB3 and
playing with it.)

However, before you go visiting, make sure you have an extremely isolated,
protected box, with nothing of value on it, or borrow someone else's. <g>

hth
-ralph