Calling a Delphi DLL

K

kelvin.koogan

How can I call a function in a Delphi DLL from C++/CLI?

The Delphi function is declared as follows:

function Func1(IsDsb: Boolean; FirstStr, SecondStr : String): String;

I've tried

[DllImport("mydll", EntryPoint="Func1")]
extern "C" char *Func1(int isDsb, char *firstString, char
*secondString);

Func1(0, "kl;kl;sdfk", "kdlks;ldfk");

but I get an access violation. What should I be doing?

What calling convention will the Delphi function use? Does it need to
have stdcall added?

TIA,
KK
 
C

Cholo Lennon

How can I call a function in a Delphi DLL from C++/CLI?

The Delphi function is declared as follows:

function Func1(IsDsb: Boolean; FirstStr, SecondStr : String): String;

I've tried

[DllImport("mydll", EntryPoint="Func1")]
extern "C" char *Func1(int isDsb, char *firstString, char
*secondString);

Func1(0, "kl;kl;sdfk", "kdlks;ldfk");

but I get an access violation. What should I be doing?

What calling convention will the Delphi function use? Does it need to
have stdcall added?

In Delphi you should...

- Use "PChar" instead of "String" (String is a delphi type, C++ CLI doesn't
known anything about Delphi strings)
- Use "stdcall" calling convention (By default Delphi use its own calling
convention).

Regards
 
C

Cholo Lennon

Cholo said:
How can I call a function in a Delphi DLL from C++/CLI?

The Delphi function is declared as follows:

function Func1(IsDsb: Boolean; FirstStr, SecondStr : String): String;

I've tried

[DllImport("mydll", EntryPoint="Func1")]
extern "C" char *Func1(int isDsb, char *firstString, char
*secondString);

Func1(0, "kl;kl;sdfk", "kdlks;ldfk");

but I get an access violation. What should I be doing?

What calling convention will the Delphi function use? Does it need to
have stdcall added?

In Delphi you should...

- Use "PChar" instead of "String" (String is a delphi type, C++ CLI
doesn't known anything about Delphi strings)
- Use "stdcall" calling convention (By default Delphi use its own
calling convention).

One more thing...

If you use "int" for boolean type in C++ CLI and if you're in 32 bits
architecture, use LongBool in Delphi. Or, in opposition, use bool in C++ CLI if
you use boolean in delphi.


Regards
 
K

kelvin.koogan

How can I call a function in a Delphi DLL from C++/CLI?
The Delphi function is declared as follows:
function Func1(IsDsb: Boolean; FirstStr, SecondStr : String): String;
I've tried
[DllImport("mydll", EntryPoint="Func1")]
extern "C" char *Func1(int isDsb, char *firstString, char
*secondString);
Func1(0, "kl;kl;sdfk", "kdlks;ldfk");
but I get an access violation. What should I be doing?
What calling convention will the Delphi function use? Does it need to
have stdcall added?

In Delphi you should...

- Use "PChar" instead of "String" (String is a delphi type, C++ CLI doesn't
known anything about Delphi strings)
- Use "stdcall" calling convention (By default Delphi use its own calling
convention).

Regards

--
Cholo Lennon
Bs.As.
ARG- Hide quoted text -

- Show quoted text -

Thanks. I ShortString as good as PChar in this instance?

KK
 
P

Pavel Minaev

Thanks. I ShortString as good as PChar in this instance?

No. You need to use PChar (which is really just Char^, that is, a
pointer to characters of the string).
 
C

Cholo Lennon

How can I call a function in a Delphi DLL from C++/CLI?
The Delphi function is declared as follows:
function Func1(IsDsb: Boolean; FirstStr, SecondStr : String):
String;
I've tried
[DllImport("mydll", EntryPoint="Func1")]
extern "C" char *Func1(int isDsb, char *firstString, char
*secondString);
Func1(0, "kl;kl;sdfk", "kdlks;ldfk");
but I get an access violation. What should I be doing?
What calling convention will the Delphi function use? Does it need
to have stdcall added?

In Delphi you should...

- Use "PChar" instead of "String" (String is a delphi type, C++ CLI
doesn't known anything about Delphi strings)
- Use "stdcall" calling convention (By default Delphi use its own
calling convention).

Regards

--
Cholo Lennon
Bs.As.
ARG- Hide quoted text -

- Show quoted text -

Thanks. I ShortString as good as PChar in this instance?

No. PChar (pointer to char) is equivalent to char*. ShortString is another
Delphi type and C++ CLI doesn't know anything about it. BTW ShortString
internally has (or Is) an array with up 255 chars. In this array the string
length is stored at the 1st byte and IIRC the string isn't terminated with NULL
(necessary in every C function that expects a tipical string). This shows the
incompatiblity between ShortString and the C++ CLI point of view about the array
of chars.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top