Created
November 10, 2012 03:00
-
-
Save uduki/4049676 to your computer and use it in GitHub Desktop.
HaskellからDXライブラリを使う方法(C#用パッケージ内のDLLを使用)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ HaskellからDXライブラリを使う ] | |
1, まず本家からC#用パッケージをダウンロードしてくる。 | |
http://homepage2.nifty.com/natupaji/DxLib/dxdload.html | |
2, (1)でダウンロードしたzipから、DxLib.dllとDxDLL.hだけ抜き取ってくる。 | |
DxLib.dllは実行時に使う為。 | |
DxDLL.hはDLL内の関数の宣言内容を人間が把握する為。 | |
3, Haskellのソースコードを書く。DxDLL.hを参照しながら以下のコードを書く(test.hs)。 | |
コード例: | |
{-# LANGUAGE ForeignFunctionInterface #-} | |
import Foreign.C.Types | |
foreign import stdcall "dx_DxLib_Init" dxlibInit:: IO CInt | |
foreign import stdcall "dx_DxLib_End" dxlibEnd:: IO CInt | |
foreign import stdcall "dx_ChangeWindowMode" dxChangeWindowMode :: CInt -> IO CInt | |
foreign import stdcall "dx_WaitKey" dxWaitKey :: IO CInt | |
main :: IO () | |
main = do | |
dxChangeWindowMode 1 | |
dxlibInit | |
dxWaitKey | |
dxlibEnd | |
return () | |
4, test.hsとDxLib.dllを同じディレクトリに置く。 | |
5, 次のコマンドでコンパイルする。 | |
ghc -optl --enable-stdcall-fixup -L. -lDxLib test.hs | |
6, test.exeが出来上がっているので実行すると動く。 | |
補足: -optl --enable-stdcall-fixupを何故付けているか? | |
ghcがdllからインポートする関数の名前を解決する際に、stdcallでは関数名の後ろに@<数字>を付けた名前で行われる。 | |
しかしDxLib.dll内にある関数の名前には@<数字>が無い(defファイルをdllから作ると実際に無い)為、この部分の修正が必要になる。 | |
上記のやり方では-optl --enable-stdcall-fixupを付けなくても名前の修正は行われるが、警告が大量発生することになる。 | |
なので、-optl --enable-stdcall-fiupを渡して修正を明示することで警告を抑制している。 | |
この場合は@<数字>から無しへの変換なので、特に問題は起こらないと思う。(逆の場合は問題が出る可能性が高い) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment