Am I Incorrectly using the Windows Clipboard? - c++

I have some code to copy and paste:
void WinClipboard::copy( const std::string& input )
{
LPWSTR lptstrCopy;
HGLOBAL hglbCopy;
std::wstring text;
text = _winUTF8ToUTF16(input);
// Open the clipboard, and empty it.
if (!OpenClipboard(NULL))
return;
EmptyClipboard();
// Allocate a global memory object for the text.
hglbCopy = GlobalAlloc(GMEM_MOVEABLE,
((text.length() + 1) * sizeof(WCHAR)));
if (hglbCopy == NULL)
{
CloseClipboard();
return;
}
// Lock the handle and copy the text to the buffer.
lptstrCopy = (LPWSTR)GlobalLock(hglbCopy);
memcpy(lptstrCopy, text.c_str(),
(text.length() + 1) * sizeof(WCHAR) );
lptstrCopy[(text.length() + 1) * sizeof(WCHAR)] = (WCHAR) 0; // null character
GlobalUnlock(hglbCopy);
// Place the handle on the clipboard.
SetClipboardData(CF_UNICODETEXT, hglbCopy);
// Close the clipboard.
CloseClipboard();
}
std::string WinClipboard::paste()
{
HGLOBAL hglb;
LPWSTR lptstr;
std::string result;
std::wstring input;
// get the clipboard text.
if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
return result;
if (!OpenClipboard(NULL))
return result;
hglb = GetClipboardData(CF_UNICODETEXT);
if (hglb != NULL)
{
lptstr = (LPWSTR)GlobalLock(hglb);
if (lptstr != NULL)
{
GlobalUnlock(hglb);
input = lptstr;
result = _winUTF16ToUTF8(input);
}
CloseClipboard();
}
return result;
}
It works great except, when I quickly do CTRL C then CTRL-V (essentially calling the above copy and paste functions) the entire application freezes.
Am I forgetting to check for something or forgetting to release a resource?

I see two problems in your paste() function:
1) it is calling GlobalUnlock() before assigning the clipboard data to your std::wstring variable. You need to reverse those operations - call GlobalUnlock() after copying the data, not before.
2) it is not calling CloseClipboard() if GetClipboardData() fails.

Another "problem".
In the copy function:
The line
lptstrCopy[(text.length() + 1) * sizeof(WCHAR)] = (WCHAR) 0;
should be
lptstrCopy[text.length() + 1] = (WCHAR) 0;
to avoid the overflow/heap corruption.

Related

LPSTREAM unable to read into CString

I'm attempting to read text into a CString using an LPSTREAM, but it doesn't seem to be working correctly, here is the code that I'm calling:
static HRESULT UTL_ReadStreamTxt(MyStorage* pSrcStg, const char* pszStream, CString* myCStr, int size)
{
HRESULT hrRet = STG_E_INVALIDPARAMETER;
LPSTREAM lpSrc = NULL;
ULONG ul;
TRY
{
USES_CONVERSION;
HRESULT hrSrc = pSrcStg->GetStg()->OpenStream(CT2COLE(strSrc),
NULL,
STGM_READ | STGM_SHARE_EXCLUSIVE,
0,
&lpSrc);
if (hrSrc != NOERROR)
{
hrRet = hrSrc;
}
else
{
hrRet = lpSrc->Read(&myCStr, size, NULL); // Read into CString
}
}
CATCH_ALL(e)
{
hrRet = STG_E_UNKNOWN;
}
END_CATCH_ALL
_AfxRelease((LPUNKNOWN*)&lpSrc);
return hrRet;
}
When it reads into the string, Visual Studio says that the data in the CString is corrupted.
The compound storage's stream contents are the following:
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
I'm not entirely sure I'm using Read() correctly, How do I fix this issue?
The main problem is that you are passing a bad pointer to Read(). You are passing the memory address of the myCStr parameter itself, not the address of the CString being pointed at, or more accurately, the memory address of a character buffer that the CString owns. The code compiles only because Read() takes a simple void* pointer to a buffer, and any pointer is implicitly convertible to void*.
Also note that CString is based on TCHAR, which maps to either char or wchar_t depending on whether you are compiling your project for ANSI/MBCS or Unicode. So, reading from the stream directly into a CString will only work correctly if:
the stream contains ANSI characters and TCHAR maps to char.
the stream contains UTF-16 characters and TCHAR maps to wchar_t.
If the stream's character type does not match the character type used by CString, you would have to first read the stream into an intermediate buffer and then convert that to TCHAR before it can be stored in the CString.
Try something more like this:
static HRESULT UTL_ReadStreamTxt(MyStorage* pSrcStg, const char* pszStream, CString* myCStr, int size)
{
HRESULT hrRet = STG_E_INVALIDPARAMETER;
LPSTREAM lpSrc = NULL;
ULONG ul;
LPVOID buffer;
TRY
{
USES_CONVERSION;
HRESULT hrSrc = pSrcStg->GetStg()->OpenStream(CT2COLE(strSrc),
NULL,
STGM_READ | STGM_SHARE_EXCLUSIVE,
0,
&lpSrc);
if (hrSrc != S_OK)
{
hrRet = hrSrc;
}
else
{
// if the stream's character type matches TCHAR...
buffer = myCStr->GetBuffer(size / sizeof(TCHAR));
hrRet = lpSrc->Read(buffer, size, &ul);
myCStr->ReleaseBuffer(ul / sizeof(TCHAR));
// else, if the stream's character type is 'char' and TCHAR is 'wchar_t'...
CStringA tmp;
buffer = tmp.GetBuffer(size);
hrRet = lpSrc->Read(buffer, size, &ul);
tmp.ReleaseBuffer(ul);
*myCStr = CString((LPSTR)tmp, tmp.GetLength());
// else, if the stream's character type is 'wchar_t' and TCHAR is 'char'...
CStringW tmp;
buffer = tmp.GetBuffer(size / sizeof(wchar_t));
hrRet = lpSrc->Read(buffer, size, &ul);
tmp.ReleaseBuffer(ul / sizeof(wchar_t));
*myCStr = CString((LPWSTR)tmp, tmp.GetLength());
// alternatively, you can do the above 2 cases more generically...
typedef CStringT<char or wchar_t> CStreamString;
CStreamString tmp;
buffer = tmp.GetBuffer(size / sizeof(CStreamString::XCHAR));
hrRet = lpSrc->Read(buffer, size, &ul);
tmp.ReleaseBuffer(ul / sizeof(CStreamString::XCHAR));
*myCStr = CString((CStreamString::PXSTR)tmp, tmp.GetLength());
}
}
CATCH_ALL(e)
{
hrRet = STG_E_UNKNOWN;
}
END_CATCH_ALL
_AfxRelease((LPUNKNOWN*)&lpSrc);
return hrRet;
}

