Version 6 of Comparing TWAPI and Ffidl on Windows

Updated 2006-05-22 13:56:30

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.

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 and pre-4.0 NT. TWAPI only supports NT4.0 SP3+, Windows 2000, XP and Windows 2003.
  • lightweight. You only "carry around code" for the specific API you want to access. With TWAPI, you have to lug around the whole extension.
  • Thread safe? I'm not sure if Ffidl is thread-safe but TWAPI currently cannot be called from interpreters running in multiple threads. Or actually, it can be called from multiple threads but the effect will not be pretty :-)

Pros of TWAPI

  • provides both low level (direct Win32 API) 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.
  • 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 [L1 ] using Ffidl to get a feel of difference in complexity.


Category Windows