Solved

copy CArray to  memory

Posted on 1999-01-20
5
401 Views
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  
0
Comment
Question by:wanlop
  • 3
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
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 ) );
GlobalUnlock();
SetClipboardData( ...// and so on

hope that helps,

ZOPPO
0
 

Author Comment

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

Expert Comment

by:Zoppo
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,

ZOPPO
0
 

Author Comment

by:wanlop
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;
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
 
LVL 31

Expert Comment

by:Zoppo
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"
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
deburging in oracle form 12 105
twoTwo  challenge 35 108
sumHeights2  challenge 7 117
Unix Command -- Challenging  question 7 96
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 …
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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