/////////////////////////////////////////////////////////////////////////////
// globals.cpp : IComponent Interface helpers
//
// This is a part of the MMC SDK.
// Copyright (C) 1997 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// MMC SDK Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// MMC Library product.
//
#include "stdafx.h"
#include "globals.h"
HINSTANCE g_hinst; // Global instance handle
/////////////////////////////////////////////////////////////////////////////
// Global helper functions to help work with dataobjects and
// clipboard formats
//---------------------------------------------------------------------------
// Returns the current object based on the s_cfInternal clipboard format
//
CDataObject*
ExtractOwnDataObject
(
LPDATAOBJECT lpDataObject // [in] IComponent pointer
)
{
HGLOBAL hGlobal;
HRESULT hr = S_OK;
CDataObject *pdo = NULL;
hr = ExtractFromDataObject( lpDataObject,
CDataObject::s_cfInternal,
sizeof(CDataObject **),
&hGlobal
);
if (SUCCEEDED(hr))
{
pdo = *(CDataObject **)(hGlobal);
_ASSERT(pdo);
_ASSERT(!GlobalFree(hGlobal));
}
return pdo;
} // end ExtractOwnDataObject()
//---------------------------------------------------------------------------
// Extracts data based on the passed-in clipboard format
//
HRESULT
ExtractFromDataObject
(
LPDATAOBJECT lpDataObject, // [in] Points to data object
UINT cfClipFormat, // [in] Clipboard format to use
ULONG nByteCount, // [in] Number of bytes to allocate
HGLOBAL *phGlobal // [out] Points to the data we want
)
{
HRESULT hr = S_OK;
STGMEDIUM stgmedium = { TYMED_HGLOBAL, NULL };
FORMATETC formatetc = { cfClipFormat, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
_ASSERT( NULL != lpDataObject );
*phGlobal = NULL;
do
{
// Allocate memory for the stream
stgmedium.hGlobal = GlobalAlloc( GMEM_SHARE, nByteCount );
if (!stgmedium.hGlobal)
{
hr = E_OUTOFMEMORY;
ATLTRACE( L"Out of memory\n" );
break;
}
// Attempt to get data from the object
hr = lpDataObject->GetDataHere( &formatetc, &stgmedium );
if (FAILED(hr))
{
break;
}
// stgmedium now has the data we need
*phGlobal = stgmedium.hGlobal;
stgmedium.hGlobal = NULL;
} while (0);
if (FAILED(hr) && stgmedium.hGlobal)
{
_ASSERT( !GlobalFree(stgmedium.hGlobal) );
}
return hr;
} // end ExtractFromDataObject()
VOID DisplayError( LONG nErrorCode, LPWSTR wszDlgTitle )
{
LPVOID lpMsgBuf;
::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
nErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&lpMsgBuf,
0,
NULL
);
::MessageBox( NULL, (LPWSTR)lpMsgBuf, wszDlgTitle, MB_OK|MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
}