Article View: borland.public.cppbuilder.nativeapi
Article #16660Re: How to get the actual size of a pointed object
From: 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
Technical Details
Message-ID:
<3eb4be2b$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailReferences:
<3eb47518@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,843f8a38f40cb88f X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-05-04 00:28:12 PST From: "Remy Lebeau \(TeamB\)" <gambit47@yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3eb47518@newsgroups.borland.com> Subject: Re: How to get the actual size of a pointed object Date: Sun, 4 May 2003 00:17:31 -0700 Lines: 105 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 207.175.226.26 Message-ID: <3eb4be2b$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1052032555 207.175.226.26 (4 May 2003 00:15:55 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail