import ctypes
global Init
Init = ctypes.WinDLL ("ProfitDLL.dll")
def Initialize_ctypes():
activate = Init.InitializeMarket
activate.restype = ctypes.c_uint
Conn_lic = { 'pwcActivationKey': 'PWideChar',<br />
'StateCallback': 'TStateCallBack',<br />
'NewTradeCallback': 'TNewTradeCallback',<br />
'NewDailyCallback': 'TNewDailyCallback',<br />
'HistoryTradeCallBack': 'THistoryTradeCallBack',<br />
'ProgressCallBack': 'TProgressCallBack',<br />
'TinyBookCallBack': 'TTinyBookCallBack' }
I don't know if callback functions were written in better using ctypes because callback functions expected argument types as the remaining arguments.
Anyone can you write this code in python in the right form?
Your question is not very clear. For callback functions that you pass to a function loaded from a dll/ library, it must be passed as aa prope structure - either cfunctype or winfunctype ( latter in case the callbacks are also stdcall as the main function appears to be). See the documentation on callbacks for an example.
Morning, My question is how can I write this code in python? Delphi ( this is a part of function in DDL) function InitializeMarket ( const pwcActivationKey : PWideChar; StateCallback : TStateCallBack ; NewTradeCallback : TNewTradeCallback ; NewDailyCallback : TNewDailyCallback ; HistoryTradeCallBack : THistoryTradeCallBack; TinyBookCallBack : TTinyBookCallBack ) : Short ; stdcall;
Thanks in advance.
I have shared a stackoverflow link below on how you can achieve this. I have also added some tutorial links which can be useful to get you started on this. 1. Using Delphi DLL in Python via ctypes - Stack Overflow 2. Extending Python With C Libraries and the “ctypes” Module – dbader.org
Hi Ishan, Thanks for your help, certainly will be very helpful.
Can you explain what has been your tries so far. It is difficult to give anything other than a generic answer, I have no clue what dll your are after. The following may help (NOT RUN! of course):
from ctypes import *
import ctypes
load the dll -> windll for stdcall convention
mylib = ctypes.WinDLL('full/path/to/dll')
your_pwc_activation_key = "XXXXXXXX"
define the callback signature, the first value is return type
TStateCallBack = CFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
'''
Similarly define the types for other four callback
'''
TNewTradeCallback = CFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
TNewDailyCallback = CFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
THistoryTradeCallBack = CFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
TTinyBookCallBack = CFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
'''
Now define the callbacks you want matching the signature declared
'''
def StateCallback(a):
return 42
InitializeMarket = mylib.InitializeMarket
set the results type for auto convert and check
InitializeMarket.restype = c_short
'''
set the arg types for auto convert or check else you have to do StateCallback = TStateCallBack(StateCallback) to convert before the call. It works on built-in types. If it fails revert back to the explicit casting as above.
'''
InitializeMarket.argtype = [c_wchar_p, TStateCallBack, TNewTradeCallback, TNewDailyCallback, THistoryTradeCallBack, TTinyBookCallBack]
now call the function
result = InitializeMarket(your_pwc_activation_key, StateCallback, NewTradeCallback, NewDailyCallback, HistoryTradeCallBack, TinyBookCallBack)
Please check the documentation to double check and troubleshoot. It is highly readable.
Hi, Prodipta! I’m very satisfied that you are helping me. I wanna comunicate with my trading platfform and later apply the knowlege that I learnt at quantra, but it’s very difficult because my brokers are not acostumed do it. So my broker sent to me the trade plattform DLL, that’s the only way to comunicate with them. If you want and if you have time I can send DLL and the instrutions to you. Here my email and skype: julionce@gmail.com, juliocs84 Thanks so much for your advises.
I think it is best to reach out to your broker who owns the software. This will require going through their documentation, and on top sharing your credentials with a third party like me. This is not advisable
I think you can help me if you want I only wanna discover how to call the function with the arguments, the broker sent the the example of the code in C language.
I don’t have too much experience with python and I got stuck because whe I call the function with six arguments I got th error bellow: ValueError: Procedure probably called with not enough arguments (4 bytes missing). Do you have any idea?
This is the code that I tried? # A foreign function library for Python from ctypes import * import ctypes # Import DLL from Delphi code #Init = ctypes.CDLL (“ProfitDLL.dll”) #Init = ctypes.OleDLL (“ProfitDLL.dll”) #Init = ctypes.PyDLL (“ProfitDLL.dll”) Init = ctypes.WinDLL (“ProfitDLL.dll”) # Activation code pwc_activation_key = “1234567890123456789” # define the callback signature, the first value is return type #TStateCallBack = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int)) #TStateC