GTO
10-20-2015, 03:48 PM
Hello All :hi:,
After far too long, I have started trying to muddle my way through Win32 API Programming with Visual Basic (by Steven Roman).
I made it to the second example in the book before experiencing a different return than the author...:banghead:. Being as I'd rather not be missing stuff while trying to understand and learn, here's the examples, in case anyone can point out what I am missing (or if there is an error in the example of course).
In case there are followup questions, here are the declared functions thus far. I'll repost the lot of them only if there are added ones or changes...
In a Standard Module:
Option Explicit
'//*************************************************************************** **********//
'// All Basic Examples from Win32 API Programming with Visual Basic by Steven Roman
'// I tacked in (hopefully correct) conditional declarations in some cases, just in case
'// it would make any difference...
'//*************************************************************************** **********//
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
#If VBA7 Then
Const FAKE As Boolean = True
Declare PtrSafe Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (lpDest As Any, _
lpSource As Any, _
ByVal cbCopy As LongPtr)
Declare PtrSafe Sub VBGetTarget Lib "kernel32" _
Alias "RtlMoveMemory" (Target As Any, _
ByVal lPointer As LongPtr, _
ByVal cbCopy As LongPtr)
Declare PtrSafe Function GetClassName Lib "user32" _
Alias "GetClassNameA" (ByVal hWnd As LongPtr, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long _
) As Long
Declare PtrSafe Function FormatMessage Lib "kernel32" _
Alias "FormatMessageA" (ByVal dwFlags As Long, _
lpSource As Any, _
ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, _
ByVal lpBuffer As String, _
ByVal nSize As Long, _
Arguments As LongPtr _
) As Long
#Else
Const FAKE As Boolean = False
Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (lpDest As Any, _
lpSource As Any, _
ByVal cbCopy As Long)
Declare Sub VBGetTarget Lib "kernel32" _
Alias "RtlMoveMemory" (Target As Any, _
ByVal lPointer As Long, _
ByVal cbCopy As Long)
Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" (ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long _
) As Long
Declare Function FormatMessage Lib "kernel32" _
Alias "FormatMessageA" (ByVal dwFlags As Long, _
lpSource As Any, _
ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, _
ByVal lpBuffer As String, _
ByVal nSize As Long, _
ByVal Arguments As Long _
) As Long
#End If
Here are the two examples, the difference in return is shown in the code as comment.
In a Standard Module:
' PG 34, output a string by bytes
Sub CopyMemoryString_ANSI()
Dim sString As String
Dim aBytes(1 To 20) As Byte
Dim i As Integer
sString = "help"
CopyMemory aBytes(1), ByVal sString, LenB(sString)
For i = 1 To 20
Debug.Print aBytes(i);
Next
' Book shows output of: 104 101 108 112 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
' At least in WIN7, I get: 104 101 108 112 0 0 110 0 0 0 0 0 0 0 0 0 0 0 0 0
End Sub
Private Sub CopyMemoryString_Uni()
Dim sString As String
Dim aBytes(1 To 20) As Byte
Dim i As Integer
Dim lng As Long
sString = "help"
lng = StrPtr(sString) 'Get contents of BSTR variable
CopyMemory aBytes(1), ByVal lng, LenB(sString) 'Now copy as array
For i = 1 To 20
Debug.Print aBytes(i);
Next
' Book shows and I get correct output of: 104 0 101 0 108 0 112 0 0 0 0 0 0 0 0 0 0 0 0 0
End Sub
After far too long, I have started trying to muddle my way through Win32 API Programming with Visual Basic (by Steven Roman).
I made it to the second example in the book before experiencing a different return than the author...:banghead:. Being as I'd rather not be missing stuff while trying to understand and learn, here's the examples, in case anyone can point out what I am missing (or if there is an error in the example of course).
In case there are followup questions, here are the declared functions thus far. I'll repost the lot of them only if there are added ones or changes...
In a Standard Module:
Option Explicit
'//*************************************************************************** **********//
'// All Basic Examples from Win32 API Programming with Visual Basic by Steven Roman
'// I tacked in (hopefully correct) conditional declarations in some cases, just in case
'// it would make any difference...
'//*************************************************************************** **********//
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
#If VBA7 Then
Const FAKE As Boolean = True
Declare PtrSafe Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (lpDest As Any, _
lpSource As Any, _
ByVal cbCopy As LongPtr)
Declare PtrSafe Sub VBGetTarget Lib "kernel32" _
Alias "RtlMoveMemory" (Target As Any, _
ByVal lPointer As LongPtr, _
ByVal cbCopy As LongPtr)
Declare PtrSafe Function GetClassName Lib "user32" _
Alias "GetClassNameA" (ByVal hWnd As LongPtr, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long _
) As Long
Declare PtrSafe Function FormatMessage Lib "kernel32" _
Alias "FormatMessageA" (ByVal dwFlags As Long, _
lpSource As Any, _
ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, _
ByVal lpBuffer As String, _
ByVal nSize As Long, _
Arguments As LongPtr _
) As Long
#Else
Const FAKE As Boolean = False
Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (lpDest As Any, _
lpSource As Any, _
ByVal cbCopy As Long)
Declare Sub VBGetTarget Lib "kernel32" _
Alias "RtlMoveMemory" (Target As Any, _
ByVal lPointer As Long, _
ByVal cbCopy As Long)
Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" (ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long _
) As Long
Declare Function FormatMessage Lib "kernel32" _
Alias "FormatMessageA" (ByVal dwFlags As Long, _
lpSource As Any, _
ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, _
ByVal lpBuffer As String, _
ByVal nSize As Long, _
ByVal Arguments As Long _
) As Long
#End If
Here are the two examples, the difference in return is shown in the code as comment.
In a Standard Module:
' PG 34, output a string by bytes
Sub CopyMemoryString_ANSI()
Dim sString As String
Dim aBytes(1 To 20) As Byte
Dim i As Integer
sString = "help"
CopyMemory aBytes(1), ByVal sString, LenB(sString)
For i = 1 To 20
Debug.Print aBytes(i);
Next
' Book shows output of: 104 101 108 112 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
' At least in WIN7, I get: 104 101 108 112 0 0 110 0 0 0 0 0 0 0 0 0 0 0 0 0
End Sub
Private Sub CopyMemoryString_Uni()
Dim sString As String
Dim aBytes(1 To 20) As Byte
Dim i As Integer
Dim lng As Long
sString = "help"
lng = StrPtr(sString) 'Get contents of BSTR variable
CopyMemory aBytes(1), ByVal lng, LenB(sString) 'Now copy as array
For i = 1 To 20
Debug.Print aBytes(i);
Next
' Book shows and I get correct output of: 104 0 101 0 108 0 112 0 0 0 0 0 0 0 0 0 0 0 0 0
End Sub