Brian Snyder
Brian Snyder

Reputation: 73

How do I access a third party DLL from a Tcl script?

I am trying to learn how to access a third party DLL from a Tcl script. There are a lot of tidbits of information on this topic in numerous places, but nothing that provides a nice, concise start-to-finish description of how to do it. I've tried a number of things, with varying degrees of failure. I've gotten the closest to success using SWIG and Visual Studio Express 2012 (both of which I am a complete newbie...)

I saw a previous response to a similar question here How to create a DLL with SWIG from Visual Studio 2010, in which the goal was to create a DLL from C code. I do not have the source for the DLL, only the .dll, .h, and .lib files from the vendor who created the DLL.

The responder in that post had instructions that are very close to what I'm looking for, I think. He stated that the process for wrapping a DLL is similar, and to ask if instructions were needed to accomplish that. I'm hoping he responds to this post...

(Disclaimer: I'm a complete newbie to this forum as well, and I couldn't figure out a way to follow up on that post directly. Please forgve my ignorance...)

Can anyone povide detailed instructions on how to accomplish my goal? Thanks in advance!

Upvotes: 3

Views: 401

Answers (1)

andy mango
andy mango

Reputation: 1551

There are two options for accessing shared libraries in Tcl that I have used.

  1. Write an compiled "C" extension. This can be done either manually coded or via SWIG. The extension is usually little more than a piece of glue code between the Tcl interpreter and the functions in the shared library. SWIG is not my strong suit so others would have to help you there. If you write a "C" extension, there is a sample extension in the Tcl sources and quite a bit of help in the Tcl wiki.
  2. Use a general shared object caller. The one I usually use is "ffidl". A simple google will point you to it. To use a general shared object caller, you typically have to invoke some Tcl commands to define the interface of the called function and then it deals with loading the library, finding the entry points and actually invoking the function and returning a result.

Which is best depends upon your circumstances. If the library is relatively small, I usually just code up the extension by hand. If it is large and I only want to call a few functions, then I usually use ffidl to do the heavy lifting.

Upvotes: 2

Related Questions