- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 ctypes 从 python 初始化 Matlab 编译器运行时 (MCR)。我的最终目标是能够在 python 中使用由 Matlab 编译器创建的 C DLL,但我需要克服的第一个障碍是启动并运行 MCR。
我使用的是 Python 2.7.8、Matlab 2013a、MCR8.1。
来自 mclbase.h 的 Snipit 以显示参数等
LIBMWMCLBASE_API_EXTERN_C bool mclInitializeApplication(const char** options, size_t count);
mclInitializeApplication(NULL,0)
# Initialize the MATLAB Compiler Runtime global state
from ctypes import *
libmcr=cdll.mclmcrrt8_1
# Load mclbase library
mcr_dll = cdll.LoadLibrary('C:\\Program Files\\MATLAB\\MATLAB Compiler Runtime\\v81\\bin\\win64\\mclbase.dll')
# Pick function we want to use
mclInit=mcr_dll.mclInitializeApplication
# Set up argument and results types
mclInit.argtypes = [POINTER(POINTER(c_char)),c_size_t]
# mclInit.argtypes = [POINTER(c_char_p),c_size_t] #different formatting attempt
mclInit.restype = c_bool
a=None
acast=cast(a,POINTER(c_char_p))
acast1=cast(a,POINTER(c_char))
acast2=cast(a,POINTER(POINTER(c_char)))
print 'a='
print a
print 'acast='
print acast
print 'acast1='
print acast1
print ''
# Try calling the function with various argument types
try:
b=mclInit(None,0)
except Exception as ex:
print ex
raw_input("Exception occurred. b=mclInit(None,0) didn't work. Press Enter to continue")
print ''
try:
b=mclInit(byref(acast),0)
except Exception as ex:
print ex
raw_input("b=mclInit(byref(acast),0) didn't work. Press Enter to continue")
print ''
try:
b=mclInit(acast,0)
except Exception as ex:
print ex
raw_input("b=mclInit(acast,0) didn't work. Press Enter to continue")
print ''
try:
b=mclInit(byref(acast1),0)
except Exception as ex:
print ex
raw_input("mclInit(byref(acast1) didn't work. Press Enter to continue")
print ''
try:
b=mclInit(acast1,0)
except Exception as ex:
print ex
raw_input("b=mclInit(acast1,0) didn't work. Press Enter to continue")
print ''
try:
b=mclInit(byref(acast2),0)
except Exception as ex:
print ex
raw_input("mclInit(byref(acast2) didn't work. Press Enter to continue")
print ''
try:
b=mclInit(acast2,0)
except Exception as ex:
print ex
raw_input("b=mclInit(acast2,0) didn't work. Press Enter to continue")
print ''
raw_input("Reached the end!!!! Press enter to close")
a=
None
acast=
<__main__.LP_c_char_p object at 0x00000000034E68C8>
acast1=
<ctypes.LP_c_char object at 0x00000000034E6948>
[Error -529697949] Windows Error 0xE06D7363
Exception occurred. b=mclInit(None,0) didn't work. Press Enter to continue
argument 1: <type 'exceptions.TypeError'>: expected LP_LP_c_char instance instea
d of pointer to LP_c_char_p
b=mclInit(byref(acast),0) didn't work. Press Enter to continue
argument 1: <type 'exceptions.TypeError'>: expected LP_LP_c_char instance instea
d of LP_c_char_p
b=mclInit(acast,0) didn't work. Press Enter to continue
[Error -529697949] Windows Error 0xE06D7363
mclInit(byref(acast1) didn't work. Press Enter to continue
[Error -529697949] Windows Error 0xE06D7363
b=mclInit(acast1,0) didn't work. Press Enter to continue
argument 1: <type 'exceptions.TypeError'>: expected LP_LP_c_char instance instea
d of pointer to LP_LP_c_char
mclInit(byref(acast2) didn't work. Press Enter to continue
[Error -529697949] Windows Error 0xE06D7363
b=mclInit(acast2,0) didn't work. Press Enter to continue
mclmcrInitialize()
的电话。正如@eryksun 指出的那样。我现在可以调用函数(万岁!)但初始化不成功:(。所以有些进展,但仍有工作要做!这是代码,以防它对任何人有用。我多次调用
mclIsMCRInitialized()
和
mclGetLastErrorMessage()
在那里增加了一些东西,但可能会提供有用的调试信息。
from ctypes import *
libmcr=cdll.mclmcrrt8_1
# Load mclmcr library
mclmcr_dll = cdll.LoadLibrary('C:\\Program Files\\MATLAB\\MATLAB Compiler Runtime\\v81\\bin\\win64\\mclmcr.dll')
# Load mclbase library
mclbase_dll = cdll.LoadLibrary('C:\\Program Files\\MATLAB\\MATLAB Compiler Runtime\\v81\\bin\\win64\\mclbase.dll')
# Call mclmcrInitialize()
mclmcrInit = mclmcr_dll.mclmcrInitialize
mclmcrInit.argtypes = None
mclmcrInit.restypes = c_bool
a = mclmcrInit()
print "mclmcrInitialize returned "
print a
# call mclIsMCRInitialized()
mclIsMCRInit = mclbase_dll.mclIsMCRInitialized
mclIsMCRInit.argtypes = None
mclIsMCRInit.restype = c_bool
b = mclIsMCRInit()
print "mclIsMCRInitialized = "
print b
# Call mclGetLastErrorMessage()
mclGetLastErrMsg = mclbase_dll.mclGetLastErrorMessage
mclGetLastErrMsg.argtypes = None
mclGetLastErrMsg.restypes = c_char_p
err = mclGetLastErrMsg()
print "mcl last error returns "
print err
# call mclInitializeApplication(NULL,0)
mclInit = mclbase_dll.mclInitializeApplication
mclInit.argtypes = [POINTER(c_char_p),c_size_t]
mclInit.restype = c_bool
b = mclInit(None,0)
print "mclInitializeApplication returned "
print b
# call mclIsMCRInitialized()
mclIsMCRInit = mclbase_dll.mclIsMCRInitialized
mclIsMCRInit.argtypes = None
mclIsMCRInit.restype = c_bool
b = mclIsMCRInit()
print "mclIsMCRInitialized = "
print b
# Call mclGetLastErrorMessage()
mclGetLastErrMsg = mclbase_dll.mclGetLastErrorMessage
mclGetLastErrMsg.argtypes = None
mclGetLastErrMsg.restypes = c_char_p
err = mclGetLastErrMsg()
print "mcl last error returns "
print err
# Call mclTerminateApplication()
mclTerminate = mclbase_dll.mclTerminateApplication
mclTerminate.argtypes = None
mclTerminate.restype = c_bool
f = mclTerminate()
print "mclTerminateApp returned "
print f
mclmcrInitialize returned
-2147483647
mclIsMCRInitialized =
False
mcl last error returns
124384774
mclInitializeApplication returned
False
mclIsMCRInitialized =
False
mcl last error returns
128050512
mclTerminateApp returned
True
最佳答案
我是应@Krcevina 的请求发布此信息的。不幸的是,由于 PC 上的空间限制,我不得不卸载 MCR,因此今天无法对此进行测试,但我认为此代码应该具有合理的功能和稳定性。我试图在评论中发布它,但它似乎不允许大代码片段,所以我创建了这个答案。它只是初始化和终止 MCR 和我自己的 DLL。显然,您需要更改路径名称等以匹配您自己的情况。我还没有从 DLL 中调用函数或将变量传递给所述函数,但希望这对您有用。
这就是我对这个问题的了解。 Matlab R2014b 上的新 python 引擎意味着我不需要再进一步了。
from ctypes import *
libmcr=cdll.mclmcrrt8_1
# Just check to make sure its 64bit python I'm using...
print "4 for 32bit, 8 for 64bit:"
print sizeof(c_voidp)
# Load mclmcr library
mclmcr_dll = cdll.LoadLibrary('C:\\Program Files\\MATLAB\\MATLAB Compiler Runtime\\v81\\bin\\win64\\mclmcr.dll')
# Load mclbase library
mclbase_dll = cdll.LoadLibrary('C:\\Program Files\\MATLAB\\MATLAB Compiler Runtime\\v81\\bin\\win64\\mclbase.dll')
# Load matrix library
matrix_dll = cdll.LoadLibrary('C:\\Program Files\\MATLAB\\MATLAB Compiler Runtime\\v81\\bin\\win64\\libmx.dll')
# Load ClairesTestInterface library
claires_dll = cdll.LoadLibrary('ClairesTestCompile.dll')
#typedef int (*mclMainFcnType)(int, const char **);
mclMainFcnType=CFUNCTYPE(c_int, c_int, POINTER(c_char_p) ) # (returnvalu, arg1, arg2)
def run_main(argc,argv):
print "run_main entered."
# call mclInitializeApplication(NULL,0)
mclInitializeApplication = mclbase_dll.mclInitializeApplication
mclInitializeApplication.argtypes = [POINTER(c_char_p),c_size_t]
mclInitializeApplication.restype = c_bool
b = mclInitializeApplication(None,0)
print "mclInitializeApplication returned "
print b
# call mclIsMCRInitialized()
mclIsMCRInitialized = mclbase_dll.mclIsMCRInitialized
mclIsMCRInitialized.argtypes = None
mclIsMCRInitialized.restype = c_bool
b = mclIsMCRInitialized()
print "mclIsMCRInitialized = "
print b
# call ClairesTestCompileInitialize()
ClairesTestCompileInitialize = claires_dll.ClairesTestCompileInitialize
ClairesTestCompileInitialize.argtypes = None
ClairesTestCompileInitialize.restype = c_bool
b = ClairesTestCompileInitialize()
print "ClairesTestCompileInitialize = "
print b
## -------------------------------------------------------------------------------------------------------
## -------------------------------------------------------------------------------------------------------
# call ClairesTestCompileTerminate()
ClairesTestCompileTerminate = claires_dll.ClairesTestCompileTerminate
ClairesTestCompileTerminate.argtypes = None
ClairesTestCompileTerminate.restype = None
ClairesTestCompileTerminate()
print "ClairesTestCompileTerminate has run "
# Call mclTerminateApplication()
mclTerminateApplication = mclbase_dll.mclTerminateApplication
mclTerminateApplication.argtypes = None
mclTerminateApplication.restype = c_bool
f = mclTerminateApplication()
print "mclTerminateApp returned "
print f
print "Reached the end of run_main()!!!"
return b
def main():
print "main() entered."
# Call mclmcrInitialize()
mclmcrInitialize = mclmcr_dll.mclmcrInitialize
mclmcrInitialize.argtypes = None
mclmcrInitialize.restypes = c_bool
a = mclmcrInitialize()
print "mclmcrInitialize returned "
print a
#LIBMWMCLBASE_API_EXTERN_C int mclRunMain(mclMainFcnType run_main, int argc, const char **argv);
mclRunMain=mclbase_dll.mclRunMain
mclRunMain.argtypes = [mclMainFcnType, c_int, POINTER(c_char_p)]
mclRunMain.restype = c_int
try:
_run_main = mclMainFcnType(run_main) # reference the callback to keep it alive
except Exception as ex:
print ex
#print "callback referenced to keep it alive."
c = mclRunMain(_run_main, 0, None)
print "mclRunMain returned "
print c
raw_input("Reached the end of main()! Press enter to close")
main()
关于python-2.7 - Python ctypes : How to pass NULL as argument with format const char **,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26995899/
我似乎找不到任何将 ctypes.c_void_p() 转换为字符串或字节数组的简单示例。有没有简单的衬里可以做到这一点? 最佳答案 给你: import ctypes as ct # set up
在ctypes中,pointer和byref有什么区别?它们似乎都是将指针传递给函数的一种方式,例如作为输出参数。 最佳答案 在功能上,它们是等价的。 然而,python docs请指出 pointe
我知道我应该指定 argtypes对于我的 C/C++ 函数,因为我的某些调用会导致堆栈损坏。 myCfunc.argtypes = [ct.c_void_p, ct.POINTER(ct.c
有没有办法获取指向 ctypes 数组中间元素的指针?示例: lib = ctypes.cdll.LoadLibrary('./lib.so') arr = (ctypes.c_int32 * 100
在我自定义的 TYPO3 Extbase 扩展中,我创建了一个后端模块来管理个人记录。现在我需要一个内容元素来在前端显示记录。 我看到了两种实现此目的的方法: 使用 CType“list”和自定义 l
实际上,我正在尝试将 ctypes 数组转换为 python 列表并返回。 如果找到this thread 。但它假设我们在编译时知道类型。 但是是否可以检索元素的 ctypes 类型? 我有一个 p
我正在将 float 列表转换为具有以下字段的 ctypes Structure 类,然后再将它们传递给 FFI 函数: FFIArray(Structure): _fields_ = [("
我需要将异质数据的二维数组从我的 c dll 返回到 python。 为此目的,我从我的 c dll 返回一个元组的元组。它作为 PyObject 返回 * 这个元组的元组需要作为第一行第一列的 tu
这是不一致的: from ctypes import * class S(Structure): _fields_ = [("x", POINTER(c_int)), ("y", c_int)
我真的希望一些 Python/Ctypes/C 专家可以帮助我解决这个问题,这可能是我在使用 Python 与 C 库交互时正确使用 Ctypes 的类型结构方面缺乏知识。 目标:我需要访问几个使用
我正在尝试调试 python 使用 ctypes 调用 C 函数的代码。我感兴趣的 python 代码中的一行看起来像: returnValue = cfunction() 其中 cfunction
我正在开发 DLL/SO 的 Python 包装器。我已经验证了代码可以调用实际的 DLL 和 SO。我想对我的包装器进行单元测试,而不需要安装底层 DLL/SO。我正在考虑使用 mock 。 我遇到
大家。我在使用 ctypes 和 C 代码时遇到内存分配错误。我想知道内存问题是在 C 内部,还是由 ctypes 使用不当引起的。内存错误是 python(79698) malloc: * erro
我想制作一个笑话程序,首先它打开一个消息框,关闭后另一个消息框出现在随机位置。它会一直这样重复,直到有什么东西终止了它的任务。使用 tkinter 消息框,那么这些消息框就无法被 Hook ,我必须制
我对 python 中的变量大小有疑问,我使用 Ctypes 因为我想要一个 1 字节的数字,但是当我试图在 python 中检查它的大小时(通过 sys.getsize ) 它说它是 80 字节但是
我正在尝试在 python lambda 函数中使用 matplotlib 生成图形。我使用库 mathplotlib 导入了一个图层,但它不起作用。 这个想法是生成一个图形,将其保存为临时文件并上传
我正在尝试使用 C 中的 python ctypes 制作简单的库 blake 哈希函数包装器。但只是为了首先测试我的简单 C 辅助函数是否能正常工作,我编写了小的 python 脚本 blake 哈
图书馆代码(简化版): // package1.go package package1 import "C" func Play(s *C.char) { } 客户代码: // main.go pac
到目前为止,我已经得到了一个不适用于 python 的 DLL,并输入 return: I just can't pass it arguments because I doing it wrong
我有一个具有以下签名的 C 函数: void init(int* argc, char** argv[]); 我想使用 Ctypes 从我的 OCaml 代码中调用此函数,但我想不出一个正确的方法来传
我是一名优秀的程序员,十分优秀!