Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

I still need to know how the copymemory to an object works

I can give you the answer as long as you're happy with a byte array you get
from the clipboard. I have the same problem as you, I worked everything out
but I still have some problems with the copymemory function. Just have a
look at the code and if you find out something that will set the clipboard
object to an object in stead of a byte array, please let me now.

Private Declare Function CloseClipboard Lib "USER32" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest
As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Declare Function EmptyClipboard Lib "USER32" () As Long
Private Declare Function GetClipboardData Lib "USER32" (ByVal wFormat As
Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As
Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As
Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As
Long
Private Declare Function IsClipboardFormatAvailable Lib "USER32" (ByVal
wFormat As Long) As Long
Private Declare Function OpenClipboard Lib "USER32" (ByVal hWnd As Long) As
Long
Private Declare Function RegisterClipboardFormat Lib "USER32" Alias
"RegisterClipboardFormatA" (ByVal lpString As String) As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As
Long, ByVal hMem As Long) As Long
Public Declare Function RegisterClipboardFormat Lib "user32" Alias
"RegisterClipboardFormatA" (ByVal lpString As String) As Integer

Private Sub Command1_Click()
Dim MyObject as <Your object Name>
set MyObject = New <Your object Name>
Call RegisterClipboardFormat("<Your object Name>")
SetToClipBoard (MyObject)
End Sub

Public Sub SetToClipBoard(MyObject as <Your object Name>)
Dim tmpByteArray as Byte()
Dim lID As Long
lID = GetClipboardIDForCustomFormat("<Your object Name>")
If (lID <> 0) Then
OpenClipboard 0
EmptyClipBoard
CopyMemory tmpByteArray, MyObject, 4
SetClipboardData lID, tmpByteArray
CloseClipBoard
End If
End Sub

Private Sub Command2_Click()
Dim lID As Long
Dim sText As String
Dim MyObject as Byte()

lID = GetClipboardIDForCustomFormat("<Your object Name>")
If (lID <> 0) Then
MyObject = GetClipboardDataAsByteArray(Me.hWnd, lID)
End If

End Sub

Public Function GetClipboardIDForCustomFormat(ByVal sName As String) As Long
Dim wFormat As Long
wFormat = RegisterClipboardFormat(sName & Chr$(0))
If (wFormat > &HC000&) Then
GetClipboardIDForCustomFormat = wFormat
End If
End Function

Public Function GetClipboardDataAsByteArray(ByVal hWndOwner As Long, ByVal
lFormatId As Long) As Variant
Dim bData() As Byte
Dim hMem As Long
Dim lSize As Long
Dim lPtr As Long

' Open the clipboard for access:
If (OpenClipboard(hWndOwner)) Then
' Check if this data format is available:
If (IsClipboardFormatAvailable(lFormatId) <> 0) Then
' Get the memory handle to the data:
hMem = GetClipboardData(lFormatId)
If (hMem <> 0) Then
' Get the size of this memory block:
lSize = GlobalSize(hMem)
If (lSize > 0) Then
' Get a pointer to the memory:
lPtr = GlobalLock(hMem)
If (lPtr <> 0) Then
' Resize the byte array to hold the data:
ReDim bData(0 To lSize - 1) As Byte
' Copy from the pointer into the array:
CopyMemory bdata(0), byval lPtr, lsize

' Unlock the memory block:

GlobalUnlock hMem
' Now return the data as a string:
GetClipboardDataAsByteArray = bData
End If
End If
End If
End If
CloseClipboard
End If

End Function

