TinyURL plugin for Windows Mobile

I was surprised by the reaction to my del.icio.us plugin for Pocket Internet Explorer – between my blog post and the download page, I got something like 70 links and over 1,000 downloads in a couple of weeks. Wow.

If I’d known so many people would read it, I might not have made the blog post quite so moany :-)

I got emails asking for versions of the plugin for their own favourite web service, but had to leave it for a bit because something else came up. Now I’m back, I thought I’d give one a go.

I’ve started with my next most used web service: TinyURL.

If you’re not familiar with it, TinyURL turns long web addresses into shorter, easy to share ones. It’s particularly useful when you’re trying to cram a link into a 140 character twitter update!

And – similarly to del.icio.us – using it on the Windows Mobile web browser is fiddly and long-winded. So here is a plugin for Pocket Internet Explorer to help.

It adds an entry to the Pocket Internet Explorer menu that gets a TinyURL for the webpage you are looking at, copying it to your clipboard so you can paste it into an SMS, email, tweet, IM message etc.

As always, anyone is welcome to give it a go – and any feedback would be gratefully received. It should run on any Windows Mobile 5/6 device, shouldn’t need any pre-reqs, and you can download it for Pocket PC (touchscreen) or Smartphone.

Read on if you are interested in a bit more ramble about how it works. :-)

Generating a TinyURL

I’d read that TinyURL has a simple API on Dave Winer’s blog about a month ago.

You just send an HTTP request to:
http://tinyurl.com/api-create.php?url=http://url_you_want_to_tiny.com/
and it returns you a tinyurl. Easy :-)

Sending an HTTP request

As part of my ongoing effort to keep my hand in with “real” programming despite all of the C# and Java I seem to end up doing, this is all done in C++.

Sending an HTTP request isn’t too tough – you basically do this (with error-handling ripped out to keep things short):

1. Initialise an Internet handle

HINTERNET hInternetSession = InternetOpen(TEXT("CeHttp"), 
                                          INTERNET_OPEN_TYPE_PRECONFIG, 
                                          NULL, 
                                          NULL, 
                                          0);

2. Create a session for tinyurl.com

HINTERNET hConnection = InternetConnect(hInternetSession, 
                                        TEXT("tinyurl.com"), 
                                        INTERNET_DEFAULT_HTTP_PORT, 
                                        NULL, NULL, 
                                        INTERNET_SERVICE_HTTP, 
                                        0, 1);

3. Open a handle for an HTTP request – where lpszServer is the rest of the TinyURL URL
(e.g. /api-create.php?url=http://myurl.com)

HINTERNET hURL = HttpOpenRequest(hConnection, 
                                 TEXT("GET"), 
                                 lpszServer, 
                                 TEXT("HTTP/1.1"), 
                                 NULL, NULL, 0, 1); 

4. Send the HTTP request

HttpSendRequest(hURL, NULL, 0, NULL, 0);

5. Read the response

InternetReadFile(hURL, (LPSTR)cBuffer, (DWORD)1024, &dwBytesRead);

6. Cleanup using InternetCloseHandle

Nice and simple.

Copying the result to the clipboard

To copy something into the clipboard using C++, you can do this sort of thing:

OpenClipboard(GetFocus());

EmptyClipboard();
	
HGLOBAL hglbCopy1 = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE, dwBytesRead + 1); 

if( hglbCopy1 != NULL )
{
   char* lptstrCopy = (char*) GlobalLock(hglbCopy1); 

   strcpy(lptstrCopy, cBuffer);

   SetClipboardData(CF_TEXT, hglbCopy1);

   GlobalUnlock(hglbCopy1);
}

CloseClipboard();

where cBuffer (and dwBytesRead) is the buffer I got back from InternetReadFile – with the TinyURL URL.

It is a bit more fiddly – the biggest issue is you need to save a couple of versions of the text to make sure you cover the different Windows Mobile apps. For example, the mobile MS Word can paste something from CF_TEXT fine, but if you want to paste into the address bar of Pocket Internet Explorer you need to have the URL in the CF_UNICODETEXT clipboard format.

Turning the text version into unicode isn’t too difficult – you can convert it using MultiByteToWideChar.

Then you do the above work again with the converted unicode string, but using CF_UNICODETEXT when you call SetClipboardData.

And that’s pretty much it

The rest of the work was the same as the del.icio.us plugin – doing the work to get a new menu item in Pocket Internet Explorer.

This time, I’ve tried building a version for Windows Mobile Smartphone, which I’ve not done before. Not entirely sure it’ll work, as I’ve not got a smartphone device to try it on. (It works on the emulator, but I never really trust them… :-) )

Why isn’t anyone else doing this?

I was surprised not to see any other versions already… about a dozen copies of the zipped-up source for the del.icio.us plugin have been downloaded in the last few weeks, so I thought someone would have a go at mod-ding it to create a version for digg.com or something.

Maybe my code isn’t as straightforward as I thought?

13 Responses to “TinyURL plugin for Windows Mobile”

  1. Alcides says:

    Hi.

    I have an idea for a simple pluggin like your delicious one, but I can’t manage to get your c++ code working.

    Please get in touch with me for more details please ;)

  2. dale says:

    I’d be happy to hear about your idea. You can either write about it here, or if you feel it is private in some way, you could always email it to me.

    Kind regards, D

  3. dale says:

    Whoops…. must have copy-and-pasted too much code from the del.icio.us plugin – installing this one breaks my del.icio.us plugin :-(

    Until I’ve figured out what I’ve done wrong, if you want to use both, I’ve bundled the two extensions together into a single DLL available here as a combined Pocket Internet Explorer plugins package.

    Sorry to all those who emailed me about it! :-)

  4. Angelo says:

    Nice programs! I had just thought to myself “I wonder if both of these can be installed without conflict” and then you provided an alternative fix. Good work!

  5. [...] learning something new » Blog Archive » TinyURL plugin for Windows Mobile Another great tool for Windows Mobile – Tiny URL. From the same guy that made the del.icio.us tool for windows Mobile. (tags: mobile tinyurl plugin) [...]

  6. I just use http://www.tiny9.com instead because you can specify tags to use in your URLs instead of random ones.

  7. Dave Atkins says:

    How does one paste from the clipboard on a Windows Mobile 5 Smartphone (Motorola Q). This plugin appears to work, but the phone does not have copy and paste.

  8. dale says:

    @Dave – I never realised that smartphones don’t have a clipboard (I’ve always used touchscreen WM devices). Wow, that must be annoying

    I guess this plugin isn’t too useful then – if no WM smartphone apps have ‘Paste’ functions – sorry about that!

  9. No help for existing device users, but Windows Mobile 6.1 introduces cut/copy/paste functionality for standard (i.e. smartphone) devices via a couple of menu items in most of the standard applications.

  10. Jason Gill says:

    What we really need is something that causes a tinyurl link opened on your windows mobile browser to open in Google’s Mobile proxy so that the page opened can be stripped down! I am always so scared to follow a tinyurl in twitter on my mobile browser because it might take a long time to load and even sometimes lock up my browser completely because the site isn’t optimized.

  11. dale says:

    @Jason – Neat idea!

  12. [...] TinyURL Plugin for Windows Mobile adds a TinyURL function to Windows Mobile phone systems. Currently, there isn’t an iPhone app for reduced URLs, but there is method using Safari and Twitterrific that will work, if you are determined. [...]

  13. Thanks for sharing, I am using TinyUrl Creator from Firefox Add-on.