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 30

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 30

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 30

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hibernate methods 2 58
ffmpeg - "rtsp://...... Operation not permitted" 4 68
sumHeights  challenge 17 60
Not needed 13 54
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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 …
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now