Base64Encode HGLOBAL from clipboard
I am trying to base64 encode some binary data stored on the
clipboard. The resulting string will be stored in an XML file. The
data is a PNG image, and the string I get from ATL::Base64Encode seems
to be very wrong. The data on the clipboard seems to be fine, and I
believe that I am retrieving it correctly, but the string has several
invalid characters at the end ("=CD=FD=FD=FD=FD=AB=AB=AB=AB=AB=AB=AB=AB=FE=
=EE=FE"), and there are no
'=' characters. Here is the code:
hr = spDataObject->GetData( &formatEtc, &stgMedium );
if ( SUCCEEDED( hr ) )
{
if ( stgMedium.hGlobal != NULL )
{
BYTE *pImage = (LPBYTE)::GlobalLock( stgMedium.hGlobal ) );
// this call works just fine, so the data appears to be valid
std::fstream filestream( "D:\\image.png", std::ios::out |
std::ios::binary |
std::ios::app );
filestream.write( reinterpret_cast<char *>( pImage ),
::GlobalSize( stgMedium.hGlobal ) );
filestream.close();
if ( pImage )
{
int iSize = Base64EncodeGetRequiredLength(
::GlobalSize( stgMedium.hGlobal ),
ATL_BASE64_FLAG_NOPAD | ATL_BASE64_FLAG_NOCRLF );
char *szBase64Image = new char[ iSize ];
if ( Base64Encode( pImage,
::GlobalSize( stgMedium.hGlobal ),
szBase64Image,
&iSize,
ATL_BASE64_FLAG_NOPAD |
ATL_BASE64_FLAG_NOCRLF ) )
{
// store szBase64Image in XML...
}
delete [] szBase64Image;
}
::GlobalUnlock( stgMedium.hGlobal );
}
::ReleaseStgMedium( &stgMedium );
}
I have tried other methods for obtaining the BYTE array, including
CreateILockBytesOnHGlobal. I don't think that is the problem. I
don't have a lot of experience with the clipboard, and I have never
used ATL::Base64Encode before, so any help with what I am doing wrong
would be appreciated.
Thank you.
Jeff