Updated 2011-07-04 22:22:47 by RLE

APN This page describes the pros and cons of accessing the Win32 API using Ffidl (which is a terrific extension) versus TWAPI from my objective :-) perspective as the author of the latter.

Of course, there is no reason you cannot use TWAPI and Ffidl together, and in fact I view them more complementary than competitive. Use TWAPI if possible and Ffidl to access an API that the former does not support.

To understand the two clearly, it's important also to introduce winapi, Emmanuel Frecons' Ffidl-based library for "access to the low-level Win32 API". In comparison with twapi, winapi is closer to "raw" Win32. APN respectfully differs - if you prefer the raw Win32 API as documented in Microsoft's SDK, TWAPI supports that every bit as well.

Pros of Ffidl

  • open interface - you can create wrappers to any API on the fly. With TWAPI, if the extension does not have support for the specific API you need compiled in, you are out of luck.
  • support for Win95/Win98. TWAPI supports NT-based systems only - V1.x needs NT 4.0 or later, V2.x needs Win2K or later.
  • lightweight. You only "carry around code" for the specific API you want to access plus Ffidl itself.
  • Thread safe? I'm not sure if Ffidl is thread-safe but TWAPI currently cannot be called from interpreters running in multiple threads.

Pros of TWAPI

  • provides both low level (direct Win32 API like winapi) and high level interfaces
  • The high level interface is far, far easier to use than either Ffidl or TWAPI's low level interface. Try the examples below to see the difference in productivity.
  • Support for 64-bit platforms and 64-bit Tcl (as of V3.0). Ffidl does not seem to support 64-bit Windows.
  • AFAIK some things simply cannot be done or are very hard/cumbersome to do in Ffidl, particularly those involving callbacks that require multiple threads or complex/variable size data structures. Examples are supporting Windows hot keys or monitoring the file system.
  • You need minimal Windows knowledge (which is as it should be - you should not need to be a Win32 API guru to script in Tcl).
  • incorporates a lot of Windows quirks and platform differences.

As an example of the differences in complexity, convenience and ease of use, suppose you wanted to find the user account associated with a process. In TWAPI, you would simply say
 twapi::get_process_info PID -user

You need very little Windows knowledge for this! To do this via Ffidl, you would need to know about OpenProcess, OpenProcessToken, access rights and masks, GetTokenInformation, LookupAccountSid, WTSEnumerateProcesses and which can be used on which version of Windows and what privileges need to be obtained to use them. You would have to find out, through experience because it is not documented, that not only are there OS version differences but service pack differences, particularly when retrieving information about "privileged" processes like svchost. Then once you have this knowledge, try coding it all using Ffidl or the TWAPI low level interface.

Similarly, try implementing some of the examples at [1] using Ffidl to get a feel of difference in complexity.