BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Copy Files

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
Alexander Adam
Guest





PostPosted: Mon Aug 08, 2005 12:34 am    Post subject: Copy Files Reply with quote



hi!

I am trying to copy a list of files into the clipboard. In the following
is my code but it doesn't work (acess error with memory or something
that the CodeGuard tells me).. any clue or maybe some more simple code
to do what I want?

AnsiString ShellConcatFiles(TStrings* sourceFiles) {
AnsiString tmpStr = "";
for(int i=0; i<sourceFiles->Count; i++) {
tmpStr += sourceFiles->Strings[i];
tmpStr += '';
}
return tmpStr;
}

bool CopyFilesToClipboard(TStrings* sourceFiles)
{

AnsiString tmpList = ShellConcatFiles(sourceFiles);
char* FileList = new char[tmpList.Length() + 1];
try {
strcpy(FileList, tmpList.c_str());

int len;
BYTE* pData;
HGLOBAL hGbl;
DROPFILES hf;

if (!OpenClipboard(0))
return false;
EmptyClipboard();

hf.fNC = false;
hf.fWide = false;
hf.pFiles = sizeof(hf);
hf.pt.x = hf.pt.y = 0;

len = tmpList.Length() + 2;
hGbl = GlobalAlloc(GHND, sizeof(hf)+len);
pData = (BYTE*)GlobalLock(hGbl);
if (pData != NULL) {
memcpy(pData, &hf, sizeof(hf));
memcpy(pData+sizeof(hf), FileList, len);
GlobalUnlock(hGbl);
if (!SetClipboardData(CF_HDROP,hGbl)) {
GlobalFree(hGbl);
hGbl = NULL;
}
} else if(hGbl != NULL) {
GlobalFree(hGbl);
hGbl = NULL;
}
CloseClipboard();
return hGbl != NULL;
} __finally {
delete FileList;
}
}
Back to top
Ed Mulroy
Guest





PostPosted: Mon Aug 08, 2005 12:48 am    Post subject: Re: Copy Files Reply with quote



Check the source code you posted.

It is hard to decipher with all indenting removed but it looks as if the
'try' statement is terminated with

else if(hGbl != NULL)

.. Ed

Quote:
Alexander Adam wrote in message
news:42f6a8ab (AT) newsgroups (DOT) borland.com...

I am trying to copy a list of files into the clipboard. In the following
is my code but it doesn't work (acess error with memory or something that
the CodeGuard tells me).. any clue or maybe some more simple code to do
what I want?

AnsiString ShellConcatFiles(TStrings* sourceFiles) {
AnsiString tmpStr = "";
for(int i=0; i<sourceFiles->Count; i++) {
tmpStr += sourceFiles->Strings[i];
tmpStr += '';
}
return tmpStr;
}

bool CopyFilesToClipboard(TStrings* sourceFiles)
{

AnsiString tmpList = ShellConcatFiles(sourceFiles);
char* FileList = new char[tmpList.Length() + 1];
try {
strcpy(FileList, tmpList.c_str());

int len;
BYTE* pData;
HGLOBAL hGbl;
DROPFILES hf;

if (!OpenClipboard(0))
return false;
EmptyClipboard();

hf.fNC = false;
hf.fWide = false;
hf.pFiles = sizeof(hf);
hf.pt.x = hf.pt.y = 0;

len = tmpList.Length() + 2;
hGbl = GlobalAlloc(GHND, sizeof(hf)+len);
pData = (BYTE*)GlobalLock(hGbl);
if (pData != NULL) {
memcpy(pData, &hf, sizeof(hf));
memcpy(pData+sizeof(hf), FileList, len);
GlobalUnlock(hGbl);
if (!SetClipboardData(CF_HDROP,hGbl)) {
GlobalFree(hGbl);
hGbl = NULL;
}
} else if(hGbl != NULL) {
GlobalFree(hGbl);
hGbl = NULL;
}
CloseClipboard();
return hGbl != NULL;
} __finally {
delete FileList;
}
}



Back to top
Alexander Adam
Guest





