Author Topic: SetClipboardData  (Read 3795 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6935
  • AKA Daniel
SetClipboardData
« on: July 15, 2007, 03:35:39 PM »
I know this is a win32 issue… I am trying to use SetClipboardData to copy a string to the clipboard
but my code is only picking up the first character and I don’t know why. What am I doing wrong?

Thanks


Code: [Select]
int SetClipboardText(void)
{
    HWND docHwnd = adsw_acadDocWnd();
    struct resbuf *pArgs = acedGetArgs() ;

    if (pArgs == NULL && pArgs->restype != RTSTR )
        return RSERR;

    if(OpenClipboard(docHwnd))
    {
        HGLOBAL clipbuffer;
        ACHAR* buffer;

        EmptyClipboard();

        clipbuffer = GlobalAlloc(GMEM_DDESHARE |
            GMEM_DDESHARE, _tcslen(pArgs->resval.rstring) + 1);

        buffer = (ACHAR*)GlobalLock(clipbuffer);
        _tcscpy(buffer, pArgs->resval.rstring);

        GlobalUnlock(clipbuffer);
        SetClipboardData(CF_TEXT,clipbuffer);
        CloseClipboard();
    }
    return (RSRSLT) ;
}


LE

  • Guest
Re: SetClipboardData
« Reply #1 on: July 15, 2007, 04:21:43 PM »
I have two functions:

setclipboardtext()
getclipboardtext()

Let me know, if you want to use them.


Sorry, I am cleaning a house right now, so no time to do some research or to provide better help...
« Last Edit: July 15, 2007, 04:58:47 PM by LE »

LE

  • Guest
Re: SetClipboardData
« Reply #2 on: July 15, 2007, 06:09:36 PM »
Daniel;

Without testing your function....

GMEM_DDESHARE is obsolete and will be ignored, you are using that twice, try with GMEM_MOVEABLE instead (it is the one I have)

MP

  • Seagull
  • Posts: 17400
Re: SetClipboardData
« Reply #3 on: July 15, 2007, 07:15:56 PM »
GMEM_DDESHARE is obsolete ...

Forgive me, but the irony of this, in the context of this thread is killing me.

{ where's the shrug icon? }

Sorry?

:whistle:

{ genuinely hoping the wrinkles can be ironed out }
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6935
  • AKA Daniel
Re: SetClipboardData
« Reply #4 on: July 15, 2007, 08:55:58 PM »
Daniel;

Without testing your function....

GMEM_DDESHARE is obsolete and will be ignored, you are using that twice, try with GMEM_MOVEABLE instead (it is the one I have)

Hi Luis,

I tried all of the combinations of GMEM_MOVEABLE and GMEM_DDESHARE but still no go.. I would love to see your code but give me a little time to figure it out…


Apples and fish MP  :-D

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6935
  • AKA Daniel
Re: SetClipboardData
« Reply #5 on: July 15, 2007, 09:43:30 PM »
Got it

thanks all

I needed to define this

Code: [Select]
#ifdef UNICODE
#define CF_TCHAR CF_UNICODETEXT
#else
#define CF_TCHAR CF_TEXT
#endif


new code

Code: [Select]
int SetClipboardText(void)
{
    CString strin;

    struct resbuf *pArgs = acedGetArgs() ;

    if (pArgs == NULL && pArgs->restype != RTSTR )
        return RSERR;

    strin = pArgs->resval.rstring;
    if(OpenClipboard(NULL))
    {
        HGLOBAL clipbuffer;
        ACHAR* buffer;
        EmptyClipboard();
        clipbuffer = GlobalAlloc(GMEM_MOVEABLE ,
            (lstrlen (LPCTSTR(strin)) + 1) * sizeof (TCHAR));
        buffer = (ACHAR*)GlobalLock(clipbuffer);
        lstrcpy(buffer,LPCTSTR(strin));
        GlobalUnlock(clipbuffer);
        SetClipboardData(CF_TCHAR,clipbuffer);
        CloseClipboard();
    }
    return (RSRSLT) ;
}

LE

  • Guest
Re: SetClipboardData
« Reply #6 on: July 15, 2007, 09:51:31 PM »
Code: [Select]
Got it

thanks all
...

(lstrlen (LPCTSTR(strin)) + 1) * sizeof (TCHAR));

Good.... was about to post a simliar code as above.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6935
  • AKA Daniel
Re: SetClipboardData
« Reply #7 on: July 16, 2007, 12:01:19 AM »
Thanks Luis

These seem to work well. do you see any errors?

Code: [Select]
//(SetClipboardText "How yuz doin'")
int SetClipboardText(void)
{
    CString cstrStrin;
    struct resbuf *pArgs = acedGetArgs() ;

    if (pArgs == NULL && pArgs->restype != RTSTR )
        return RSERR;

    cstrStrin = pArgs->resval.rstring;

    if(!OpenClipboard(NULL))
        return RSERR;

    HGLOBAL hglClipBuff;
    ACHAR* pszBuf;
    EmptyClipboard();
    hglClipBuff = GlobalAlloc(GMEM_MOVEABLE ,
        (lstrlen (LPCTSTR(cstrStrin)) + 1) * sizeof (TCHAR));
    pszBuf = (ACHAR*)GlobalLock(hglClipBuff);
    lstrcpy(pszBuf,LPCTSTR(cstrStrin));
    GlobalUnlock(hglClipBuff);
    SetClipboardData(CF_TCHAR,hglClipBuff);
    CloseClipboard();

    acedRetT();
    return (RSRSLT) ;
}

//(GetClipboardText)
int GetClipboardText(void)
{
    ACHAR* pszBuf;
    ACHAR* pszStrout;
    HGLOBAL hglClipBuff;

    if(!OpenClipboard(NULL))
        return RSERR;

    if (hglClipBuff = GetClipboardData (CF_TCHAR))
    {
        pszStrout = (ACHAR*)GlobalLock (hglClipBuff);
        pszBuf = (ACHAR*)malloc (GlobalSize (hglClipBuff));

        if (pszBuf == NULL)
            return RSERR;

        lstrcpy (pszBuf, pszStrout);

        GlobalUnlock(hglClipBuff);
    }

    CloseClipboard () ;

    if (pszBuf == NULL)
        return RSERR;

    acedRetStr(pszBuf);
    free(pszBuf);
    pszBuf = NULL;
    return (RSRSLT) ;
}
« Last Edit: July 16, 2007, 12:09:22 AM by Danielm103 »

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6935
  • AKA Daniel
Re: SetClipboardData
« Reply #8 on: July 16, 2007, 12:42:00 AM »
Daniel;

They look basically the same... :)

Attached are the ones I did.... just in case.

Awesome, Thanks Luis  :-)

LE

  • Guest
Re: SetClipboardData
« Reply #9 on: July 16, 2007, 09:44:16 AM »