Answer the question
In order to leave comments, you need to log in
Calling a Delphi function from a C++ DLL?
Good afternoon. Prompt as correctly:
1) To transfer from the application written on Delphi the pointer on function in DLL written on C ++?
2) Call a function from a DLL with parameter passing and return the result?
Well, actually how I solved this issue.
1. Described the types of functions.
TSendPacketStr = procedure (pck: string; const tid: integer; const ToServer: Boolean);stdcall;
TGetConnectionName = function(id : integer):string;stdcall;
TGetConnectioIdByName = function(name : string):integer;stdcall;
PSendPacketStr = ^TSendPacketStr;
PGetConnectionName = ^TGetConnectionName;
PGetConnectioIdByName = ^TGetConnectioIdByName;
TCFunctionStruct = record
pSendPacketStr: PSendPacketStr;
pGetConnectionName: PGetConnectionName;
pGetConnectioIdByName: PGetConnectioIdByName;
end;
PCFunctionStruct = ^TCFunctionStruct;
TSetCFStruct = function(const struct: PCFunctionStruct): Boolean; stdcall;
SetCFStruct: TSetCFStruct;
@SetCFStruct:=GetProcAddress(hLib, 'SetFunctionStruct');
if(Assigned(SetCFStruct)) then
begin
ScriptStruct.pSendPacketStr:[email protected];
ScriptStruct.pGetConnectionName:[email protected];
ScriptStruct.pGetConnectioIdByName:[email protected];
SetCFStruct(@ScriptStruct);
end;
// Структура скриптов
struct ScriptStruct
{
void(*sendPacketStr)(unsigned char* pck, const int tid, bool toServer);
char*(*getConnectionName)(int);
int(*getConnectioIdByName)(char* name);
};
/// Функция передачи указателя на ScriptStruct
DLL_API bool SetFunctionStruct(ScriptStruct * scriptStruct);
// Вызывается при включении плагина
DLL_API bool SetFunctionStruct(ScriptStruct * scriptStruct)
{
OutputDebugStringA("SetFunctionStruct\n");
_scriptStruct->getConnectionName(123); // <- Здесь я пытаюсь вызвать функцию из Delphi
return true;
}
function TPluginStructClass.getConnectionName(id: integer): string; stdcall;
begin
result := dmData.ConnectNameById(id); /// <- вызывается
end;
Answer the question
In order to leave comments, you need to log in
You defined the function as stdcall in delphi, but not in c. It should be char* (__stdcall *getConnectionName)(int);
Well, plus the sish char* is not equal to the Delphi string, char* will be PAnsiChar
Delphi has a different calling convention, function parameters are in registers, not on the stack. Plus, there may be other work with the stack. In C / C ++ - stdcall, if I'm not mistaken, the parameters are strictly on the stack. This is where something goes wrong. It is correct to google or not to use Delphi, since there can still be a lot of pitfalls.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question