PostPosted: Mon Aug 08, 2005 7:51 am    Post subject: Re: Copy Files Reply with quote



hi!

Quote:
Check the source code you posted.

It is hard to decipher with all indenting removed but it looks as if the
'try' statement is terminated with

else if(hGbl != NULL)

Well here in Thunderbird I still see the correct intending, wierd.
But the try statement is correctly terminated by the __finally, as this
code otherwise wouldn't even compile ;-)

thanks!
Alex

Back to top
JF Jolin
Guest





PostPosted: Mon Aug 08, 2005 4:32 pm    Post subject: Re: Copy Files Reply with quote



strcpy(FileList, tmpList.c_str());


copies only the first file of your list of files and variable FileList
isn't a double - null-terminated list of file names anymore



--
JF Jolin
Back to top
Alexander Adam
Guest





PostPosted: Tue Aug 09, 2005 3:30 pm    Post subject: Re: Copy Files Reply with quote

JF Jolin wrote:
Quote:


strcpy(FileList, tmpList.c_str());


copies only the first file of your list of files and variable FileList
isn't a double - null-terminated list of file names anymore



hmm okay.. not sure what exactly is wrong on that now.. could u give me

a hint?

thanks!

Back to top
Jonathan Benedicto
Guest





PostPosted: Tue Aug 09, 2005 4:28 pm    Post subject: Re: Copy Files Reply with quote

"Alexander Adam" <sdg (AT) dg (DOT) dg> wrote

Quote:
hi!

I am trying to copy a list of files into the clipboard. In the following
is my code but it doesn't work (acess error with memory or something that
the CodeGuard tells me).. any clue or maybe some more simple code to do
what I want?

I don't know anything about copying files to the clipboard, but I did
revise your code to be simpler, and I think that it might work.

// Untested.

AnsiString ShellConcatFiles( TStrings *sourceFiles )
{
AnsiString tmpStr;
for( int i = 0; i < sourceFiles->Count; ++i )
{
tmpStr += sourceFiles->Strings[i];
tmpStr += '';
}

return tmpStr;
}

bool CopyFilesToClipboard( TStrings *sourceFiles )
{
AnsiString tmpList = ShellConcatFiles( sourceFiles );
int len;
BYTE* pData;
HGLOBAL hGbl;
DROPFILES hf;

if( !OpenClipboard( 0 ) )
return false;

EmptyClipboard();

hf.fNC = false;
hf.fWide = false;
hf.pFiles = sizeof( hf );
hf.pt.x = hf.pt.y = 0;

len = tmpList.Length() + 2;
hGbl = GlobalAlloc(GHND, sizeof(hf) + len );
pData = (BYTE*)GlobalLock( hGbl );
if( pData != NULL )
{
memcpy(pData, &hf, sizeof( hf ));
memcpy(pData + sizeof( hf ), tmpList.c_str(), len );
GlobalUnlock( hGbl );

if( !SetClipboardData( CF_HDROP, hGbl ) )
{
GlobalFree(hGbl);
hGbl = NULL;
}
}
else if( hGbl != NULL )
{
GlobalFree(hGbl);
hGbl = NULL;
}

CloseClipboard();
return ( hGbl != NULL );
}

HTH

Jonathan



Back to top
JF Jolin
Guest





PostPosted: Tue Aug 09, 2005 6:15 pm    Post subject: Re: Copy Files Reply with quote

Alexander Adam <sdg (AT) dg (DOT) dg> wrote:

Quote:
hmm okay.. not sure what exactly is wrong on that now.. could u give me a
hint?

thanks!


When you use SetClipboardData(uFormat, hMem) with uFormat CF_HDROP then
hMem is the handle of a double - null-terminated list of file names.

Something like _T("C:\boot.ini" "C:\windows\win.ini")
so the end of the list is a double - null-terminated list of file names (ie
)

If you use an instruction like strcpy on such a list.
The destination will contain only the first file name (single null-
terminated).
Because strcpy copies until the first null character.

I wouldn't be surprise that code's corrections from J. Benedicto makes the
clipboard holding good drag 'n drop files to be used with DragQueryFile().


--
JF Jolin

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.