June 16th, 2004, 12:05 PM
Puthon to VB interface
I need some advice (sample) on how to use Python engine from inside VB (visual basic) by directly linking to Py .dll functions. I'm a newbie to Python and it would be a bit longer way for me built COM in Python at this time. I do need to use the numerical packages that Python has.
I've managed to initialize and close Python from inside VB. That is I know I how to declare the functions from python23.dll for VB to be able to call them. However, I was wondering if there is one out-there who would know how to manage passing of the data.
June 16th, 2004, 01:53 PM
I am also trying to do same kind stuff. What i need is to provide facility to end user to write own script in Python and run that script during runtime in VB. I am trying to build COM in python. Let us hope it will work.
But for now, As you mationed that you know how to declare function in VB to use python23.dll functionality, can u please post example here? I do not have clue for that. It might helpful for me. I am not sure. But it would be good help for me to work further.
Thanks in advance. I hope you will understand and post it here or attach as zip file or provide any reference for it.
June 16th, 2004, 05:30 PM
I would have thought that calling the Python DLL from VB would be a lot more time consuming that using COM. Calling the DLL would mean you have to handle all the low level reference counting, which can get messy, or at least tedious.
You can wrap Python classes using Windows Script Components (WSC). This is a recent M$ addition to COM that lets you define an interface to Python (or any other Active Scripting language) in a XML, and the system does the rest.
The M$ docs are at http://msdn.microsoft.com/library/de...l/lettitle.asp
There are other tutorials on the web - google for them if the M$ docs are not enough (or are too much!).
There is also a wizard that will create the XML files for you. Once you have the hang of it you can create Python COM components in minutes.
WSC is part of Windows Scripting Host 5.6 (I think - it may have been included in earlier versions), so if you have Windows XP or have installed IE6 then you should have it already.
Dave - The Developers' Coach
June 16th, 2004, 06:17 PM
Here is a simple example of Windows Scripting Host in action. It exposes the Python eval function via COM.
Save the following in a file called pyeval.wsc
right click on the file and select 'Register'. This will register the file as a COM component. If the option is not available, then you do not have Windows Scripting 5.6 installed.
<?component error="true" debug="true"?>
Now in your VB code you should be able to do the following (tested in Excel VBA, since I do not have VB installed):
This is obviously a very simple example, but it should give you an idea of how it works. There may be problems passing arbitrary objects between Python and VB, but that will be true whatever method you use.
Public Sub TestEval()
Set pe = CreateObject("pyeval.WSC")
result = pe.pyeval("sum(xrange(100))")
Dave - The Developers' Coach
June 17th, 2004, 11:07 PM
Python to VB
Thank you, guys for the replies.
DevCoach, I have not tried you pyeval solution, but it looks like a good and easy solution for me.
As for the code that used to test that there is a communication with Python's engine via VB (this is for DevHims) I simply declared function Py_Initialize, Py_Finalize and Py_GetVersion as part of Lib "python23" (standard declaration of dll functions in VB... sorry for being a bit lazy with placing the actual code...am in a rush).
Py_GetVersion function is the one that should return a sort of string. Sometimes VB will get the pointer from the DLL and try "putting" into a String container. Most of the time it conflicts with the memory and therefore it crashes. However, sometimes it does return a string with the version of the installed Python. If I declare Py_GetVersion as returnin a Long type varaible then I get the address of the pointer to the string that I would like to get and that is where VB needs interface to this DLL, which I presume was written in C.
Hope this explains things a bit (from my perspective).
I will let you know if DevCoach suggestion works.
June 18th, 2004, 09:53 AM
Python to VB
I have not used Windows scripting at all. I do intend to learn using it. For now, though, I wondere if you could help me out with registering the pyeval component. I do have Windows scripting on my PC. When I try registering the pyeval.wsc you sent me it gives me an error: "Unknown error: python" and "DLLInstall in C:\WINNT\system32\scrobj.dll failed. Return code was: 0x80020101."
Does this mean that Python is configured properly on my PC or that maybe CLSID that you provided needs to be regenerated. If it is the latter then what tool did you use to generate it?
P.S. I noticed you reply to DevHim and it seems that that would work for me as well for now. I simply need Python to perform some calculations (optimizationroutines) and send back to VB the result.
June 18th, 2004, 11:30 AM
My problem is quite different. Let me explain in brief.
I have application developed in VB. I want to provide user of my application facility to wirte code in python and use it in my application to set/get certain properties for example(i have class Screen, then user can get current screen object and access it's properties) of my application, and also able to channge state of application through script.
So, my application should provide interface to user, where he can use it's own script to perform certain actions during run time.
Using COM developed python, you can send data back and forth inside the application. but you can not provide facility to user to write cod in python and see the result of that when it runs script within my application.
Did you getme? I developed COM server in python and used in VB. For my specific requirement, it did not work.
I do not know what i will do. But if you find anything please let me know.
Thanks very much.
June 18th, 2004, 11:36 AM
Do you have the Python Win32All extensions installed? It sounds like Python has not been registered for COM, which should happen automatically when you install the extensions.
If you do not have it then it can be downloaded from http://sourceforge.net/projects/pywin32/. If you already have it then try re-installing it.
DAve - The Developers' Coach
June 21st, 2004, 11:23 AM
I've played around with win32 package installation and re-installation. It did not help. Since I have started learning Python recently this bothers me a bit. There are so many builts for Windows out-there and I can't quite tell which one is complete, or which pieces are missing. Initially I used ActivePython built to install Python. Then I added SciPy, Numerics, NumArray and VB2Py. From ActivePython docs I concluded that it installs all win32 extensions.
Just in case, I tried to install (rather add) win32 extensions from the dowload site you suggested. During the installation it gave me a warning that AXScript Engine COM failed to install because of missing _sre module.
I can go ahead and reinstall the entire Python suite, but I would appreciate if you gave me some advice on what is a working (tested) installation that would also register Python as a COM. By the way, how do I check that Python is registered? When I look into Windows registry it is shown there.
June 21st, 2004, 11:36 AM
Py to VB
I'm a real newbie to Py, but I feel that you want to implement something like a script editor inside your VB application. And I think that this will require for you to write a small shell that lets user enter his/her script (text taht is) and then have the entered text parsed and sent to Python engine. The connection to the engine turned out to be really simple via the interpreter module (interp.py registers Py engine for you).
Usually, scripting tools also support debuggers, and that is where I suspect you will have problems when letting VB somehow connect to Py's debugger. I would think that you would need to add some stuff to interp.py that will give access to Py debugger.
Not much help but mey be this will spark some ideas.