Trying to access functions in Virtual Print Engine dll using python 3 ctypes in windows

1

I'm fairly new to Python programming and am seeking assistance to use Python 3 to interface into Ideal Software's Virtual Print Engine DLL on Windows 7, 8 and 10.

I've searched everywhere for a solution and contacted the DLL's author without any luck. He doesn't use Python and his Python example doesn't work.

Goal

I want to convert to Python 3 Ideal Software's simple C example from their Programmer's Manual as follows...

void MakeDoc(HWND hWndParent)
{
// hWndParent is the window handle of your application window
VpeHandle hDoc = VpeOpenDoc(hWndParent, "Test", 0);
VpeWriteBox(hDoc, 1, 1, 5, 1.5, "Hello World!");
VpeLine(hDoc, 1.5, 3, 5, 6.5);
VpeWriteDoc(hDoc, "hello world.pdf");
VpePreviewDoc(hDoc, NULL, VPE_SHOW_NORMAL);
}

You may open as many documents simultaneously as you like. The documents are identified by a unique handle (VpeHandle, which is a 32-bit integer on 32-bit platforms and a 64-bit integer on 64-bit platforms) with a value different from NULL, which is returned by VpeOpenDoc(). Use this handle in successive calls to the output functions.

Each document might send messages. The messages are received by the window, which is specified as parent-window in the first parameter of VpeOpenDoc().

From the DLL reference guide the VpeOpenDoc function is described as...

3.2 VpeOpenDoc

Creates a new document with one initial blank page.

VpeHandle VpeOpenDoc(HWND hwndParent, LPCSTR title, long flags)

HWND hwndParent----a window of your calling application that will be the parent window of the VPE Preview Window. VPE exchanges messages with it. If the Preview is embedded, this will also be the host window of the Preview. This parameter may be NULL, for example to use VPE in windowless applications like server processes or batch jobs. In this case you can install a message callback function, in order to receive events generated by VPE. For details, see VpeSetMsgCallback(). For non-Windows platforms this parameter must be NULL.

LPCSTR title ----title of the Preview Window. The title is also used by VPE to compose the default JobName of the print job.

long flags----controls the style of the Preview and the behavior of VPE (see below)

Returns: The handle (=identifier) to the virtual document. This handle has to be provided to all other VPE calls. In case of an error, NULL (0) is returned. On 32-bit platforms the handle is a 32-bit integer, on 64-bit platforms it is a 64-bit integer.

Problems

I can load the DLL, but I cannot work out how to get...

  1. the parent calling handle/id their example refers to in the first line

    e.g. void MakeDoc(HWND hWndParent).
    

    Not a big issue at the moment as I'm calling my Python script from another language in a batch mode. But I want to have a Python wrapper around it later.

  2. the 32 bit returned handle from the DLL function, VpeOpenDoc in the example code,

     VpeHandle hDoc = VpeOpenDoc(hWndParent, "Test", 0) 
    

    to use to access the other functions in the DLL.

My Python code 1

from ctypes import*

VPEdll = windll.LoadLibrary("M:\\python\\vpec3271.dll")
print("VPEdll=", VPEdll)
VPEdll_handle = VPEdll._handle
print("VPEdll_handle=", VPEdll_handle)
VPEdoc = VPEdll.VpeOpenDoc(None,"VPE_Test", 0)
print("VPEdoc=", VPEdoc)
VPEdll.VpePrint(byref(VPEdoc), 5, 1, "Hello World!")
VPEdll.VpeWriteDoc(byref(VPEdoc), "M:\\python\\My Document.pdf")
VPEdll.VpeCloseDoc(byref(VPEdoc))

Python error 1

 m:\Python\Python35-32>python vpe_test.py
 VPEdll= <WinDLL 'M:\python\vpec3271.dll', handle 62570000 at 0x23bff0>
 VPEdll_handle= 1649868800
 VPEdoc= 45918424
 Traceback (most recent call last):
   File "vpe_test.py", line 9, in <module>
     VPEdll.VpePrint(byref(VPEdoc), 5, 1, "Hello World!")
 TypeError: byref() argument must be a ctypes instance, not 'int'

So I get the next error after I change my line 7 to c_int

VPEdoc = c_int(VPEdll.VpeOpenDoc(None,"VPE_Test", 0))

Python error 2

m:\Python\Python35-32>python vpe_test.py
VPEdll= <WinDLL 'M:\python\vpec3271.dll', handle 62570000 at 0x235b30>
VPEdll_handle= 1649868800
VPEdoc= c_long(44345560)
Traceback (most recent call last):
  File "vpe_test.py", line 9, in <module>
    VPEdll.VpePrint(byref(VPEdoc), 5, 1, "Hello World!")
OSError: exception: access violation reading 0x00000010

I'd appreciate any assistance.

python
python-3.x
dll
asked on Stack Overflow May 21, 2018 by Chris_Oz

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0