"Tiger" <tong5@hotmai.com> wrote in message
news:356E9DF2.88EBF6FC@hotmai.com...
> Hello,
>
> Im writing a application using VB. I want to copy a use defined class
> to clipboard and copy it from clipboard. I know it can be done using
> Windows API, but how?
>
> Any help would be greatly appreciated.
>
> Thanks,
>
> Gao Junfeng
>
> gaojunf@hotmail.com
>
[4416 byte] By [Floris De Paepe] at [2007-11-10 0:20:29]
# 1 Re: I still need to know how the copymemory to an object works
I thought that it had already been stated in this newsgroup why trying to
copy an object like this is a bad idea. In your code you seem to be
creating a byte array, but haven't even dimensioned it before the call to
copy memory. Even so, you would only be copying a pointer to the object, so
a long would be more appropiate. Now if you did set the clipboard data to
the Value of that long (ie the pointer to the object)
you have to complete the operation and close the clipboard before your
program is allowed to access the object again. And when finished an all
those errors fixed you will have achieved the same as just using
Set MyObject2 = MyObject1

"Floris De Paepe" <Flopae@softcell.be> wrote in message
news:38d7ab09$1@news.dev-archive.com...
> I can give you the answer as long as you're happy with a byte array you
get
> from the clipboard. I have the same problem as you, I worked everything
out
> but I still have some problems with the copymemory function. Just have a
> look at the code and if you find out something that will set the clipboard
> object to an object in stead of a byte array, please let me now.

> Public Sub SetToClipBoard(MyObject as <Your object Name>)
> Dim tmpByteArray as Byte()
> Dim lID As Long
> lID = GetClipboardIDForCustomFormat("<Your object Name>")
> If (lID <> 0) Then
> OpenClipboard 0
> EmptyClipBoard
> CopyMemory tmpByteArray, MyObject, 4
> SetClipboardData lID, tmpByteArray
> CloseClipBoard
> End If
> End Sub
>
Bill McCarthy at 2007-11-11 20:03:30 >
# 2 Re: I still need to know how the copymemory to an object works
I thought that it had already been stated in this newsgroup why trying to
copy an object like this is a bad idea. In your code you seem to be
creating a byte array, but haven't even dimensioned it before the call to
copy memory. Even so, you would only be copying a pointer to the object, so
a long would be more appropiate. Now if you did set the clipboard data to
the Value of that long (ie the pointer to the object)
you have to complete the operation and close the clipboard before your
program is allowed to access the object again. And when finished an all
those errors fixed you will have achieved the same as just using
Set MyObject2 = MyObject1

"Floris De Paepe" <Flopae@softcell.be> wrote in message
news:38d7ab09$1@news.dev-archive.com...
> I can give you the answer as long as you're happy with a byte array you
get
> from the clipboard. I have the same problem as you, I worked everything
out
> but I still have some problems with the copymemory function. Just have a
> look at the code and if you find out something that will set the clipboard
> object to an object in stead of a byte array, please let me now.

> Public Sub SetToClipBoard(MyObject as <Your object Name>)
> Dim tmpByteArray as Byte()
> Dim lID As Long
> lID = GetClipboardIDForCustomFormat("<Your object Name>")
> If (lID <> 0) Then
> OpenClipboard 0
> EmptyClipBoard
> CopyMemory tmpByteArray, MyObject, 4
> SetClipboardData lID, tmpByteArray
> CloseClipBoard
> End If
> End Sub
>
Bill McCarthy at 2007-11-11 20:04:30 >
# 3 Re: I still need to know how the copymemory to an object works
Sorry I was using my wrong code for the setClipboardData. I tried that out
and of course you will have noticed that it doesnt. It's not a good way of
doing it in the first place. My real problem however is not in setting it to
the clipboard but getting it from the clipboard and there I would like some
help.
Here's the working code:
Public Sub SetToClipBoard(MyObject As Test)
Dim lID As Long
Dim lPtr As Long
lID = GetClipboardIDForCustomFormat("Test")
If (lID <> 0) Then
OpenClipboard 0
EmptyClipboard
SetClipboardData lID, lPtr
CloseClipboard
End If
End Sub

