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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 85
string initialization in java 11 110
either24  challenge 19 90
zeroMAx challenge 20 87
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now