Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

Python embedding w/Debugger

$
0
0
I have to work across multiple languages more and more. If i need to use a python library for the most part I just write a small python script and then shell it on the command line and read its output. Its simple and works well.

I have been curious for a while about what it would take to load the python dll into my vb process and interact with scripts as an embedded script interpreter. no need for external scripts on disk, more control over the scripts, ability to interactively call functions and get function returns.

This is more of an experiment vrs a complete thing. The cdecl python API is large. Right now I am using a small C dll to proxy calls between the cdecl python API and the stdcall VB declares. This also gives me space to translate the vb6 data types to C data types and a place to debug it in a smaller intermediary layer.

Current features in this sample:
- set script command line arguments
- manually import modules
- manually set global script values
- hook the python output to stdout (print) and stdErr
- run a python script in memory from its text string
- call a python function with an argument
- receive the function return value
- read a global variable from the script
- hook exit, sys.exit, and quit so they dont close the process.
- arbitrarily interact with vb COM objects (pywin32lib)
- threading example
- python debugger example

Once a script is loaded, you can keep adding code to execute within the same context, or call multiple functions within it etc.
This example only has one script context running at a time. Not all python data formats are integrated yet.

There are probably some memory leaks and it will be very easy to crash the process if doing things out of order.

You will need a 32bit version of python 3 installed on your system. I used VS2019 to compile the C dll. You will need to change your include and lib directories if you install python to a different path than mine ( c:\py311\ )

I can not offer much support on it, still learning as I go but kinda fun to experiment with anyway.

edit:
I have added a COM example to interact with VB objects, a threading example from Yokesee, and a basic debugger example
Attached Files

Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>