Writing a COM client in Haskell
This is most likely GHC specific. I am currently using GHC 6.8.3 and any other version may not work.
It should be obvious that we need FFI to make C calls to Windows COM components. I also use Krasimir Angelov's hscom package. Hscom implements the machinery needed to work with COM components.
It should be noted here that another subtle requirement is GHC extensions needed to make this all work. I had a tough time getting around a compilation problem. Krasimir, who is the author of hscom, graciously helped me with that problem. You can read the exact issue at http://www.nabble.com/Unboxing-VT_VARIANT-in-hscom-td19044443.html#a19044443. The short summary is that you need to enable GHC extensions with the switch -fglasgow-exts .
GHC package names seem to have changed over time. I spent a few hours chasing why import Process was failing. Well, it is now import System.Process.
In my next blog, I will show a minimal COM client written in Haskell.
Saturday, August 23, 2008
Getting started with Haskell
Installation
As you can see on haskell.org, there are four implementations of Haskell. I am using GHC. Installation on MS Windows presents no serious issues (unfortunately, many fine things are Linux only with Windows implementation either non-existent or a step child like effort. I am glad GHC team seems to consider Windows effort important enough.
Packages
Packages are at http://hackage.haskell.org/packages/archive/pkg-list.html. I haven't managed to get it working on my system yet. Installing packages one at a time has worked out okay. Here is a sample of how I do it.
Issues
It is possible you will run into some issues in building. For e.g., hscom package complains of missing files as below.
I got around that by copying the missing file.
Installing packages usually only takes a few steps but getting cabal to work would be much better (presuming that it takes care all dependencies of the package I am trying to install).
Installation
As you can see on haskell.org, there are four implementations of Haskell. I am using GHC. Installation on MS Windows presents no serious issues (unfortunately, many fine things are Linux only with Windows implementation either non-existent or a step child like effort. I am glad GHC team seems to consider Windows effort important enough.
Packages
Packages are at http://hackage.haskell.org/packages/archive/pkg-list.html. I haven't managed to get it working on my system yet. Installing packages one at a time has worked out okay. Here is a sample of how I do it.
- Download the package or use darcs client to check out the sources
- cd to the directory which contains Setup.hs or Setup.lhs
- Run: ghc --make Setup.hs
- Run: setup configure
- Run: setup build
- Run: setup install
Issues
It is possible you will run into some issues in building. For e.g., hscom package complains of missing files as below.
C:\tools\haskell\hscom>setup makefile
Preprocessing library hscom-0.1...
Generating Makefile hscom-0.1...
C:\tools\haskell\hscom>setup configure
Configuring hscom-0.1...
Warning: No license-file field.
C:\tools\haskell\hscom>dir dis
C:\tools\haskell\hscom>setup build
Preprocessing library hscom-0.1...
Building hscom-0.1...
src\Foreign\COM\Automation\SafeArray.hsc:22:22:
Can't find dist\build/Foreign/COM/Automation/Variant.hs-boot
I got around that by copying the missing file.
C:\tools\haskell\hscom>copy src\Foreign\COM\Automation\Variant.hs-boot dist\buil
d\Foreign\COM\Automation
1 file(s) copied.
C:\tools\haskell\hscom>setup configure
C:\tools\haskell\hscom>setup build
Preprocessing library hscom-0.1...
Building hscom-0.1...
[ 1 of 17] Compiling Foreign.COM.Automation.Variant[boot] ( dist\build/Foreign/C
OM/Automation/Variant.hs-boot, dist\build/Foreign/COM/Automation/Variant.o-boot
)
[ 2 of 17] Compiling Foreign.COM.Automation.BSTR ( src/Foreign/COM/Automation/BS
TR.hs, dist\build/Foreign/COM/Automation/BSTR.o )
[ 3 of 17] Compiling Foreign.COM.Marshal.Alloc ( src/Foreign/COM/Marshal/Alloc.h
s, dist\build/Foreign/COM/Marshal/Alloc.o )
[ 4 of 17] Compiling Foreign.COM.Marshal.Exception ( dist\build/Foreign/COM/Mars
hal/Exception.hs, dist\build/Foreign/COM/Marshal/Exception.o )
[ 5 of 17] Compiling Foreign.COM.Automation.Currency ( dist\build/Foreign/COM/Au
tomation/Currency.hs, dist\build/Foreign/COM/Automation/Currency.o )
[ 6 of 17] Compiling Foreign.COM.Automation.Decimal ( dist\build/Foreign/COM/Aut
omation/Decimal.hs, dist\build/Foreign/COM/Automation/Decimal.o )
[ 7 of 17] Compiling Foreign.COM.Automation.SafeArray ( dist\build/Foreign/COM/A
utomation/SafeArray.hs, dist\build/Foreign/COM/Automation/SafeArray.o )
[ 8 of 17] Compiling Foreign.COM.Registry ( dist\build/Foreign/COM/Registry.hs,
dist\build/Foreign/COM/Registry.o )
C:\DOCUME~1\pprakash\LOCALS~1\Temp\ghc2832_0\ghc2832_0.hc: In function `s78n_ent
ry':
C:\DOCUME~1\pprakash\LOCALS~1\Temp\ghc2832_0\ghc2832_0.hc:156: warning: implicit
declaration of function `com_hresult_from_win32'
[ 9 of 17] Compiling Foreign.COM.Marshal.VTable ( src/Foreign/COM/Marshal/VTable
.hs, dist\build/Foreign/COM/Marshal/VTable.o )
[10 of 17] Compiling Foreign.COM.Marshal ( src/Foreign/COM/Marshal.hs, dist\buil
d/Foreign/COM/Marshal.o )
[11 of 17] Compiling Foreign.COM.GUID ( src/Foreign/COM/GUID.hs, dist\build/Fore
ign/COM/GUID.o )
[12 of 17] Compiling Foreign.COM.Client ( dist\build/Foreign/COM/Client.hs, dist
\build/Foreign/COM/Client.o )
[13 of 17] Compiling Foreign.COM.Server ( src/Foreign/COM/Server.hs, dist\build/
Foreign/COM/Server.o )
[14 of 17] Compiling Foreign.COM.Automation.IDispatch ( dist\build/Foreign/COM/A
utomation/IDispatch.hs, dist\build/Foreign/COM/Automation/IDispatch.o )
[15 of 17] Compiling Foreign.COM.Automation.Variant ( dist\build/Foreign/COM/Aut
omation/Variant.hs, dist\build/Foreign/COM/Automation/Variant.o )
[16 of 17] Compiling Foreign.COM.Automation ( src/Foreign/COM/Automation.hs, dis
t\build/Foreign/COM/Automation.o )
[17 of 17] Compiling Foreign.COM.ConnectionPoint ( dist\build/Foreign/COM/Connec
tionPoint.hs, dist\build/Foreign/COM/ConnectionPoint.o )
/usr/bin/ar: creating dist\build\libHShscom-0.1.a
C:\tools\haskell\hscom>setup install
Installing: C:\Program Files\Haskell\hscom-0.1\ghc-6.8.3
Registering hscom-0.1...
Reading package info from "dist\\installed-pkg-config" ... done.
Saving old package config file... done.
Writing new package config file... done.
Installing packages usually only takes a few steps but getting cabal to work would be much better (presuming that it takes care all dependencies of the package I am trying to install).
Learning Haskell Redux
When you are looking for a nice language that doesn't suck, which would inevitably rule out the languages most of us use at work, there are many choices. Usually, I am torn between Lisp, Erlang and Haskell. I have become quite comfortable with Lisp but Erlang and Haskell still present a high starting effort.
So, I started toying with another language JRuby. Unfortunately, the issue of FFI in JRuby is still a challenge and JRuby still has a few rough edges. After a few attempts at using it, I gave up and decided to try Haskell instead. One of my requirements was to be able to implement COM client programming and Haskell turned out to be not such a challenge after all. I will blog more on my COM client in Haskell later.
Picking up Haskell after a couple of years has been interesting. Much to my surprise, my understanding of Haskell has improved a lot. Interestingly, Lisp proved to be quite a challenge in the beginning but at some point, it became quite natural. The best way I can describe that is that the parenthesis seemed to disappear and suddenly everything became very clear. I am having a similar experience with Haskell at the moment. Suddenly, I really understand the benefits of monadic style of programming. The understanding has progressed beyond that is gained by reading what others have written.
Speaking of other's work, I discovered a new Haskell book in progress, http://book.realworldhaskell.org/. The few chapters I have read have been very useful. I am glad to a book which makes this wonderful language more accessible.
Haskell is a complex language and a real mind bender. However, a pragmatic approach to learning it is likely to be an easier course for most people. Hopefully, I will be able to blog as I go along and end up with something worth reading.
When you are looking for a nice language that doesn't suck, which would inevitably rule out the languages most of us use at work, there are many choices. Usually, I am torn between Lisp, Erlang and Haskell. I have become quite comfortable with Lisp but Erlang and Haskell still present a high starting effort.
So, I started toying with another language JRuby. Unfortunately, the issue of FFI in JRuby is still a challenge and JRuby still has a few rough edges. After a few attempts at using it, I gave up and decided to try Haskell instead. One of my requirements was to be able to implement COM client programming and Haskell turned out to be not such a challenge after all. I will blog more on my COM client in Haskell later.
Picking up Haskell after a couple of years has been interesting. Much to my surprise, my understanding of Haskell has improved a lot. Interestingly, Lisp proved to be quite a challenge in the beginning but at some point, it became quite natural. The best way I can describe that is that the parenthesis seemed to disappear and suddenly everything became very clear. I am having a similar experience with Haskell at the moment. Suddenly, I really understand the benefits of monadic style of programming. The understanding has progressed beyond that is gained by reading what others have written.
Speaking of other's work, I discovered a new Haskell book in progress, http://book.realworldhaskell.org/. The few chapters I have read have been very useful. I am glad to a book which makes this wonderful language more accessible.
Haskell is a complex language and a real mind bender. However, a pragmatic approach to learning it is likely to be an easier course for most people. Hopefully, I will be able to blog as I go along and end up with something worth reading.
Subscribe to:
Posts (Atom)