|
-
Jun 10th, 2010, 09:07 AM
#1
Thread Starter
Evil Genius
[RESOLVED] [Excel 2003 VBA]: CopyMemory with Objects
Hi everyone,
Can someone lend me a hand & tell me what I'm doing wrong here please?? I'm trying to use the CopyMemory API call in Excel, but without any luck. Could be the object length perhaps? (Len & LenB don't seem to want to work with an object type here)...
Code:
'In a module...
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Sub testit()
Dim classInstance1 As Class1
Dim classInstance2 As Class1
Set classInstance1 = New Class1
classInstance1.AStringValue = "hello world"
CopyMemory ObjPtr(classInstance2), ObjPtr(classInstance1), 4
classInstance1.AStringValue = "REPLACED"
MsgBox classInstance1.AStringValue & " -- " & classInstance2.AStringValue
End Sub
Code:
'In a class (Class1)...
Private m_stringValue As String
Public Property Get AStringValue() As String
AStringValue = m_stringValue
End Property
Public Property Let AStringValue(ByVal newString As String)
m_stringValue = newString
End Property
-
Jun 10th, 2010, 04:38 PM
#2
Re: [Excel 2003 VBA]: CopyMemory with Objects
' Copy the first 1000 elements of array a() to b()
' both arrays must be of the same type, and can't be objects
' nor variable-length strings
CopyMemory b(0), a(0), 1000 * Len(a(0))
from http://www.devx.com/vb2themax/Tip/18519
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 10th, 2010, 04:45 PM
#3
Thread Starter
Evil Genius
Re: [Excel 2003 VBA]: CopyMemory with Objects
Hi guys, and thanks for the responses (the earlier one which someone deleted too). Seems the 1st argument should have been an object & not a pointer was the main reason here. All hunky-dory now! This code works if anyone finds it useful for the future...
Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Sub testit()
Dim classInstance1 As Class1
Dim classInstance2 As Object
Set classInstance1 = New Class1
classInstance1.AStringValue = "hello world"
CopyMemory classInstance2, ObjPtr(classInstance1), 4
MsgBox "classInstance1.AStringValue: " & classInstance1.AStringValue
MsgBox "classInstance2.AStringValue: " & classInstance2.AStringValue
CopyMemory classInstance1, CLng(0), 4
Set classInstance1 = Nothing
MsgBox "classInstance2.AStringValue: " & classInstance2.AStringValue
CopyMemory classInstance2, CLng(0), 4
Set classInstance2 = Nothing
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|