调用一些win32 api函数时,Haskell未定义引用

wil*_*y-s 5 c winapi haskell ffi

我正在尝试编写一些win32包中未包含的win32 API函数的绑定,但遇到了一些困难.在下面的代码中,EnumWindows和GetWindow的绑定工作正常,但GetWindowText和GetWindowTextLength的绑定不能:

{-# LANGUAGE ForeignFunctionInterface #-}

import Foreign.Ptr
import Graphics.Win32.GDI.Types (HWND)
import System.Win32.Types (ptrToMaybe)
import Foreign.C
import Foreign.Marshal.Alloc (free)
import Control.Applicative ((<$>))

gW_CHILD = 5::CInt

getWindow :: HWND -> CInt -> IO (Maybe HWND)
getWindow hwnd cint = ptrToMaybe <$> (c_getWindow hwnd cint)
foreign import stdcall "windows.h GetWindow"
  c_getWindow :: HWND -> CInt -> IO HWND

foreign import stdcall "windows.h EnumWindows"
  enumWindows :: (FunPtr (HWND -> Ptr b -> IO a)) -> CInt -> IO CInt  

foreign import stdcall "windows.h GetWindowText"
  getWindowText ::  HWND -> CString -> CInt -> IO CInt 

foreign import stdcall "windows.h GetWindowTextLength"
  getWindowTextLength ::  HWND -> IO CInt 

foreign import ccall "wrapper"
  wrapEnumWindowsProc :: (HWND -> Ptr a -> IO CInt) -> IO (FunPtr (HWND -> Ptr a-> IO CInt))

findFirstNamedChildWindow :: HWND -> Ptr a -> IO CInt
findFirstNamedChildWindow hwnd _ = do
      mchild <- getWindow hwnd gW_CHILD
      case mchild of 
            Just hchwnd -> do                
                  clen <- getWindowTextLength (hchwnd)
                  case clen of 
                         0 -> return 1 
                         _ -> do
                              str <- newCString (replicate (fromEnum clen) ' ')
                              getWindowText hwnd str $ clen+1
                              print =<< peekCString str
                              free str >> return 0                                          
            Nothing -> return 1
main = do
      enptr <- wrapEnumWindowsProc findFirstNamedChildWindow
      enumWindows enptr 0
      return ()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

C:\Users\me>ghc nc.hs
Linking nc.exe ...
nc.o:fake:(.text+0x958): undefined reference to `GetWindowText@12'
nc.o:fake:(.text+0xe12): undefined reference to `GetWindowTextLength@4'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

所有4个函数都在User32.dll中.GHC版本是7.8.2(32位),OS是Windows 7(64).

如果我添加这个C文件:

#include <windows.h>

int getWindowText (HWND hwnd, char* str, int len) {
    return GetWindowText (hwnd, str, len);
    }
int getWindowTextLength (HWND hwnd) {
    return GetWindowTextLength (hwnd);
    }
Run Code Online (Sandbox Code Playgroud)

并更改导入调用

foreign import call "getWindowText"

foreign import call "getWindowTextLength" 
Run Code Online (Sandbox Code Playgroud)

一切都按预期工作.到底是怎么回事?关于隐式演员或类似的东西?我尝试了Foreign.C.String中的宽字符串函数但是没有改变任何东西.(也就是传递字符串缓冲区以便C写入的预期方法,还是有更好的方法?)

arx*_*arx 9

大多数处理字符串的Windows函数有两个版本,一个带有A后缀的ANSI版本和带有后缀的Unicode版本W.

例如,GetWindowText实际上是导出为两个函数,GetWindowTextAGetWindowTextW.这些名称显示在文档底部附近.

一个LPTSTR参数被解释为LPSTRA版本LPWSTRW版本.您可以使用任一功能,但显然您必须使用适当的字符串类型.

C版本有效,因为GetWindowText它实际上是一个C宏,可以扩展为任何一个GetWindowTextAGetWindowTextW取决于您是否UNICODE定义了宏.