{"id":181,"date":"2007-08-07T15:49:39","date_gmt":"2007-08-07T15:49:39","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=181"},"modified":"2007-08-07T15:51:37","modified_gmt":"2007-08-07T15:51:37","slug":"tinyurl-plugin-for-windows-mobile","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=181","title":{"rendered":"TinyURL plugin for Windows Mobile"},"content":{"rendered":"<p>I was surprised by the reaction to my <a href=\"http:\/\/dalelane.co.uk\/page.php?id=39\">del.icio.us plugin for Pocket Internet Explorer<\/a> &#8211; between my <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=175\">blog post<\/a> and the download page, I got something like 70 links and over 1,000 downloads in a couple of weeks. Wow. <\/p>\n<p>If I&#8217;d known so many people would read it, I might not have made the blog post quite so moany \ud83d\ude42<\/p>\n<p>I got emails asking for versions of the plugin for their own favourite web service, but had to leave it for a bit because <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=176\">something else came up<\/a>. Now I&#8217;m back, I thought I&#8217;d give one a go. <\/p>\n<p>I&#8217;ve started with my next most used web service: TinyURL. <\/p>\n<p><img decoding=\"async\" src=\"http:\/\/dalelane.co.uk\/blog\/post-images\/070807-tinyurl2.gif\" align=\"left\"\/>If you&#8217;re not familiar with it, <a href=\"http:\/\/tinyurl.com\/\" target=\"_blank\">TinyURL<\/a> turns long web addresses into shorter, easy to share ones. It&#8217;s particularly useful when you&#8217;re trying to cram a link into a 140 character <a href=\"http:\/\/twitter.com\/dalelane\" target=\"_blank\">twitter update<\/a>! <\/p>\n<p>And &#8211; similarly to <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=175\">del.icio.us<\/a> &#8211; using it on the Windows Mobile web browser is fiddly and long-winded. So here is a plugin for Pocket Internet Explorer to help. <\/p>\n<p>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.<\/p>\n<p>As always, anyone is welcome to give it a go &#8211; and any feedback would be gratefully received. It should run on any Windows Mobile 5\/6 device, shouldn&#8217;t need any pre-reqs, and you can download it for <a href=\"http:\/\/dalelane.co.uk\/files\/turn_into_tinyurl.cab\">Pocket PC (touchscreen)<\/a> or <a href=\"http:\/\/dalelane.co.uk\/files\/turn_into_tinyurl_smartphone.cab\">Smartphone<\/a>.<\/p>\n<p>Read on if you are interested in a bit more ramble about how it works. \ud83d\ude42<\/p>\n<p><!--more--><strong>Generating a TinyURL<\/strong><\/p>\n<p>I&#8217;d read that TinyURL has a simple API on <a href=\"http:\/\/www.scripting.com\/stories\/2007\/06\/27\/tinyurlHasAnApi.html\">Dave Winer&#8217;s blog<\/a> about a month ago. <\/p>\n<p>You just send an HTTP request to:<br \/>\n<code>http:\/\/tinyurl.com\/api-create.php?url=http:\/\/url_you_want_to_tiny.com\/<\/code><br \/>\nand it returns you a tinyurl. Easy \ud83d\ude42<\/p>\n<p><strong>Sending an HTTP request<\/strong><\/p>\n<p>As part of my ongoing effort to keep my hand in with &#8220;real&#8221; programming despite all of the C# and Java I seem to end up doing, this is all done in C++. <\/p>\n<p>Sending an HTTP request isn&#8217;t too tough &#8211; you basically do this (with error-handling ripped out to keep things short):<\/p>\n<p>1. Initialise an Internet handle<\/p>\n<pre><code>HINTERNET hInternetSession = InternetOpen(TEXT(\"CeHttp\"), \r\n                                          INTERNET_OPEN_TYPE_PRECONFIG, \r\n                                          NULL, \r\n                                          NULL, \r\n                                          0);<\/code><\/pre>\n<p>2. Create a session for tinyurl.com<\/p>\n<pre><code>HINTERNET hConnection = InternetConnect(hInternetSession, \r\n                                        TEXT(\"tinyurl.com\"), \r\n                                        INTERNET_DEFAULT_HTTP_PORT, \r\n                                        NULL, NULL, \r\n                                        INTERNET_SERVICE_HTTP, \r\n                                        0, 1);<\/code><\/pre>\n<p>3. Open a handle for an HTTP request &#8211; where lpszServer is the rest of the TinyURL URL<br \/>\n(e.g. <code>\/api-create.php?url=http:\/\/myurl.com<\/code>)<\/p>\n<pre><code>HINTERNET hURL = HttpOpenRequest(hConnection, \r\n                                 TEXT(\"GET\"), \r\n                                 lpszServer, \r\n                                 TEXT(\"HTTP\/1.1\"), \r\n                                 NULL, NULL, 0, 1); <\/code><\/pre>\n<p>4. Send the HTTP request<\/p>\n<pre><code>HttpSendRequest(hURL, NULL, 0, NULL, 0);<\/code><\/pre>\n<p>5. Read the response<\/p>\n<pre><code>InternetReadFile(hURL, (LPSTR)cBuffer, (DWORD)1024, &dwBytesRead);<\/code><\/pre>\n<p>6. Cleanup using <code>InternetCloseHandle<\/code><\/p>\n<p>Nice and simple.<\/p>\n<p><strong>Copying the result to the clipboard<\/strong><\/p>\n<p>To copy something into the clipboard using C++, you can do this sort of thing:<\/p>\n<pre><code>OpenClipboard(GetFocus());\r\n\r\nEmptyClipboard();\r\n\t\r\nHGLOBAL hglbCopy1 = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE, dwBytesRead + 1); \r\n\r\nif( hglbCopy1 != NULL )\r\n{\r\n   char* lptstrCopy = (char*) GlobalLock(hglbCopy1); \r\n\r\n   strcpy(lptstrCopy, cBuffer);\r\n\r\n   SetClipboardData(CF_TEXT, hglbCopy1);\r\n\r\n   GlobalUnlock(hglbCopy1);\r\n}\r\n\r\nCloseClipboard();<\/code><\/pre>\n<p>where cBuffer (and dwBytesRead) is the buffer I got back from InternetReadFile &#8211; with the TinyURL URL. <\/p>\n<p>It is a bit more fiddly &#8211; 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.<\/p>\n<p>Turning the text version into unicode isn&#8217;t too difficult &#8211; you can convert it using <code>MultiByteToWideChar<\/code>.<\/p>\n<p>Then you do the above work again with the converted unicode string, but using CF_UNICODETEXT when you call SetClipboardData.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/dalelane.co.uk\/blog\/post-images\/070807-tinyurl1.gif\" hspace=\"10\" align=\"right\"\/><strong>And that&#8217;s pretty much it<\/strong><\/p>\n<p>The rest of the work was the same as the del.icio.us plugin &#8211; doing the work to get a new menu item in Pocket Internet Explorer. <\/p>\n<p>This time, I&#8217;ve tried building a version for Windows Mobile Smartphone, which I&#8217;ve not done before. Not entirely sure it&#8217;ll work, as I&#8217;ve not got a smartphone device to try it on. (It works on the emulator, but I never really trust them&#8230; \ud83d\ude42 )<\/p>\n<p><strong>Why isn&#8217;t anyone else doing this?<\/strong><\/p>\n<p>I was surprised not to see any other versions already&#8230; about a dozen copies of the <a href=\"http:\/\/dalelane.co.uk\/files\/posttodelicious.src.zip\">zipped-up source for the del.icio.us plugin<\/a> 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. <\/p>\n<p>Maybe my code isn&#8217;t as straightforward as I thought?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was surprised by the reaction to my del.icio.us plugin for Pocket Internet Explorer &#8211; 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&#8217;d known so many people would read it, I might not have made the blog [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-181","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=181"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/181\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}