  	pFIELD <- mallocBytes (#const sizeof(FIELD))	
  	res <- sqlAllocStmt hDBC ((#ptr FIELD, hSTMT) pFIELD)
  	unless (sqlSuccess res) (free pFIELD)
-@@ -258,10 +266,7 @@
+@@ -267,10 +270,7 @@
  	let handleResult res = do
  		unless (sqlSuccess res) (free pFIELD)
  		handleSqlResult (#const SQL_HANDLE_STMT) hSTMT res

 		) where
 
 import Data.Word(Word32, Word16)
@@ -39,7 +42,6 @@
 import Data.Dynamic
 import Foreign
 import Foreign.C
-import qualified Foreign.Concurrent as C
 import Control.Monad(when,unless)
 import Control.Exception (throwDyn, catchDyn, dynExceptions, Exception(..), finally)
 import System.IO.Unsafe
@@ -62,23 +64,23 @@
 type SQLLEN       = SQLINTEGER
 type SQLULEN      = SQLINTEGER
 
-foreign import stdcall "sqlext.h SQLAllocEnv" sqlAllocEnv :: Ptr HENV -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLFreeEnv" sqlFreeEnv :: HENV -> IO ()
-foreign import stdcall "sqlext.h SQLAllocConnect" sqlAllocConnect :: HENV -> Ptr HDBC -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLFreeConnect" sqlFreeConnect:: HDBC -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLConnect" sqlConnect :: HDBC -> CString -> Int -> CString -> Int -> CString -> Int -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLDisconnect" sqlDisconnect :: HDBC -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLAllocStmt" sqlAllocStmt :: HDBC -> Ptr HSTMT -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLFreeStmt" sqlFreeStmt :: HSTMT -> SQLUSMALLINT -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLNumResultCols" sqlNumResultCols :: HSTMT -> Ptr SQLUSMALLINT -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLDescribeCol" sqlDescribeCol :: HSTMT -> SQLUSMALLINT -> CString -> SQLSMALLINT -> Ptr SQLSMALLINT -> Ptr SQLSMALLINT -> Ptr SQLULEN -> Ptr SQLSMALLINT -> Ptr SQLSMALLINT -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLBindCol" sqlBindCol :: HSTMT -> SQLUSMALLINT -> SQLSMALLINT -> Ptr a -> SQLLEN -> Ptr SQLINTEGER -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLFetch" sqlFetch :: HSTMT -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLGetDiagRec" sqlGetDiagRec :: SQLSMALLINT -> SQLHANDLE -> SQLSMALLINT -> CString -> Ptr SQLINTEGER -> CString -> SQLSMALLINT -> Ptr SQLSMALLINT -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLExecDirect" sqlExecDirect :: HSTMT -> CString -> Int -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLSetConnectOption" sqlSetConnectOption :: HDBC -> SQLUSMALLINT -> SQLULEN -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLTransact" sqlTransact :: HENV -> HDBC -> SQLUSMALLINT -> IO SQLRETURN
-foreign import stdcall "sqlext.h SQLGetData" sqlGetData :: HSTMT -> SQLUSMALLINT -> SQLSMALLINT -> Ptr () -> SQLINTEGER -> Ptr SQLINTEGER -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLAllocEnv" sqlAllocEnv :: Ptr HENV -> IO SQLRETURN
+foreign import ccall "sqlext.h &SQLFreeEnv" sqlFreeEnv_p :: FunPtr (HENV -> IO ())
+foreign import ccall "sqlext.h SQLAllocConnect" sqlAllocConnect :: HENV -> Ptr HDBC -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLFreeConnect" sqlFreeConnect:: HDBC -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLConnect" sqlConnect :: HDBC -> CString -> Int -> CString -> Int -> CString -> Int -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLDisconnect" sqlDisconnect :: HDBC -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLAllocStmt" sqlAllocStmt :: HDBC -> Ptr HSTMT -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLFreeStmt" sqlFreeStmt :: HSTMT -> SQLUSMALLINT -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLNumResultCols" sqlNumResultCols :: HSTMT -> Ptr SQLUSMALLINT -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLDescribeCol" sqlDescribeCol :: HSTMT -> SQLUSMALLINT -> CString -> SQLSMALLINT -> Ptr SQLSMALLINT -> Ptr SQLSMALLINT -> Ptr SQLULEN -> Ptr SQLSMALLINT -> Ptr SQLSMALLINT -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLBindCol" sqlBindCol :: HSTMT -> SQLUSMALLINT -> SQLSMALLINT -> Ptr a -> SQLLEN -> Ptr SQLINTEGER -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLFetch" sqlFetch :: HSTMT -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLGetDiagRec" sqlGetDiagRec :: SQLSMALLINT -> SQLHANDLE -> SQLSMALLINT -> CString -> Ptr SQLINTEGER -> CString -> SQLSMALLINT -> Ptr SQLSMALLINT -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLExecDirect" sqlExecDirect :: HSTMT -> CString -> Int -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLSetConnectOption" sqlSetConnectOption :: HDBC -> SQLUSMALLINT -> SQLULEN -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLTransact" sqlTransact :: HENV -> HDBC -> SQLUSMALLINT -> IO SQLRETURN
+foreign import ccall "sqlext.h SQLGetData" sqlGetData :: HSTMT -> SQLUSMALLINT -> SQLSMALLINT -> Ptr () -> SQLINTEGER -> Ptr SQLINTEGER -> IO SQLRETURN
 
 
 -- | A 'Connection' type represents a connection to a data source, through which you can operate on the data source. 
@@ -138,7 +140,13 @@
    | SqlFetchNull
    		{ seFieldName :: String
 		}
-   deriving (Show, Typeable)
+   deriving (Show)
+
+sqlErrorTc :: TyCon
+sqlErrorTc = mkTyCon "Database.ODBC.HSQL.SqlError"
+
+instance Typeable SqlError where
+	typeOf _ = mkAppTy sqlErrorTc []
    
 -----------------------------------------------------------------------------------------
 -- routines for handling exceptions
@@ -191,7 +199,7 @@
 	hEnv <- peek phEnv
 	free phEnv
 	handleSqlResult 0 nullPtr res
-	C.newForeignPtr hEnv (sqlFreeEnv hEnv)
+	newForeignPtr sqlFreeEnv_p hEnv
 
 -----------------------------------------------------------------------------------------
 -- Connect/Disconnect
@@ -247,9 +255,9 @@
 	sqlFreeStmt hSTMT 0 >>= handleSqlResult (#const SQL_HANDLE_STMT) hSTMT
 	free pFIELD
 

 	sqlNumResultCols hSTMT ((#ptr FIELD, fieldsCount) pFIELD) >>= handleResult
 	count <- (#peek FIELD, fieldsCount) pFIELD
 	(fields, bufSize) <- getFieldDefs hSTMT pFIELD 1 count
@@ -295,25 +295,32 @@
 				(fields, fullBufSize) <- getFieldDefs hSTMT pFIELD (n+1) count
 				return ((name,sqlType,toBool nullable):fields, max bufSize fullBufSize)
 				
