What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Trying to copy some text to the clipboard

Trying to copy some text to the clipboard
Author: Message:
segosa
Community's Choice
*****


Posts: 1407
Reputation: 92
Joined: Feb 2003
O.P. Trying to copy some text to the clipboard
I'm trying to make a function that will copy text to the clipboard. I'm using the Windows API. This is the code I have so far:

code:
function set_clipboard_text()
{
    var tmp = "This is a test";
    var l = tmp.length * 2 + 2;

    if (Interop.Call("User32", "OpenClipboard", 0))
    {
            Interop.Call("User32", "EmptyClipboard");
            var h = Interop.Call("Kernel32", "GlobalAlloc", 0x2000, l);
            var p = Interop.Allocate( l );
            p.WriteDWORD(0, Interop.Call("Kernel32", "GlobalLock", h));
            Interop.Call("Kernel32", "lstrcpyW", p, tmp);
            Interop.Call("Kernel32", "GlobalUnlock", h);
            Interop.Call("User32", "SetClipboardData", 1, h)
            Interop.Call("User32", "CloseClipboard");
    }
}


I'm obviously going to make it set_clipboard_text(str), the way the function is now is just while I test it and try and get it to work.

I'm basing it off some C++ code that I wrote a while ago:

code:
    OpenClipboard(NULL);
    EmptyClipboard();
    HGLOBAL h = GlobalAlloc(GMEM_DDESHARE, strlen(source)+1);
    char* p = (char*)GlobalLock( h );
    lstrcpy(p, source);
    GlobalUnlock( h );
    SetClipboardData(CF_TEXT,h);
    CloseClipboard();


The function as it is now works fine, there are no errors. And text is copied to the clipboard. The problem is, this text consists of several spaces and random characters. I have a feeling it's a unicode problem but I can't see where I've gone wrong in my function, so does anyone have any ideas?
The previous sentence is false. The following sentence is true.
07-07-2006 04:27 PM
Profile PM Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: Trying to copy some text to the clipboard
How about using the Clipboard object? (following code copied directly from the MSDN without testing):

code:
private function button1_Click(sender : Object, e : System.EventArgs) {
    //Take the selected text from a text box and put it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText);
    else
       textBox2.Text = "No text selected in textBox1";
}

private function button2_Click(sender : Object, e : System.EventArgs) {
    //Declare an IDataObject to hold the data returned from the clipboard.
    //Then retrieve the data from the clipboard.
    var iData : IDataObject = Clipboard.GetDataObject();

    //Determine whether the data is in a format you can use.
    if(iData.GetDataPresent(DataFormats.Text)) {
       //Yes it is, so display it in a text box.
       textBox2.Text = String(iData.GetData(DataFormats.Text));
    }
    else {
       //No it is not.
       textBox2.Text = "Could not retrieve data off the clipboard.";
    }
}

07-07-2006 10:48 PM
Profile E-Mail PM Find Quote Report
RaceProUK
Elite Member
*****

Avatar

Posts: 6073
Reputation: 57
39 / Male / Flag
Joined: Oct 2003
RE: Trying to copy some text to the clipboard
That looks a little .NET to me... will it actually work?
[Image: spartaafk.png]
07-07-2006 10:55 PM
Profile PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: Trying to copy some text to the clipboard
I noticed that... But this page is really talking about JScript... It seems like this is different from the language we are using for scripts...^o)
07-07-2006 11:00 PM
Profile E-Mail PM Find Quote Report
Dhaya
Junior Member
**

Avatar
lala~

Posts: 25
Reputation: 4
38 / Male / –
Joined: Jul 2006
RE: Trying to copy some text to the clipboard
I tested using this class which is inherited from System.Windows.Forms, and i didn't found a way to make it work :/
Godamn JScript ! I HATE you ! ... but I love you so much <3
07-07-2006 11:28 PM
Profile PM Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: Trying to copy some text to the clipboard
quote:
Originally posted by J-Thread
I noticed that... But this page is really talking about JScript... It seems like this is different from the language we are using for scripts...^o)

thats because its JScript.net 8-)
07-08-2006 07:43 AM
Profile PM Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: Trying to copy some text to the clipboard
Right... I think I get it now...

So let's go back to topic, I don't see why that wouldn't work... But I guess there is a better way to acces the clipboard. Maybe with some ActiveX object?
07-08-2006 11:02 AM
Profile E-Mail PM Find Quote Report
andrey
elite shoutboxer
****

Avatar

Posts: 795
Reputation: 48
– / Male / Flag
Joined: Aug 2004
RE: Trying to copy some text to the clipboard
Solution for copying text to/from the clipboard:
[UPDATED] Clipboard Functions (that WORK)

This post was edited on 07-12-2006 at 02:33 PM by andrey.
[Image: w2kzw8qp-sq2_dz_b_xmas.png]
07-12-2006 02:26 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On