Author Topic: Using dll with python  (Read 2089 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 9
Using dll with python
« on: October 28, 2021, 12:59:26 AM »
I've found the way to use PdfShellTools.dll with python, and here's the basic recipe. This doesn't format the text input to all the particular specifications PdhShellTools requires; it's just about getting it to the dll.

Code: [Select]
from ctypes import c_int, WINFUNCTYPE, WinDLL
from ctypes.wintypes import HWND, HINSTANCE, LPSTR

def setup():
    """Set dll up to run."""
    DLLNAME = 'C:/Program Files (x86)/PDF-ShellTools/PDFShellTools.dll'
    dll = WinDLL(DLLNAME)
    prototype = WINFUNCTYPE(None, HWND, HINSTANCE, LPSTR, c_int)
    paramflags = ((1, 'hwnd', 0), (1, 'hinst', 0), (1, 'IpszCmdLine', ""),
                  (1, 'nCmdShow', 0))
    return (dll, prototype, paramflags)

def make_function(pdfstname, dll, prototype, paramflags):
    """Make a function to run PdfShellTools action from python."""
    return prototype((pdfstname, dll), paramflags)

def run_dll_function(function, param, file):
    """Run function in dll. Format param & file nicely elsewhere."""
    dllinput = param + " " + file

dll, prototype, paramflags = setup()
set_metadata = make_function('SetMetadata', dll, prototype, paramflags)
pdfstparams = '"Metadata=Title=SomeTitle"'
pdfstfiles = 'afile.pdf'
run_dll_function(set_metadata, pdfstparams, pdfstfiles)

My guess is that you only want to do the setup part once to realize the full speed gain. It's probably good to minimize the calls to make_function as well. I haven't looked into performance concerns yet.


  • Newbie
  • *
  • Posts: 9
Re: Using dll with python
« Reply #1 on: October 28, 2021, 01:03:10 AM »
I should have mentioned that the dll only works with 32-bit Python installations.  :-[