2

MSDN DLLの例からMathFuncsDll.dllを作成し、呼び出し元の.cppを実行すると正常に機能しました。今、これを次のようなctypesを使用してIPythonにロードしようとしています

import ctypes
lib = ctypes.WinDLL('MathFuncsDll.dll')

正しいフォルダにあると

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 28: ordinal not in range(128)

同様にPythonシェルでは、これにより

WindowsError: [Error 193] %1 is not a valid Win32 application

何を変更すればよいですか?うーん、それはWin 7 64ビット対いくつかの32ビットdllか何か正しいかもしれませんか?後でまた時間があるときに確認します。

4

1 に答える 1

3

ctypesMathFuncsDLL の例が書かれている C++ では動作しません。

代わりに、C で書くか、少なくとも「C」インターフェースをエクスポートします。

#ifdef __cplusplus
extern "C" {
#endif

__declspec(dllexport) double Add(double a, double b)
{
    return a + b;
}

#ifdef __cplusplus
}
#endif

また、呼び出し規約のデフォルトは__cdeclであるため、 (呼び出し規約を使用する)CDLLの代わりに使用することに注意してください。WinDLL__stdcall

>>> import ctypes
>>> dll=ctypes.CDLL('server')
>>> dll.Add.restype = ctypes.c_double
>>> dll.Add.argtypes = [ctypes.c_double,ctypes.c_double]
>>> dll.Add(1.5,2.7)
4.2
于 2012-04-15T16:42:47.340 に答える