Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delphi code to Python
#1
I have this code bellow that was written in delphi:

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;

How can I write in python? anyone can help me?
Reply
#2
That's just a function definition. It doesn't do anything. So the section of the python tutorial on defining functions should be everything you need to get going: https://docs.python.org/3/tutorial/contr...-functions
Reply
#3
Hey guys!

Anyone can help me, I'm trying to write this C code in python, but I don't know how to do.

I'm stuck in this code I loose a lot of hours.
/////////////////////////////////////////////////////////////////////////////
// WARNING: Não utilizar funções da dll dentro do CALLBACK
////////////////////////////////////////////////////////////////////////////////
//Callback do stado das diferentes conexões
typedef void (__stdcall *TStateCallback)(int nResult, int result);
////////////////////////////////////////////////////////////////////////////////
//Callback com informações marketData
typedef void (__stdcall *TNewTradeCallback)(TAssetID assetId, wchar_t* date, float price, float vol, int qtd, int buyAgent, int sellAgent, int tradeType);
typedef void (__stdcall *TNewDailyCallback)(TAssetID TAssetIDRec, wchar_t* date, float sOpen, float sHigh, float sLow, float sClose, float sVol, float sAjuste, float sMaxLimit, float sMinLimit, int sVolBuyer, int sVolSeller, int nQtd, int nNegocios, int nContratosOpen, int nQtdBuyer, int nQtdSeller, int nNegBuyer, int nNegSeller);
typedef void(__stdcall *TNewHistoryCallback)(TAssetID assetId, wchar_t* date, float price, float vol, int qtd, int buyAgent, int sellAgent, int tradeType);
typedef void(__stdcall *TProgressCallBack)(TAssetID assetId, int nProgress);
typedef void(__stdcall *TNewTinyBookCallBack)(TAssetID assetId, float price, int qtd, int side);
////////////////////////////////////////////////////////////////////////////////
// Functions From ProfitDLL
extern "C" {
	typedef signed char (__stdcall *InitializeMarket)(const wchar_t *activationKey,
                                                   TStateCallback stateCallback,
                                                   TNewTradeCallback newTradeCallback,
                                                   TNewDailyCallback newDailyCallback,
                                                   TNewHistoryCallback newHistoryCallback,
                                                   TProgressCallBack progressCallBack,
                                                   TNewTinyBookCallBack newTinyBookCallBack);
	typedef signed char (__stdcall *Finalize)();
	typedef signed char (__stdcall *SubscribeTicker)(const wchar_t *ticker, const wchar_t *bolsa);
	typedef signed char (__stdcall *UnsubscribeTicker)(const wchar_t *ticker, const wchar_t *bolsa);
	typedef wchar_t* (__stdcall *GetAgentNameByID)(int nID);
	typedef wchar_t* (__stdcall *GetAgentShortNameByID)(int nID);
	typedef signed char(__stdcall *GetHistoryTradesInInterval)(const wchar_t *pwcTicker, const wchar_t *bolsa,  wchar_t* dateStart, wchar_t* dateTimeStart);
}
// #Functions
// Error Codes
const signed char NL_OK = 0;     // OK
//const signed char NL_LOGIN_INVALID = [1..4];  // LOGIN INVALID
const signed char NL_ERR_INIT = 80;     // Not initialized
const signed char NL_ERR_INVALID_ARGS = 90;     // Invalid arguments
const signed char NL_ERR_INTERNAL_ERROR = 100;     // Internal error

bool g_Activated = false;

//////////////////////////////////////////////////////////////////////////////
// DLL Functions
InitializeMarket g_initialize;
Finalize g_finalize;
SubscribeTicker g_subscribe;
UnsubscribeTicker g_unsubscribe;
GetAgentNameByID g_getAgentName;
GetAgentShortNameByID g_getAgentShortName;
Reply
#4
This code is a part of a C header file. It doesn't do anything. There is no need to translate it to python. You can as well use an empty python file.
Reply
#5
Bellow the code that I made in python.

# 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))
#TStateCallBack = WINFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
TStateCallBack = WINFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
TNewTradeCallback = WINFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
TNewDailyCallback = WINFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
THistoryTradeCallBack = WINFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
#TProgressCallBack = WINFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature
TTinyBookCallBack = WINFUNCTYPE(c_int, POINTER(c_int)) # specify the correct callback signature

# ////////////////////////////////////////////////////////////////////////////////
# // WARNING: Não utilizar funções da dll dentro do CALLBACK
# ////////////////////////////////////////////////////////////////////////////////

# //Callback do stado das diferentes conexões
def StateCallback(nResult, result):
return 0
# ////////////////////////////////////////////////////////////////////////////////

# //Callback com informações marketData

def NewTradeCallback(TAssetID, assetId, price, vol, qtd, buyAgent, sellAgent, tradeType):
return 1
def NewDailyCallback(TAssetID, TAssetIDRec, date, sOpen, sHigh, sLow, sClose, sVol, sAjuste, sMaxLimit, sMinLimit, sVolBuyer, sVolSeller, nQtd, nNegocios, nContratosOpen, nQtdBuyer, nQtdSeller, nNegBuyer, nNegSeller):
return 2
def HistoryTradeCallBack(TAssetID, assetId, date, price, vol, qtd, buyAgent, sellAgent, tradeType):
return 3
#def TProgressCallBack(TAssetID, assetId, nProgress):
# return 4
def TinyBookCallBack(TAssetID, assetId, price, qtd, side):
return 5

# ////////////////////////////////////////////////////////////////////////////////
# // Functions From ProfitDLL

def MarketCallback():
#


# Initialize DLL
InitializeMarket = Init.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.

# now call the function
#InitializeMarket.argtypes = [c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
InitializeMarket.argtypes = [c_wchar_p, TStateCallBack, TNewTradeCallback, TNewDailyCallback, THistoryTradeCallBack, TTinyBookCallBack]

# now call the function
#result = InitializeMarket(pwc_activation_key, StateCallback, NewTradeCallback, NewDailyCallback, HistoryTradeCallBack, TinyBookCallBack)
result = InitializeMarket(pwc_activation_key, TStateCallBack(StateCallback), TNewTradeCallback(NewTradeCallback), TNewDailyCallback(NewDailyCallback), THistoryTradeCallBack(HistoryTradeCallBack), TTinyBookCallBack(TinyBookCallBack))
#result = InitializeMarket(pwc_activation_key, b'StateCallback', b'NewTradeCallback', b'NewDailyCallback', b'HistoryTradeCallBack', b'TinyBookCallBack')
#s = result.decode("utf8")

ValueError: Procedure probably called with not enough arguments (4 bytes missing)

I don't why I got this error, apparently everything is good, I made some changes with ctypes.CDLL, but I still having problem.
Reply
#6
The error's pretty clear: you didn't pass all the required arguments. Just by looking at the c header file, it looks like you're missing a TProgressCallBack progressCallBack.
Reply
#7
When I pass this argument (TProgressCallBack) I got this error TypeError: item 6 in _argtypes_ has no from_param method.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020