[vbox-dev] Shared clipboard, overview of host-client -interaction

Michael Thayer Michael.Thayer at Sun.COM
Mon May 11 09:54:53 GMT 2009


Hello Jori,

Jori Mantysalo wrote:
> I haven't figured out how to
> get some working example to compile. I can #include headers and use
> clipboard
> functions, but how to link it? Could somebody show for example a code to
> put "hello" to shared clipboard or something similar?
You will need to link in $(VBOX_LIB_IPRT_GUEST_R3) (RuntimeGuestR3.a),
$(VBOX_LIB_VBGL_R3) (VBoxGuestR3Lib.a) and $(VBOX_LIB_IPRT_GUEST_R3)
again.  The last is due to circular dependencies between those
libraries.  If in doubt, take a look at the makefile for VBoxClient in
the VirtualBox sources.

I'm afraid I don't have time to do a proper sample clipboard "hello" as
you asked, but I will write a skeleton (which will probably not even
compile :) ) below.  Please do not use this without adding error code
checking...

    uint32_t u32Client = 0, u32Msg = 0, u32Formats = 0;
    PRTUTF16 pwcHello = NULL;
    /* Initialise our runtime before all else. */
    RTR3Init();
    /* Initialise the guest library. */
    VbglR3InitUser();
    /* Convert our "hello" to a format the clipboard subsystem
     * understands, that is Windows UCS-2 text.  Note the CR LF.
     * RTStr* is prototyped in iprt/string.h */
    RTStrToUtf16("Hello world\r\n", &pwcHello);
    /* Connect to the shared clipboard */
    VbglR3ClipboardConnect(&u32Client);
    /* Grab the clipboard and offer unicode text */
    VbglR3ClipboardReportFormats(u32Client,
            VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
    /* Loop until the host tells us it is shutting down, or the host
     * grabs the clipboard. */
    while (   (u32Msg != VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT)
           && (u32Msg != VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS))
    {
        /* Wait for a request from the host */
        VbglR3ClipboardGetHostMsg(u32Client, &u32Msg, &u32Formats);
        /* If the host tries to read unicode text from our clipboard,
         * send "Hello world\n" as Windows UCS-2 text. */
        if (   (u32Msg == VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA)
            && (u32Formats & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT))
            VbglR3ClipboardWriteData(u32Client,
                    VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT,
                    pwcHello, sizeof("Hello world\r\n") * 2);
    }
    /* Shut down the shared clipboard */
    VbglR3ClipboardDisconnect(u32Client);

Note that currently the host side of the shared clipboard expects that
only one guest application (more precisely, client) will connect to it
at one time.  I think that current svn (but not 2.2.2) removes this
restriction for X11 hosts, but not for Windows hosts.  So for now, you
will have to terminate VBoxClient in the guest to try out your code.
Once you have got this working, we can look at how to do this properly.

Regards,

Michael
-- 
Sun Microsystems GmbH        Michael Thayer
Werkstrasse 24               VirtualBox engineer
71384 Weinstadt, Germany     mailto:michael.thayer at sun.com

Sitz der Gesellschaft:
Sun Microsystems GmbH, Sonnenallee 1, 85551 Kirchheim-Heimstetten
Amtsgericht Muenchen: HRB 161028
Geschaeftsfuehrer: Thomas Schroeder, Wolfgang Engels, Wolf Frenkel
Vorsitzender des Aufsichtsrates: Martin Haering




More information about the vbox-dev mailing list