Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Assalam-o-Alaikum,

I right-click on a file and copied it, the filename is send to clipboard, now I want to get that string(filename or filePath) from the Clipboard.
Is there any function for getting filepath of the file copied to clipboard?
Posted
Comments
enhzflep 2-Jul-13 11:15am    
Yes.
MSDN will tell you what it is if you ask it.

In windows you can use GetClipboardData() to get the clipboard data.
C++
OpenClipboard( 0 );
HANDLE hClipboardText = GetClipboardData( CF_UNICODETEXT );

if( hClipboardText )
{
    HANDLE h= GlobalLock( hClipboardText );
    TCHAR* tcClipboardText = wcsdup( (TCHAR*)hClipboardText );
    GlobalUnlock( hClipboardText );
}
else
{
    printf( "Error:%d", GetLastError() );
}
CloseClipboard();


http://msdn.microsoft.com/en-us/library/windows/desktop/ms649039(v=vs.85).aspx
 
Share this answer
 
Comments
saad_lah 3-Jul-13 12:46pm    
That is what i want, but with fullpath name like c:\program\test.txt
how can i get the full path with file name which i copied to clipboard?
Santhosh G_ 3-Jul-13 12:50pm    
Above code will provide the contents of the clipboard as string.
Clipboard should contain entire file path.
saad_lah 3-Jul-13 12:57pm    
got it ! thanx :)
You guys are awesome!
You need to use the following APIs -
OpenClipboard
GetClipboardData
GlobalLock // Call GlobalLock on the the handle returned by GetClipboardData
// Copy data in pointer returned by GlobalLock
GlobalUnlock
CloseClipboard
 
Share this answer
 
Comments
saad_lah 3-Jul-13 12:46pm    
That is what i want, but with fullpath name like c:\program\test.txt
how can i get the full path with file name which i copied to clipboard?
When copying files in the Windows Explorer, the names are stored in multiple clipboard formats. To retrieve the names you may use the pre-defined CF_HDROP format. Other formats are not pre-defined, but registered like CFSTR_FILENAMEW (single file name / first of list as wide char string), CFSTR_FILENAMEA (single file name / first of list as ANSI string in 8.3 format), and CFSTR_SHELLIDLIST. See also Shell Clipboard Formats[^] in the MSDN.

To get the first name from a CF_HDROP object:
C++
TCHAR lpszFileName[MAX_PATH];
OpenClipboard();
HGLOBAL hGlobal = (HGLOBAL)GetClipboardData(CF_HDROP);
if (hGlobal)
{
    HDROP hDrop = (HDROP)GlobalLock(hGlobal);
    if (hDrop)
    {
        DragQueryFile(hDrop, 0, lpszFileName, MAX_PATH);
        GlobalUnlock(hGlobal);
    }
}
CloseClipboard();
 
Share this answer
 
Comments
saad_lah 3-Jul-13 12:46pm    
That is what i want, but with fullpath name like c:\program\test.txt
how can i get the full path with file name which i copied to clipboard?
Jochen Arndt 3-Jul-13 12:50pm    
The code snippet will give you the full name.
saad_lah 3-Jul-13 12:52pm    
code snippet?
Jochen Arndt 3-Jul-13 12:57pm    
The code posted in my answer. Snippet beacuse it has been (or looks like) cutted (snipped) out from some sources.
saad_lah 3-Jul-13 12:57pm    
got it ! thanx :)
You guys are awesome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900