Click here to Skip to main content
Licence CPOL
First Posted 26 Feb 2009
Views 16,176
Downloads 144
Bookmarked 16 times

WSH Clipboard Access

By | 26 Feb 2009 | Article
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)

About the Author

Adrian Dorache

Software Developer (Senior)
Codecentrix Software
Romania Romania

Member

Software consultant in Bucharest Romania writing Windows programs and browsers plug-ins since 1998 with Visual C++.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCOM Event Listener possible? PinmemberMember 406135711:36 9 Feb '11  
AnswerRe: COM Event Listener possible? PinmemberAdrian Dorache20:20 9 Feb '11  
GeneralCould Not Create Object named "ClipboardHelper.Clipboard" Pinmembermasonfoley10:51 22 Feb '10  
GeneralRe: Could Not Create Object named "ClipboardHelper.Clipboard" PinmemberAdrian Dorache20:43 22 Feb '10  
GeneralProblem PinmemberUros Calakovic8:53 27 Feb '09  
GeneralRe: Problem PinmemberAdrian Dorache1:14 28 Feb '09  
GeneralRe: Problem PinmemberUros Calakovic3:34 28 Feb '09  

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

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

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