Reading Clipboard with WIC

I'm trying to write a function to read images copied to the clipboard with Windows Imaging Component. Currently my code is based off how I load resources but modified to work with the clipboard. everything up to initializing the stream works fine but once I try to use CreateDecoderFromeStream it fails. I've tried copying images from numerous places to no avail. Is there something different about the copied image format that can't be read by WIC?
Here's my code below...
Creating a memory object to read from the clipboard
COMStreamSPtr WIC::createStreamFromClipboard() {
IStream* ipStream = NULL;
COMStreamSPtr stream = nullptr;
CoInitialize(nullptr);
if (!IsClipboardFormatAvailable(CF_BITMAP) && !IsClipboardFormatAvailable(CF_DIB) && !IsClipboardFormatAvailable(CF_DIBV5))
goto Return;
if (!OpenClipboard(NULL))
goto Return;
// Load the clipboard
HGLOBAL hMem = GetClipboardData(CF_BITMAP);
if (hMem == NULL || hMem == INVALID_HANDLE_VALUE)
hMem = GetClipboardData(CF_DIB);
if (hMem == NULL || hMem == INVALID_HANDLE_VALUE)
hMem = GetClipboardData(CF_DIBV5);
if (hMem == NULL || hMem == INVALID_HANDLE_VALUE)
goto CloseClipboard;
// Lock the clipboard, getting a pointer to its data
LPVOID pvSourceClipboardData = GlobalLock(hMem);
if (pvSourceClipboardData == NULL)
goto CloseClipboard;
// Read the clipboard data size
DWORD dwClipboardSize = GlobalSize(hMem);
if (dwClipboardSize == 0)
goto GlobalUnlock;
// Allocate memory to hold the clipboard data
HGLOBAL hgblClipboardData = GlobalAlloc(GMEM_MOVEABLE, dwClipboardSize);
if (hgblClipboardData == NULL)
goto GlobalUnlock;
// Get a pointer to the allocated memory
LPVOID pvClipboardData = GlobalLock(hgblClipboardData);
if (pvClipboardData == NULL)
goto FreeData;
// Copy the data from the clipboard to the new memory block
CopyMemory(pvClipboardData, pvSourceClipboardData, dwClipboardSize);
GlobalUnlock(hgblClipboardData);
// Create a stream on the HGLOBAL containing the data
if (SUCCEEDED(CreateStreamOnHGlobal(hgblClipboardData, TRUE, &ipStream))) {
stream = std::make_shared<COMStream>(ipStream);
goto CloseClipboard;
}
FreeData:
// Couldn't create stream; free the memory
GlobalFree(hgblClipboardData);
GlobalUnlock:
// Unlock the clipboard data
GlobalUnlock(hMem);
CloseClipboard:
// Close the clipboard
CloseClipboard();
Return:
// No need to unlock or free the resource
CoUninitialize(); // CoInialize is called by my COMObjects in the constructor so this is just decrementing the count.
return stream;
}
Elsewhere in the code using the returned stream.
WICBitmapDecoderSPtr WIC::createDecoderFromStream(COMStreamSPtr stream) {
if (isCOMObjectNull(stream))
return nullptr;
IWICImagingFactory* ipFactory = NULL;
IWICBitmapDecoder* ipDecoder = NULL;
WICBitmapDecoderSPtr decoder = nullptr;
// Create the factory
if (FAILED(CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ipFactory))))
goto Return;
// Create the decoder
if (FAILED(ipFactory->CreateDecoderFromStream(stream->ipObject, NULL, WICDecodeMetadataCacheOnDemand, &ipDecoder)))
goto ReleaseFactory; // FAILS HERE
decoder = std::make_shared<WICBitmapDecoder>(ipDecoder);
ReleaseFactory:
ipFactory->Release();
Return:
return decoder;
}
I've finally figured out how to do this. What I did is I used IWICImagingFactory's CreateBitmapFromHBITMAP function. Since reading an HBITMAP from the clipboard work trivial, the solution becomes pretty straight forward.
IWICBitmapSource* WIC::readSourceFromClipboard() {
IWICBitmapSource* ipSource = NULL;
if (Clipboard::containsFormat(CF_BITMAP)) {
if (OpenClipboard(NULL)) {
HBITMAP hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
ipSource = createSourceFromHBitmap(hBitmap);
CloseClipboard();
}
}
return ipSource;
}
IWICBitmapSource* WIC::createSourceFromHBitmap(HBITMAP hBitmap) {
if (hBitmap == NULL)
return NULL;
IWICImagingFactory* ipFactory = NULL;
IWICBitmap* ipBitmap = NULL;
CoInitialize(NULL);
// Create the factory
if (FAILED(CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ipFactory))))
goto Return;
// Create the bitmap
if (FAILED(ipFactory->CreateBitmapFromHBITMAP(hBitmap, NULL, WICBitmapIgnoreAlpha, &ipBitmap)))
goto ReleaseFactory;
ReleaseFactory:
ipFactory->Release();
Return:
CoUninitialize();
return ipBitmap;
}

Copy text to Clipboard in MFC

I tried to make a quick method to set the clipboard text in MFC, but this does not work.
void CopyTextToClipBoard( CString strText)
{
if (OpenClipboard(GetFrame()->GetSafeHwnd()))
{
EmptyClipboard() ;
SetClipboardData (CF_TEXT, strText.GetBuffer() ) ;
CloseClipboard () ;
}
}
I get a 'Windows breakpoint' error at 'setClipboardData'. Anyone know what I might have done wrong?
Edit: Thanks for your comment. Modfied. Now it fails at: memcopy function.
void CopyTextToClipBoard( CString strText)
{
if (OpenClipboard(GetFrame()->GetSafeHwnd()))
{
HGLOBAL hglbCopy;
LPTSTR lptstrCopy;
hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (strText.GetLength() + 1) * sizeof(TCHAR));
if (hglbCopy == NULL)
{
CloseClipboard();
return ;
}
memcpy(GlobalLock(hglbCopy), &strText, strText.GetLength() + 1 * sizeof(TCHAR));
GlobalUnlock(hglbCopy);
SetClipboardData(CF_TEXT, hglbCopy);
EmptyClipboard() ;
SetClipboardData (CF_TEXT, strText.GetBuffer() ) ;
CloseClipboard () ;
}
}
Edit: Using this old msdn example.
const char* output = "Test";
const size_t len = strlen(output) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hMem), output, len);
GlobalUnlock(hMem);
OpenClipboard(0);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();
This is a working one - unicode.
void CopyToClipboard(HWND owner, const std::wstring &w)
{
if (OpenClipboard(owner))
{
HGLOBAL hgClipBuffer = nullptr;
std::size_t sizeInWords = w.size() + 1;
std::size_t sizeInBytes = sizeInWords * sizeof(wchar_t);
hgClipBuffer = GlobalAlloc(GHND | GMEM_SHARE, sizeInBytes);
if (!hgClipBuffer)
{
CloseClipboard();
return;
}
wchar_t *wgClipBoardBuffer = static_cast<wchar_t*>(GlobalLock(hgClipBuffer));
wcscpy_s(wgClipBoardBuffer, sizeInWords, w.c_str());
GlobalUnlock(hgClipBuffer);
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hgClipBuffer);
CloseClipboard();
}
}
(Should be correct, wrote on phone)
From the MSDN documentation of SetClipboardData(uFormat,hMem)
If the hMem parameter identifies a memory object, the object must have
been allocated using the function with the GMEM_MOVEABLE flag
You can do that like this:
LPTSTR lptstrCopy;
HGLOBAL hglbCopy;
unsigned int strSize=strText.GetLength();//get your string lenght
hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (strSize+1) * sizeof(TCHAR));//allocate the memory object with GMEM_MOVEABLE
if (hglbCopy == NULL)
{
CloseClipboard();
//other error handling
}
lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);
memcpy(lptstrCopy, strText.GetBuffer(), strSize * sizeof(TCHAR)); //copy the text data
lptstrCopy[strSize] = (TCHAR) 0;//the null terminator
GlobalUnlock(hglbCopy);
EmptyClipboard() ;
SetClipboardData (CF_TEXT,hglbCopy);
CloseClipboard () ;
The second parameter of SetClipboardData is a handle to a global memory block.
See
http://www.codeproject.com/Articles/2242/Using-the-Clipboard-Part-I-Transferring-Simple-Tex
for a detailed explanation.

