3

I am trying to interface with a dynamic simulation software (Vensim) with python. The software has a header file, vendll.h, that I'd like to be able to use.

I've been looking through the ctypes module for this but haven't found anything. I'd also like to avoid using cython but I'll see how this goes. Here's an example of the file provided with the software on how to utilize this file in matlab. You'll probably get the gist of what I want to be able to replicate in python more or less.

clear all
%*********************LOAD THE MODEL *************************

if not(libisloaded('VenDLL32'))
    hfile = ['C:\Users\Public\Vensim\dll\vendll.h'];
    loadlibrary('VenDLL32',hfile);
end

% Display Vensim DDL function calls and signatures
libfunctionsview VenDLL32

%% *********************EXECUTE SIMULATION *************************
% check calllib returns to verify initiation

str=['SPECIAL>LOADMODEL|RABFOX.vpm'];
calllib('VenDLL32','vensim_check_status')
calllib('VenDLL32','vensim_command',str) 
str=['MENU>RUN|o'];
calllib('VenDLL32','vensim_command',str)


%% *********************RETRIEVE DATA *************************
% retrieve data from the post simulation execution

tnum = 201;
timepts = int32(tnum);

mytime = zeros(1,tnum);
mytimePtr1 = libpointer('singlePtr', mytime);

myrabpop = zeros(1,tnum);
myrabpopPtr1 = libpointer('singlePtr', myrabpop);

% vensim get data command
[int32_status, cstring_vdf, cstring_var1, cstring_var2, singlePtr_var1, ... 
    singlePtr_var2] = calllib('VenDLL32','vensim_get_data','CURRENT.vdf', ...
    'Rabbit Population','time',myrabpopPtr1(1),mytimePtr1(1),timepts);

myrabpopPtr1(1).Value'
mytimePtr1(1).Value'

%% ******************** UNLOAD VENSIM DLL ********************
% unload the library 

unloadlibrary VenDLL32

Seems straightforward in matlab so should be a piece of cake in python right?

How can I load the header file and access its contents via python?

There is also an equivalent static link library (.lib) file (I don't see any .dll) named vendll32.lib. Is there a way to utilize this file in a similar way if I'm not able to make use of the header file?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
pbreach
  • 16,049
  • 27
  • 82
  • 120
  • 1
    You could start by reading through https://docs.python.org/3/c-api/ (or another version of the document matching your python interpreter version), and trying to use the functions therein to write a C++ shared object exposing the functions to python. That's the only way I've ever done such things / there may be others. Boost offers a library that may or may not make it easier for you (I've heard anecdotal reports of reliability issues before, but the latest boost library might have fixed whatever issues it used to have). I don't know any way to "import"/call the functions from the py. side. – Tony Delroy Oct 09 '15 at 05:42
  • I've looked through this briefly but am hoping there is a better way (without having to write any C++). Previous versions of the software provided DLLs for this type of stuff which were not bad to work with using ctypes. Am hoping for something similar. – pbreach Oct 09 '15 at 05:51
  • I think the easiest way is to just search the web for "how to import a DLL into Python", which should give you hundreds of hits. These are applicable because your case is probably not special enough. And yes, ctypes is one way, so why are you even asking if you knew about that? – Ulrich Eckhardt Oct 09 '15 at 05:52
  • I can load and work with DLLs using ctypes but in this circumstance only the .h and .lib files are present. At a bit of a sticking point and wanted to ask here before trying any particular method. – pbreach Oct 09 '15 at 05:57
  • Unless there's someone that actually wrote a tool that parses the header file and create python bindings from that (and there's none AFAIK), you're bound to write the python bindings yourself. – skyking Oct 09 '15 at 06:10
  • You can try to build a DLL out of the .lib. – n. m. could be an AI Oct 09 '15 at 06:18
  • That would be ideal. I'll have to look into it, no experience building a DLL. – pbreach Oct 09 '15 at 06:20

0 Answers0