Python ctypes:如何将NULL作为参数传递,格式为const char **

时间:2014-11-18 13:47:41

标签: python-2.7 pointers null ctypes

我试图使用ctypes从python初始化Matlab编译器运行时(MCR)。我的最终目标是能够在python中使用由Matlab编译器创建的C DLL,但我需要克服的第一个障碍就是启动并运行MCR。

我使用的是Python 2.7.8,Matlab 2013a,MCR8.1。

从mclbase.h剪切以显示参数等

LIBMWMCLBASE_API_EXTERN_C bool mclInitializeApplication(const char** options, size_t count);

相当于我试图做的事情

mclInitializeApplication(NULL,0)

这是我在python中调用函数的各种尝试。它们不可避免地导致TypeErrors或Windows错误0xE06D7363。我不能破解它!我是一个蟒蛇新手,所以可能有一些我想念的简单。欢迎任何评论!

# 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")

编辑:只需添加例外python throws

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

编辑2

事实证明我错过了对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

这是python的输出:

mclmcrInitialize returned
-2147483647
mclIsMCRInitialized =
False
mcl last error returns
124384774
mclInitializeApplication returned
False
mclIsMCRInitialized =
False
mcl last error returns
128050512
mclTerminateApp returned
True

2 个答案:

答案 0 :(得分:1)

我发布这个是为了回应@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()

答案 1 :(得分:0)

虽然我们猜测,你可以试试:

mclInit.argtypes = [POINTER(c_char_p), c_size_t]
a = POINTER(c_char_p)()
b = c_size_t(0)
ret = mclInit(byref(a), b)