copy CArray to  memory

Posted on 1999-01-20
Last Modified: 2013-11-20
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  
Question by:wanlop
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 31

Accepted Solution

Zoppo earned 60 total points
ID: 1328093
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 ) );
SetClipboardData( ...// and so on

hope that helps,


Author Comment

ID: 1328094
hi zoppo,
please show how to get data back from clipboard if I do follow your suggest.
LVL 31

Expert Comment

ID: 1328095
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,


Author Comment

ID: 1328096
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;
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) );
UINT clipformat = ::RegisterClipboardFormat((LPCTSTR)"test");
SetClipboardData( clipformat,handle);

---  Paste from clipboard ---
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 );

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.


LVL 31

Expert Comment

ID: 1328097
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"
UINT clipformat = ::RegisterClipboardFormat((LPCTSTR)"test");
SetClipboardData( clipformat,handle);
// and so on...
// conver to get from clipboard
// same vars declared as above

handle = GetClipboardData(clipformat);
num = GlobalSize( handle ) / sizeof( CString );
tmp = (char *)GlobalLock( handle );
for (;;)
    if ( (char)0 == *tmp )
        break;// terminatin '\0\0' reached
    str = CString( tmp );
    tmp += str.GetLength() + 1;
    ptArray.Add( str );
GlobalUnlock( handle );
// 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 ''.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question