I was hoping someone could help me. I am running a windows C based program and there is data being held in memory. If i hit a buttin on the keyboard i would like to copy all the data to the clipboard and paste it into an Excel spreadsheet.
Thus far I have written the below code.
And while it does not throw any exceptions / return NULL pointers or anythign.. the data is not in the clipboard after i have completed.
///////////////////////////////////////////////////////
typedef struct transtructFormat
{
CHAR ValueDate[10];
CHAR Counterparty[20];
CHAR CcyPair[10];
LONG Amount;
DOUBLE Rate;
CHAR Dealer[5];
CHAR Tp[3];
CHAR Status[4];
LONG DealNumber;
CHAR Med[4];
} TRANSTRUCTFORMAT; // format of the data copied to clipboard
UINT format = RegisterClipboardFormat("TRANSTRUCT_FORMAT");
int l;
HGLOBAL clipbuffer = GlobalAlloc(GMEM_FIXED, sizeof(TRANSTRUCTFORMAT)* TranStruct.lNumEl*/)
TRANSTRUCTFORMAT* pTranstructFormat = (TRANSTRUCTFORMAT*)GlobalLock(clipbuffer);
if (OpenClipboard(hwd) )
{
EmptyClipboard();
for (l = 0; l < tranStruct.lNumEl; ++l)
{
PTRANLISTITEM pItem = TranStruct.Trans[l];
From above see there is an array of TranStruct.Trans and i want to copy data from each one into an array of (dynamically created) TRANSTRUCTFORMAT
and then copy to the clipboard... to then paste into excel.
Well, aside from the ordering of your calls (why GlocalAlloc if the call to OpenClipboard has failed, etc), MSDN does state that memory must be allocated (with GlobalAlloc) as GMEM_MOVEABLE
If the hMem parameter identifies a memory object, the object must have been allocated using the function with the GMEM_MOVEABLE flag.
But Excel won't know anything about your custom format. You need to provide data in one of the formats that Excel can already handle, or implement an OLE DataObject which can convert the data into one of the formats that Excel knows about on request.