using a win32 dll

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

using a win32 dll

Kees Bleijenberg

I made a Delphi dll (32 bits windows). This dll exports a function named getPngVersion. This is a function with no arguments that returns a pointer to a array of chars.

If I examine the dll with a tools like tdump, I can see the function getPngVersion on index 1.

Now I want to use this dll in Haskell:

 

{-# LANGUAGE ForeignFunctionInterface #-}

module Main(

main

)

 

where

import Control.Monad

import Foreign.C

import Foreign.Marshal.Alloc

import Foreign.Marshal.Array

import System.Win32.Types

 

foreign import stdcall "glasPng getPngVersion"  getPngDllVersion :: IO CString

 

main :: IO ()

main = do

         s <- getPngDllVersion

         putStrLn (show s)

 

Compiling this with ghc --make glasPng.hs gives:

GlasPng.o: fake: (.text +0x82): undefined reference to  ‘getPngVersion@0’ collect2 : Id returned 1 exit status

 

I wonder what went wrong. In the foreign import line I use “glasPng…. “ to tell ghc, it has to searh in glasPng.dll (changing glasPng to glasPng.dll doesn’t help). I’ve changed the name of the function in  the dll to getPngVersion@0 but no luck.  I’ve read and tested the last example at http://stackoverflow.com/questions/1027246/haskell-foreign-import-stdcall-on-dll-function  (win32_getComputername,  this works!). I wonder how  haskell knows which dll to use in this example?)

Probably I’am doing something wrong with the way I tell ghc which dll to link or….

 

Any ideas?

 

Kees


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: using a win32 dll

Robert Jakob
On Wed, 29 May 2013 09:13:09 +0200
"Kees Bleijenberg" <[hidden email]> wrote:

> I made a Delphi dll (32 bits windows). This dll exports a function
> named getPngVersion. This is a function with no arguments that
> returns a pointer to a array of chars.
Did you annotate the exported function with stdcall? The calling
conventions of Delphi are different to that of C. I had problems with
that sometimes (not with Haskell, but with C)


R.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: using a win32 dll

Krzysztof Skrzętnicki
In reply to this post by Kees Bleijenberg
My first guess would be missing -l parameter. The examples you have linked read:
ghc --make compname.hs -lkernel32

Best regards,
Krzysztof Skrzętnicki

On Wed, May 29, 2013 at 9:13 AM, Kees Bleijenberg <[hidden email]> wrote:

I made a Delphi dll (32 bits windows). This dll exports a function named getPngVersion. This is a function with no arguments that returns a pointer to a array of chars.

If I examine the dll with a tools like tdump, I can see the function getPngVersion on index 1.

Now I want to use this dll in Haskell:

 

{-# LANGUAGE ForeignFunctionInterface #-}

module Main(

main

)

 

where

import Control.Monad

import Foreign.C

import Foreign.Marshal.Alloc

import Foreign.Marshal.Array

import System.Win32.Types

 

foreign import stdcall "glasPng getPngVersion"  getPngDllVersion :: IO CString

 

main :: IO ()

main = do

         s <- getPngDllVersion

         putStrLn (show s)

 

Compiling this with ghc --make glasPng.hs gives:

GlasPng.o: fake: (.text +0x82): undefined reference to  ‘getPngVersion@0’ collect2 : Id returned 1 exit status

 

I wonder what went wrong. In the foreign import line I use “glasPng…. “ to tell ghc, it has to searh in glasPng.dll (changing glasPng to glasPng.dll doesn’t help). I’ve changed the name of the function in  the dll to getPngVersion@0 but no luck.  I’ve read and tested the last example at http://stackoverflow.com/questions/1027246/haskell-foreign-import-stdcall-on-dll-function  (win32_getComputername,  this works!). I wonder how  haskell knows which dll to use in this example?)

Probably I’am doing something wrong with the way I tell ghc which dll to link or….

 

Any ideas?

 

Kees


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: using a win32 dll

Kees Bleijenberg
In reply to this post by Robert Jakob
> On Wed, 29 May 2013 09:13:09 +0200
> "Kees Bleijenberg" <[hidden email]> wrote:

> > I made a Delphi dll (32 bits windows). This dll exports a function
> > named getPngVersion. This is a function with no arguments that
> > returns a pointer to a array of chars.

> Did you annotate the exported function with stdcall? The calling
> conventions of Delphi are different to that of C. I had problems with
> that sometimes (not with Haskell, but with C)
----------------

Thanks for your answer.
Yes, I did set the calling convention to stdcall for that function in the
dll. I think, setting the calling convention wrong, leads to runtime
problems. This is a link/compile problem.

Kees


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: using a win32 dll

Kees Bleijenberg
In reply to this post by Krzysztof Skrzętnicki

Thank you Krzystof! This did the trick. Now I also have a better understanding what is going on. Great.

 

Kees

 

Van: Krzysztof Skrzętnicki [mailto:[hidden email]]
Verzonden: woensdag 29 mei 2013 11:58
Aan: Kees Bleijenberg
Onderwerp: Re: [Haskell-cafe] using a win32 dll

 

Oh, I didn't mean kernel32. And -L provides search path not the actual library. The idea was to use -l<yourLibrary.dll> , so it would be -lpngDLL.dll, provided it is in library search path. If not use -Ldirectory-with-dll -lpngDLL.dll 

 

On Wed, May 29, 2013 at 11:49 AM, Kees Bleijenberg <[hidden email]> wrote:

I’ve tried to compile it with –l kernel32, but  nothing changed. I’ve also tried  -LpngDll.dll but that didn’t work either.

Kees


My first guess would be missing -l parameter. The examples you have linked read:
ghc --make compname.hs -lkernel32

Best regards,
Krzysztof Skrzętnicki

On Wed, May 29, 2013 at 9:13 AM, Kees Bleijenberg <[hidden email]> wrote:
I made a Delphi dll (32 bits windows). This dll exports a function named getPngVersion. This is a function with no arguments that returns a pointer to a array of chars.
If I examine the dll with a tools like tdump, I can see the function getPngVersion on index 1.
Now I want to use this dll in Haskell:

{-# LANGUAGE ForeignFunctionInterface #-}
module Main(
      main
)

where
import Control.Monad
import Foreign.C
import Foreign.Marshal.Alloc
import Foreign.Marshal.Array
import System.Win32.Types

foreign import stdcall "glasPng getPngVersion"  getPngDllVersion :: IO CString

main :: IO ()
main = do
         s <- getPngDllVersion
         putStrLn (show s)

Compiling this with ghc --make glasPng.hs gives:
GlasPng.o: fake: (.text +0x82): undefined reference to  ‘getPngVersion@0’ collect2 : Id returned 1 exit status

I wonder what went wrong. In the foreign import line I use “glasPng…. “ to tell ghc, it has to searh in glasPng.dll (changing glasPng to glasPng.dll doesn’t help). I’ve changed the name of the function in  the dll to getPngVersion@0 but no luck.  I’ve read and tested the last example at http://stackoverflow.com/questions/1027246/haskell-foreign-import-stdcall-on-dll-function  (win32_getComputername,  this works!). I wonder how  haskell knows which dll to use in this example?)
Probably I’am doing something wrong with the way I tell ghc which dll to link or….

Any ideas?

Kees

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

 


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe