copy CArray to memory

hi experts,  now I use vc++5
I have some questions about copy  data to cIipboard . I use RegisterClipboardFormat() to regist new type of
clipboard format for set clipboard know my clipboard format type.But I don't know how to copy class  CArray to memory (I store all of my data in class CArray) for use SetClipboardData() .
Please suggest me how to how to copy class  CArray to memory.
thank you for your suggest  
wanlopAsked:
Who is Participating?
 
ZoppoConnect With a Mentor Commented:
To copy memory of an CArray with type TYPE (i.e. CArray <TYPE, TYPE&>array), do:

int num = array.GetSize();
HGLOBAL handle = (HGLOBAL)GlobalAlloc( GMEM_MOVEABLE | GMEM_DDESHARE, num * sizeof( TYPE ) );
TYPE *dest = (TYPE *)GlobalLock( handle );
memcpy ( (void *)dest, (void *)array.GetData(), num * sizeof( TYPE ) );
GlobalUnlock();
SetClipboardData( ...// and so on

hope that helps,

ZOPPO
0
 
wanlopAuthor Commented:
hi zoppo,
please show how to get data back from clipboard if I do follow your suggest.
0
 
ZoppoCommented:
To get the data from clipboard do the following :

HGLOBAL handle = GetClipboardData( YOUR_OWN_TYPE );
int num = GlobalSize( handle ) / sizeof( TYPE );
TYPE *src = (TYPE *)GlobalLock( handle );
CArray <TYPE, TYPE&> array;
array.SetSize( num );
memcpy( (void *)array.GetData(), (void *)src, num * sizeof( TYPE ) );
GlobalUnlock( handle ); // sorry, in my previous post I forgot the _handle_
// and so on...

The only thing you must really take care is that TYPE is exact the same class ( or at least the same object size ) in both processes.

Note: You should do some validations on pointers. I leaved it out for outline.

hope that's it,

ZOPPO
0
 
wanlopAuthor Commented:
hi zoppo
   thank you for you suggest but I still have some problems with paste
data from clipboard  .
This is my source that I use  for copy  and paste from clipboard.  I assume my data is CString and store in CStringArray.
--  Copy to clipboard ---

CString a,b,c;
a = "1111";
b = "2222";
CStringArray    ptArray;
ptArray.Add(a);
ptArray.Add(b);
OpenClipboard();
int num = ptArray.GetSize();
HGLOBAL handle = (HGLOBAL)GlobalAlloc( GMEM_MOVEABLE | MEM_DDESHARE, num * sizeof(CString) );
CString *dest = (CString *)GlobalLock( handle );
memcpy ( (void *)dest, (void *)ptArray.GetData(), num * sizeof(CString) );
GlobalUnlock(handle);
UINT clipformat = ::RegisterClipboardFormat((LPCTSTR)"test");
EmptyClipboard();
SetClipboardData( clipformat,handle);
CloseClipboard();

---  Paste from clipboard ---
OpenClipboard();
HGLOBAL handle = GetClipboardData(clipformat);
int num = GlobalSize( handle ) / sizeof( CString );
CString *src = (CString *)GlobalLock( handle );
CStringArray  array;
array.SetSize( num );
memcpy( (void *)array.GetData(), (void *)src, num * sizeof( CString ) );
GlobalUnlock( handle );
CloseClipboard();

---------------------------------------------
When I paste data from clipboard.In CStringArray it's not contain
true data same I copy.  please suggest me again to fix this problem.

 











0
 
ZoppoCommented:
I think the problem is that you're trying to copy CString objects via memcpy. This cannot work because CString handles the string via the pointer LPTSTR m_pchData, so the pointer is copied instead of the data pointet to (the string itself).

The code I posted befor does not work with objects if the objects have dynamically allocated pointers as members, because the memory to copy must be in one block.

So you will have to convert the strings before copying them, i.e.:

int num, len, i;
CStringArray ptArray;
// conver to put on clipboard
CString str;
char *tmp;
num = ptArray.GetSize();
len = 0;
// calculate length needed for buffer
for ( i = 0; i < num; i++ )
   len += ptArray.GetAt( i ).GetLength() + 1;// + 1 to include terminating 0 char
// create buffer
HGLOBAL handle = (HGLOBAL)GlobalAlloc( GMEM_MOVEABLE | GMEM_DDESHARE, ( len + 1 )* sizeof( char ));
tmp = (char *)GlobalLock( handle );
for ( i = 0; i < num; i++ )
{
    str = ptArray.GetAt( i );
    strcpy( tmp, (LPCTSTR)str );
    tmp += str.GetLength() + 1;
}
*tmp = (char)0;// terminating buffer with '\0\0'
// now you have one block of memory of this form :
// tmp = "First String \0String Nr.2 \0String Nr. 3\0... ...\0Last String\0\0"
GlobalUnlock(handle);
UINT clipformat = ::RegisterClipboardFormat((LPCTSTR)"test");
EmptyClipboard();
SetClipboardData( clipformat,handle);
CloseClipboard();
// and so on...
-----------------------------------------------------
// conver to get from clipboard
// same vars declared as above

OpenClipboard();
handle = GetClipboardData(clipformat);
num = GlobalSize( handle ) / sizeof( CString );
tmp = (char *)GlobalLock( handle );
ptArray.RemoveAll();
for (;;)
{
    if ( (char)0 == *tmp )
        break;// terminatin '\0\0' reached
    str = CString( tmp );
    tmp += str.GetLength() + 1;
    ptArray.Add( str );
}
GlobalUnlock( handle );
CloseClipboard();
// and so on...
// I hope this does not contain bugs, I didn't test it. Perhaps you'll have to try a bit if it
// does not work properly. The serious thing is that you understand what happens, then
// you'll be able to find any bug.

if you have further questions, you can mail me directly at 'zoppo@cdsgmbh.de'.

ZOPPO
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.