"Bill McCarthy" <billmcc@gsat.edu.au> wrote in message
news:38d7b48e@news.dev-archive.com...
> I thought that it had already been stated in this newsgroup why trying to
> copy an object like this is a bad idea. In your code you seem to be
> creating a byte array, but haven't even dimensioned it before the call to
> copy memory. Even so, you would only be copying a pointer to the object,
so
> a long would be more appropiate. Now if you did set the clipboard data to
> the Value of that long (ie the pointer to the object)
> you have to complete the operation and close the clipboard before your
> program is allowed to access the object again. And when finished an all
> those errors fixed you will have achieved the same as just using
> Set MyObject2 = MyObject1
>
>
> "Floris De Paepe" <Flopae@softcell.be> wrote in message
> news:38d7ab09$1@news.dev-archive.com...
> > I can give you the answer as long as you're happy with a byte array you
> get
> > from the clipboard. I have the same problem as you, I worked everything
> out
> > but I still have some problems with the copymemory function. Just have a
> > look at the code and if you find out something that will set the
clipboard
> > object to an object in stead of a byte array, please let me now.
>
> > Public Sub SetToClipBoard(MyObject as <Your object Name>)
> > Dim tmpByteArray as Byte()
> > Dim lID As Long
> > lID = GetClipboardIDForCustomFormat("<Your object Name>")
> > If (lID <> 0) Then
> > OpenClipboard 0
> > EmptyClipBoard
> > CopyMemory tmpByteArray, MyObject, 4
> > SetClipboardData lID, tmpByteArray
> > CloseClipBoard
> > End If
> > End Sub
> >
>
>
>
Floris De Paepe at 2007-11-11 20:05:34 >
# 4 Re: I still need to know how the copymemory to an object works
Sorry I was using my wrong code for the setClipboardData. I tried that out
and of course you will have noticed that it doesnt. It's not a good way of
doing it in the first place. My real problem however is not in setting it to
the clipboard but getting it from the clipboard and there I would like some
help.
Here's the working code:
Public Sub SetToClipBoard(MyObject As Test)
Dim lID As Long
Dim lPtr As Long
lID = GetClipboardIDForCustomFormat("Test")
If (lID <> 0) Then
OpenClipboard 0
EmptyClipboard
SetClipboardData lID, lPtr
CloseClipboard
End If
End Sub

"Bill McCarthy" <billmcc@gsat.edu.au> wrote in message
news:38d7b48e@news.dev-archive.com...
> I thought that it had already been stated in this newsgroup why trying to
> copy an object like this is a bad idea. In your code you seem to be
> creating a byte array, but haven't even dimensioned it before the call to
> copy memory. Even so, you would only be copying a pointer to the object,
so
> a long would be more appropiate. Now if you did set the clipboard data to
> the Value of that long (ie the pointer to the object)
> you have to complete the operation and close the clipboard before your
> program is allowed to access the object again. And when finished an all
> those errors fixed you will have achieved the same as just using
> Set MyObject2 = MyObject1
>
>
> "Floris De Paepe" <Flopae@softcell.be> wrote in message
> news:38d7ab09$1@news.dev-archive.com...
> > I can give you the answer as long as you're happy with a byte array you
> get
> > from the clipboard. I have the same problem as you, I worked everything
> out
> > but I still have some problems with the copymemory function. Just have a
> > look at the code and if you find out something that will set the
clipboard
> > object to an object in stead of a byte array, please let me now.
>
> > Public Sub SetToClipBoard(MyObject as <Your object Name>)
> > Dim tmpByteArray as Byte()
> > Dim lID As Long
> > lID = GetClipboardIDForCustomFormat("<Your object Name>")
> > If (lID <> 0) Then
> > OpenClipboard 0
> > EmptyClipBoard
> > CopyMemory tmpByteArray, MyObject, 4
> > SetClipboardData lID, tmpByteArray
> > CloseClipBoard
> > End If
> > End Sub
> >
>
>
>
Floris De Paepe at 2007-11-11 20:06:39 >