Guest User

1C DLL through Python

a guest
Jun 30th, 2017
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. # -*-coding:utf8-*-
  2.  
  3. import win32com.client
  4. import ctypes
  5. import os
  6. class GtAlgoWrapper():
  7.     # com spec
  8.     _public_methods_ = ['solve','resultCallback', 'progressCallback',]
  9.     _public_attrs_ = ['version',]
  10.     _readonly_attr_ = []
  11.  
  12.     _reg_clsid_ = '{2234314F-F3F1-2341-5BA9-5FD1E58F1526}'
  13.     _reg_progid_= 'GtAlgoWrapper'
  14.     _reg_desc_  = 'COM Wrapper For GTAlgo'
  15.     _dependencies = ['libwinpthread-1.dll',
  16.                      'libgcc_s_dw2-1.dll',
  17.                      'libstdc++-6.dll',
  18.                      'libcrypto-1_1.dll',
  19.                      'libssl-1_1.dll',
  20.                      'Qt5Core.dll',
  21.                      'Qt5Cored.dll',
  22.                      'Qt5Gui.dll',
  23.                      'Qt5Guid.dll',
  24.                      'libcurl.dll',
  25.                      'GtTools0-0-1.dll',
  26.                      'GtRouting0-0-1.dll']
  27.     def __init__(self):
  28.         self.version = '0.0.1'
  29.         self.progressOuterCb = None
  30.         self.resultOuterCb = None
  31.  
  32.     def solve(self, data):
  33.         prefix_path = 'C:/release'
  34.         try:
  35.             for dep in self._dependencies:
  36.                 ctypes.cdll.LoadLibrary(os.path.join(prefix_path, dep))
  37.             lib = ctypes.cdll.LoadLibrary(os.path.join(prefix_path, 'GtAlgo0-0-1.dll'))
  38.         except WindowsError:
  39.             raise Exception('cant load' + dep)
  40.  
  41.         solve_func = getattr(lib, '_ZN6GtAlgo5solveEPKcPFviS1_ES3_')
  42.         StatusCallback = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p)
  43.         ResultCallback = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p)
  44.         _scb_func = StatusCallback(self._libProgressCb)
  45.         _rcb_func = ResultCallback(self._libResultCb)
  46.         if self.resultOuterCb is None:
  47.             raise Exception('resultCallback function is not Set')
  48.         if self.progressOuterCb is None:
  49.             raise Exception('progressCallback function is not Set')
  50.  
  51.         solve_func(ctypes.c_char_p(data), _scb_func, _rcb_func)
  52.  
  53.         return 'DONE'
  54.  
  55.     def _libProgressCb(self, *args):
  56.         self.progressOuterCb(*args)
  57.  
  58.     def _libResultCb(self, *args):
  59.         self.resultOuterCb(*args)
  60.  
  61.     def _callbackSetter(self, obj, outerCbName):
  62.         if str(type(obj)) == "<type 'PyIDispatch'>":
  63.             com_obj = win32com.client.Dispatch(obj)
  64.             try:
  65.                 return getattr(com_obj, outerCbName)
  66.             except AttributeError:
  67.                 raise Exception('"'+outerCbName + '" not found in "This"')
  68.         else:
  69.             raise Exception('"This" is necessary parameter')
  70.  
  71.     def resultCallback(self, obj):
  72.         self.resultOuterCb = self._callbackSetter(obj, 'resultCallback')
  73.         return obj
  74.  
  75.     def progressCallback(self, obj):
  76.         self.progressOuterCb = self._callbackSetter(obj, 'progressCallback')
  77.         return obj
  78.  
  79. def main():
  80.     import win32com.server.register
  81.     win32com.server.register.UseCommandLine(GtAlgoWrapper)
  82.     print('registred')
  83.  
  84. if __name__ == '__main__':
  85.     main()
Add Comment
Please, Sign In to add comment