How to change clipboard data in Windows

I have the following clipboard data:
Can I somehow change indexes of these records?
Also can I remove / make zero-length some of them?
Is it possible via WinAPI?
As for the first question, I don't see any function for this purpose.
As for the second question, I wrote the following code:
#include <Windows.h>
int main()
{
OpenClipboard(NULL);
HGLOBAL hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, sizeof(int));
int* dst = (int*)GlobalLock(hdst);
dst[0] = 0;
GlobalUnlock(hdst);
SetClipboardData(49166, hdst);
CloseClipboard();
}
but it didn't zeroed the record of the 49166 format.
How can I do it?
This is a method to do that. First, obtain the clipboard data string by use GetClipboardData api, and then modify the data string by your way. Second, rewrite string to cliboard by use SetClipboardData api.
You don't call GetClipboardData first from you code, you can do that like this:
char *buffer = NULL;
CString fromClipboard;
//open the cliboard
if (OpenClipboard())
{
HANDLE hData = GetClipboardData(CF_TEXT);
char* buffer = (char *)GlobalLock(hData);
fromClipboard = buffer;
//modify fromClipboard string by your method and rewrite to clipboard
//maybe like this
fromCliboard.Replace("hello", "world");
HGLOBAL clipbuffer;
char* buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(source));
GlobalUnlock(hData);
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}

How can I convert PCWSTR to a char[] or wchar_t[]

I'm trying to create a file, where one of the parameters has been passed in to the method and is of the type PCWSTR. My code creates a .url file and saves the Url into the file:
wchar_t array1[] = "[InternetShortcut]\nURL=";
wchar_t array2[] = pdwFavoriteUrl;
wchar_t * DataBuffer = new wchar_t[wcslen(array1) + std::strlen(array2) + 1];
std::strcpy(DataBuffer,array1);
std::strcat(DataBuffer,array2);
// Write data to file
DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
DWORD dwBytesWritten = 0;
BOOL bErrorFlag = FALSE;
bErrorFlag = WriteFile(hFile, DataBuffer, dwBytesToWrite, &dwBytesWritten, NULL);
if (FALSE == bErrorFlag)
{
// Log error
hr = E_FAIL;
}
else
{
if (dwBytesWritten != dwBytesToWrite)
{
// Log error
hr = E_FAIL;
}
else
{
// Log success
hr = S_OK;
//_tprintf(TEXT("Wrote %d bytes to %s successfully.\n"), dwBytesWritten, argv[1]);
}
}
CloseHandle(hFile);
I am aware that the code will not compile as-is, since there are conflicting versions of unicode and non-unicode string methods. The line of interest is this one:
wchar_t array2[] = pdwFavoriteUrl;
pdwFavoriteUrl is of type PCWSTR and there is no way around it. Please help convert it to wchar_t[] (preferred) or char[].
AFAIK PCWSTR is just an alias for wchar_t* and in case it is null-terminated that the simplest thing to do would be using it to construct an object of type std::wstring. Just pass it to constructor:
PCWSTR wcstr = L"My String";
std::wstring wstr(wcstr);
and work with std::wstring objects instead of doing evil stuff such as:
wchar_t * DataBuffer = new wchar_t[wcslen(array1) + std::strlen(array2) + 1];
which will probably bring you nothing but unpleasant problems and memory leaks.
Remove
wchar_t array2[] = pdwFavoriteUrl;
and change the call to strcat to
std::strcat(DataBuffer, pwdFavoriteUrl);
(and similarly, replace array2 with pwdFavoriteUrl in the call to strlen.)
Note that PCWSTR is just
typedef const wchar_t * PCWSTR;