Click here to Skip to main content
12,625,634 members (36,736 online)
Click here to Skip to main content
Add your own
alternative version

Stats

27.7K views
339 downloads
16 bookmarked
Posted

WSH Clipboard Access

, 26 Feb 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Scripting the clipboard content in Windows Script Host.

Introduction

I did some Windows Script Host programming recently, and I was pleasantly surprised by its power, features, and flexibility. One thing that I couldn't accomplish was accessing the clipboard from WSH. Digging the Internet, I found some solutions like this one based on Internet Explorer Automation. There are several problems with this approach as you can read in my article about Internet Explorer Automation: What's wrong with Internet Explorer Automation?

Using the code

The solution for scripting the clipboard content in WSH is a regular COM object created with VC++ and ATL. To install the COM object, run register.bat.

Here is a simple example of using the component from WSH:

var clipboardHelper = null;

try
{
    clipboardHelper = WScript.CreateObject("ClipboardHelper.ClipBoard");
}
catch (ex)
{
    WScript.Echo(ex.message + "\n\nClipboardHelper library is not properly registered!");
    WScript.Quit(1);
}

var msg = "Some text";

// Put the text into the clipboard.
clipboardHelper.SetClipboardText(msg);

// Get the text from clipboard.
var text = clipboardHelper.GetClipboardText();

WScript.Echo(text);

Points of interest

The implementation is just regular ATL/COM code. Win32 API functions are used to get access to clipboard text (OpenClipboard, IsClipboardFormatAvailable, GetClipboardData, SetClipboardData, CloseClipboard, GlobalAlloc, GlobalLock, GlobalFree).

Here is the implementation of the method that retrieves the text from clipboard (CF_TEXT and CF_UNICODETEXT formats supported):

STDMETHODIMP CClipBoard::GetClipboardText(BSTR* pBstrClipboardText)
{
    if (NULL == pBstrClipboardText)
    {
        return E_INVALIDARG;
    }

    CComBSTR bstrResult = L"";

    if (::OpenClipboard(NULL))
    {
        if (::IsClipboardFormatAvailable(CF_TEXT) || 
            ::IsClipboardFormatAvailable(CF_UNICODETEXT))
        {
            // First try to ge UNICODE text.
            BOOL   bUnicode   = TRUE;
            HANDLE hClipboard = ::GetClipboardData(CF_UNICODETEXT);

            if (NULL == hClipboard)
            {
                // If UNICODE text was not available try to get ANSI text.
                bUnicode   = FALSE;
                hClipboard = ::GetClipboardData(CF_TEXT);
            }

            if (hClipboard != NULL)
            {
                LPCSTR szClipboardData = (LPCSTR)::GlobalLock(hClipboard);

                if (szClipboardData != NULL)
                {
                    if (bUnicode)
                    {
                        LPCWSTR szClipboardWText = (LPCWSTR)szClipboardData;

                        bstrResult       = szClipboardWText;
                        szClipboardWText = NULL;
                    }
                    else
                    {
                        LPCSTR szClipboardText = (LPCSTR)szClipboardData;

                        bstrResult      = szClipboardText;
                        szClipboardText = NULL;
                    }

                    ::GlobalUnlock(hClipboard);

                    *pBstrClipboardText = bstrResult.Detach();
                    hClipboard = NULL;
                }
                else
                {
                    ATLTRACE("GlobalLock failed in CClipBoard::GetClipboardText\n");
                }
            }
            else
            {
                ATLTRACE("GetClipboardData failed in CClipBoard::GetClipboardText\n");
            }

            hClipboard = NULL;
        }
        else
        {
            ATLTRACE("CF_TEXT NOT available in CClipBoard::GetClipboardText\n");
        }

        BOOL bRes = ::CloseClipboard();
        ATLASSERT(bRes);
    }
    else
    {
        ATLTRACE("Can NOT OpenClipboard in CClipBoard::GetClipboardText\n");
    }

    return S_OK;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Adrian Dorache
Software Developer (Senior) Codecentrix Software
Romania Romania
Software consultant in Bucharest Romania writing Windows programs and browsers plug-ins since 1998 with Visual C++.

You may also be interested in...

Comments and Discussions

 
QuestionCOM Event Listener possible? Pin
Member 40613579-Feb-11 12:36
memberMember 40613579-Feb-11 12:36 
AnswerRe: COM Event Listener possible? Pin
Adrian Dorache9-Feb-11 21:20
memberAdrian Dorache9-Feb-11 21:20 
GeneralCould Not Create Object named "ClipboardHelper.Clipboard" Pin
masonfoley22-Feb-10 11:51
membermasonfoley22-Feb-10 11:51 
GeneralRe: Could Not Create Object named "ClipboardHelper.Clipboard" Pin
Adrian Dorache22-Feb-10 21:43
memberAdrian Dorache22-Feb-10 21:43 
GeneralProblem Pin
Uros Calakovic27-Feb-09 9:53
memberUros Calakovic27-Feb-09 9:53 
GeneralRe: Problem Pin
Adrian Dorache28-Feb-09 2:14
memberAdrian Dorache28-Feb-09 2:14 
GeneralRe: Problem Pin
Uros Calakovic28-Feb-09 4:34
memberUros Calakovic28-Feb-09 4:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.1 | Last Updated 26 Feb 2009
Article Copyright 2009 by Adrian Dorache
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid