Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages
Started by Sky
Sun, 04 May 2003 02:03
How to get the actual size of a pointed object
Author: Sky
Date: Sun, 04 May 2003 02:03
Date: Sun, 04 May 2003 02:03
7 lines
1126 bytes
1126 bytes
hi all, i want transfer some memory by WM_COPYDATA, but i don't know how to get the actual size of a point, for example, i want transfer a TStrings *strs, but how can i get the size of strs? Any hint? TIA Sky
Re: How to get the actual size of a pointed object
Author: Rob
Date: Sun, 04 May 2003 05:44
Date: Sun, 04 May 2003 05:44
21 lines
1869 bytes
1869 bytes
"Sky" <huangc913@msn.com> wrote in message news:3eb47518@newsgroups.borland.com... > hi all, i want transfer some memory by WM_COPYDATA, > but i don't know how to get the actual size of a point, > for example, i want transfer a TStrings *strs, > but how can i get the size of strs? > Any hint? TIA > TStrings, as far as C++ is concerned, is a class type with virtual functions [in reality it is a Delphi type, but those details shouldn't matter to you]. Basically, you should not try to copy the object directly, but need to represent the data *within it* as an array of char to send it with WM_COPYDATA. I suggest you read up on various properties of TStrings (Count, Strings) first in the online help. As the Strings property allows acccess to AnsiStrings (which are also a non-trivial class type) you will need to read up on that type as well.
Re: How to get the actual size of a pointed object
Author: Remy Lebeau (TeamB)
Date: Sun, 04 May 2003 07:17
Date: Sun, 04 May 2003 07:17
103 lines
3608 bytes
3608 bytes
"Sky" <huangc913@msn.com> wrote in message
news:3eb47518@newsgroups.borland.com...
> hi all, i want transfer some memory by WM_COPYDATA,
> but i don't know how to get the actual size of a point,
> for example, i want transfer a TStrings *strs,
> but how can i get the size of strs?
You can't pass TStrings directly with WM_COPYDATA. You'll have to first
extract the raw data from the TStrings into a separate buffer prior to then
sending that buffer using WM_COPYDATA, and then on the receiving end it will
have to read the data and translate it back into a TStrings.
Two possible implementation are as follows:
1) the easy way
// sending side...
TStrings *MyList = whatever;
AnsiString buffer = MyList->CommaText;
COPYDATASTRUCT cds = {0};
cds.dwData = MyUniqueValue;
cds.cbData = buffer.Length();
cds.lpData = buffer.c_str();
SendMessage(DestWindow, WM_COPYDATA, 0, (LPARAM)&cds);
// receiving side...
TStrings *MyList = whatever;
COPYDATASTRUCT *cds = (COPYDATASTRUCT*)lParam;
if( cds->dwData == MyUniqueValue )
MyList->CommaText = AnsiString((char*)cds->lpData,
(int)cds->cbData);
2) the long way
// sending side...
TStrings *MyList = whatever;
DWORD dwSize = sizeof(int);
int iCount = MyList->Count;
for(int i = 0; i < iCount; ++i)
size += ( sizeof(int) + (MyList->Strings[i].Length() *
sizeof(char)) );
char *buffer = new char[dwSize];
char *ptr = buffer;
memcpy(ptr, &iCount, sizeof(int));
ptr += sizeof(int);
for(int i = 0; i < iCount; ++i)
{
AnsiString str = MyList->Strings[i];
int len = ( str.Length() * sizeof(char) );
memcpy(ptr, &len, sizeof(int));
ptr += sizeof(int);
memcpy(ptr, str.c_str(), len);
ptr += len;
}
COPYDATASTRUCT cds = {0};
cds.dwData = MyUniqueValue;
cds.cbData = size;
cds.lpData = buffer;
SendMessage(DestWindow, WM_COPYDATA, 0, (LPARAM)&cds);
delete[] buffer;
// receiving side...
COPYDATASTRUCT *cds = (COPYDATASTRUCT*)lParam;
if( cds->dwData == MyUniqueValue )
{
char *ptr = (char*)cds->lpData;
TStrings *MyList = whatever;
MyList->Clear();
int iCount;
memcpy(&iCount, ptr, sizeof(int));
ptr += sizeof(int);
for(int i = 0; x < iCount; x++)
{
int len;
memcpy(&len, ptr, sizeof(int));
ptr += sizeof(int);
MyList->Add(AnsiString(ptr, len));
ptr += ( len * sizeof(char) );
}
}
Gambit
Re: How to get the actual size of a pointed object
Author: Sky
Date: Tue, 06 May 2003 11:07
Date: Tue, 06 May 2003 11:07
1 lines
1013 bytes
1013 bytes
Thanks! Remy and Rob.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads