Posts

Showing posts from February, 2010

Call JavaScript in a TWebBrowser and get a result back

Calling a JavaScript function in a TWebBrowser is easy, but getting a return value from it is hard. I've been struggling for ages to find an answer to this, and lots of users have asked. My article " How to call JavaScript functions in a TWebBrowser from Delphi " goes into details. Christian Sciberras suggested a solution that depended on modifying the HTML source to include a hidden input field and modifying the function stores its result in the field. I've wanted a tidier solution that didn't involve changing either the HTML code or the JavaScript function, because we can't always do that. I've now got a solution based on Christian's: Create a hidden input field in the current document with a unique id. Wrap the required function call in JavaScript that calls the function and stores its return value in the input field. Read the value from the input field and return it. It's a bit of a dirty hack, and it only works if the function&#

Code Library Newsletter

Just lately I've spent a lot of time bringing my rather dusty code library up to date. The library has been established as a project on GoogleCode (ddab-lib) and the code is gradually being moved into the project's Subversion repository. A wiki documenting the library is also hosted on the same GoogleCode project. There has been enough activity here to warrant setting up a newsletter to notify changes to the library. If interested you can sign up on my main site . I aim to launch it early in March.

Unicode environment blocks and CreateProcess

I've just been struggling with a Unicode conversion of some code that passes a custom environment block to a child process. On Unicode Delphi compilers the code produces a Unicode environment block, and I'd done something like this... procedure ExecProgWithUnicodeEnv ( const ProgName : string ; EnvBlock : Pointer ) ; var SI : TStartupInfo ; PI : TProcessInformation ; SafeProgName : string ; begin SafeProgName := ProgName ; // workaround for read-only lpCommandLine UniqueString ( SafeProgName ) ; // param to CreateProcessW FillChar ( SI , SizeOf ( SI ) , 0 ) ; SI . cb := SizeOf ( SI ) ; CreateProcess ( nil , PChar ( ProgName ) , nil , nil , True , 0 , EnvBlock , nil , SI , PI ) ; end ; If you're wandering about that UniqueString stuff, check out this post . The assumption was that CreateProcessW would expect a Unicode environment block. Wrong! It actually still expects an ANSI block by default.

URL Encoding

I've being reviewing the URI encoding code from the Code Snippets Database and I realised that it doesn't comply with RFC 3986 . So here's my first attempt at some compliant code. According to the RFC: "the data should first be encoded as octets according to the UTF-8 character encoding [STD63]; then only those octets that do not correspond to characters in the unreserved set should be percent-encoded." So, we define the URIEncode function to operate on the UTF8String type. It's easy to encode UnicodeString and AnsiString into UTF using the System unit's UTF8Encode overloaded functions. You can overload URIEncode to do the UTF8 conversion, but I haven't done here. There's a nice shortcut we can take when url encoding. Remember only unreserved characters are percent-encoded. The set of unreserved characters is: const cURLUnreservedChars = [ 'A' .. 'Z' , 'a' .. 'z' , '0' .. '

Welcome to the new DelphiDabbler blog

Welcome to this new blog, which I'm thinking of as an extension to DelphiDabbler.com where more peripheral programming stuff can be discussed. I'll also use it for news of happenings on the main site. Will I keep up with it? Let's see how it goes!