ImportError: cannot import name WINFUNCTYPE

Hi all,
I am new to Ignition Perspective and I am using a 8.1.1 version. I am trying to extract information from a windows file and convert it into the ASCII but when I am running the code I am repeatedly having this error. Please give your inputs for below code:

from ctypes import POINTER, WINFUNCTYPE, c_char_p, c_void_p, c_int, c_ulong, c_char_p
from ctypes.wintypes import BOOL, DWORD, BYTE, INT, LPCWSTR, UINT, LONG, ULONG, LPCSTR, PCHAR, PWCHAR
import ctypes
from ctypes import byref
import sys
import os, glob

tcAccessDll = r"C:\Users\himanshu.choubisa\Desktop\TcAccess.dll"
rstfilename = b"C:\Users\himanshu.choubisa\Desktop\RA03534 AGAS UK 70+4min 130521B.rst"

#check newest results file :slight_smile:
#rstfilename = min(glob.iglob(‘C:\TCData\Data\*.rst’), key=os.path.getctime).encode(‘ascii’)

Load DLL into memory.

#hllDll = ctypes.WinDLL (r"c:\PenExe\TcWS\Ver6.3.4\Bin\TcAccess.dll")#

#hllDll = ctypes.cdll.LoadLibrary (r"c:\PenExe\TcWS\Ver6.3.4\Bin\TcAccess.dll")

#hllDll = ctypes.WinDLL (r"c:/PenExe/TcWS/Ver6.3.4/Bin/TcAccess.dll")

#hllDll = ctypes.WinDLL (“TcAccess.dll”)

def get_winfunc(libname, funcname, restype=None, argtypes=(), _libcache={}):
“”“Retrieve a function from a library, and set the data types.”""
from ctypes import windll

if libname not in _libcache:
    _libcache[libname] = ctypes.cdll.LoadLibrary(libname)
func = getattr(_libcache[libname], funcname)
func.argtypes = argtypes
func.restype = restype

return func

class TCAccess(object):
Init = get_winfunc(tcAccessDll, “TcAccessInit”)
LoggedIn = get_winfunc(tcAccessDll,“TcAccessLoggedOn”, BOOL)
OpenConversation = get_winfunc(tcAccessDll, “TcAccessOpenConversation”,LONG,[LPCSTR])
CloseConversation = get_winfunc(tcAccessDll, “TcAccessCloseConversation”, INT, [INT])
Set = get_winfunc(tcAccessDll, “TcAccessSet”, INT, [INT,LPCSTR,LPCSTR])
Get = get_winfunc(tcAccessDll, “TcAccessGet”, LPCSTR, [INT, LPCSTR])

print(TCAccess.Init())
print(TCAccess.LoggedIn())
topic = ‘RST’
c=(TCAccess.OpenConversation(b"RST"))
print(c)
print(TCAccess.Set(c,b"RST_FILE_NAME",rstfilename))
peakcount = int((TCAccess.Get(c,b"Rst_Num_Peaks")))
for index in range(peakcount):
(TCAccess.Set(c,b"RST_COMP_INDEX",str(index).encode(‘ascii’)))
CompName=(TCAccess.Get(c,b"CP_COMP_NAME").decode())
(TCAccess.Get(c,b"CP_PEAK_VECTOR"))
(TCAccess.Get(c,b"RST_PEAK_INDEX"))
CompArea=(TCAccess.Get(c,b"PK_AREA_PCT").decode())
CompResult=(TCAccess.Get(c,b"PK_RESULT").decode())
print ("Component " + CompName + " Peak Area %: " + CompArea + " Peak Concentration: " + CompResult)

Jython’s ctypes implementation is incomplete.

Generally, you should not expect to be able to load DLLs or C libraries with Jython nor do Python libraries that wrap C libraries work inside Ignition.

1 Like

Thanks Kevin for your inputs
however, I cant work out how to have the correct implementation of ctypes.
Will you please elaborate with the correct code?
Thanks in advance

There is no correct code. You cannot access .DLL files from jython or java. You would need to write another C layer with JNA to access the DLL for you, and wrap it in a java class. Then import that from jython.

It would probably be simpler to write a webservice in CPython to use the existing code, and make requests from Ignition to the webservice.

1 Like