如何停止Py_Initialise使应用程序崩溃?
从VBA和VB6我打电话给一个DLL创build一个Python解释器。 如果我将PATH
环境variables设置为"C:\python27"
, PYTHONPATH
为"c:\python27\lib"
一切正常。
如果我没有设置PATH
那么调用Py_Initialise()
崩溃XL或我的VB6应用程序,即使我先调用Py_SetProgramName
"c:\python27\python27.exe"
Py_SetProgramName
"c:\python27\python27.exe"
Py_SetProgramName
"c:\python27\python27.exe"
。
我想在VB / VBA中指定安装,而不是在环境中设置它,因为我不能在XL中使用它(适用于VB6)。
到目前为止我发现的最好的答案是它是Python中的一个错误 – http://bugs.python.org/issue6498 。 intepreter似乎调用exit()某些错误,而不是将代码传回给调用者。 如果你在应用程序中embeddedpython,不是非常友好。 但是,你去了。
尝试在调用dll之前更改工作目录:
在你的VBA代码中:
chdir("c:\python27\") '- change the working-directory for python => call dll '- call the dll chdir(app.Path) '- change back to your folder (maybe you want to save your current folder bevore you change it the first time and change back to this?!)
托马斯
您可以简单地检查是否设置了所需的环境variables:
dim PPath as string PPath = trim(Environ("PYTHONPATH")) if (PPath<>"") <call dll> else msgbox ("Error!") end if
或者你可以在另一个testing过程中运行dll:如果这个调用起作用,你知道可以调用dll – 这取决于DLL调用你的使用,所以我不确定:
Private Declare Function CloseHandle Lib "kernel32" (ByVal _ hObject As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal _ dwDesiredAccess As Long, ByVal bInheritHandle As _ Long, ByVal dwProcessId As Long) As Long Private Declare Function GetExitCodeProcess Lib "kernel32" _ (ByVal hProcess As Long, lpExitCode As Long) As Long Const STILL_ACTIVE = &H103 Const PROCESS_ALL_ACCESS = &H1F0FFF ... dim IsActive as boolean dim Handle as long Dim TaskID As Long TaskID = Shell("rundll32.exe pyton.dll Py_Initialise()", vbNormalNoFocus) <wait for some time> '- check if pyton.dll is still running Handle = OpenProcess(PROCESS_ALL_ACCESS, False, TaskID) Call GetExitCodeProcess(Handle, ExitCode) Call CloseHandle(Handle) IsActive = IIf(ExitCode = STILL_ACTIVE, True, False) if (not IsActive) then msgbox ("Error!") else <kill process> <call dll normally> end if
问候托马斯