Link to home
Start Free TrialLog in
Avatar of MooDave
MooDave

asked on

copy text string to clipboard

I wish to copy a text string to Windows' clipboard. I'm encountering type errors when trying to copy my local string into the allocated memory block. The code segment folows. The commented section in the middle is where I am encountering the problem.  FYI: I am using Borland C++ v5.01. Thanks.

int i, wLen;                  // counter & text length variables
HGLOBAL hGMem;    // global handle to a memory block
void FAR* lpGMem;    // pointer to the memory block
char *lpText = "This is a test";   // pointer to the text string

wLen = strlen(lpText);
hGMem = GlobalAlloc(GHND, (DWORD) wLen + 1);
lpGMem = GlobalLock(hGMem);

// copy the string pointed to by lpText into the memory block
// This is where I'm having the problem. I'm getting "not an allowed type"
// errors here. How do I get the memory block to hold my text? I've tried:

//for (i=0; i<wLen; i++)
//      *lpGMem = *lpText++;

// and

// *lpGMem = * lpText;

// with no success.

GlobalUnlock(hGMem);

if (OpenClipboard()) {
      EmptyClipboard();
   SetClipboardData(CF_TEXT, hGMem);
   CloseClipboard();
   }
ASKER CERTIFIED SOLUTION
Avatar of slp
slp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joneva
joneva

strcpy was a good place to start, but slp's answer:
strcpy( *lpGMem, *lpText );
gave me the "not an allowed type" error.

joneva's suggestion of:
strcpy( lpGMem, lpText ); with casting, turned out to be the solution. The final solution was:

strcpy( (char *)lpGMem, lpText );

Many thanks to joneva (is it possible to give her some points for this?