"Joey Sabey" <Ga***********@googlemail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
>
Ondra Holub wrote:
>Hi. I think it is question more related to some delphi forum. If you
are using only C interface for your shared library, it should be
accessible by other languages. Most modern languages have some
capability to call routines written in C/C++.
Well, it might be more suited there, but I doubt it, as I am writing
the thing in C++, and they would likely not be able to help... I might
try looking for a delphi place to ask though...
In the future, a good bet for getting answers that relate to both Delphi AND
C++ is to ask in one of the C++Builder forums since most C++Builder
developers are also generally knowledgeable in Delphi.
To answer your question, Delphi has a "pchar" (pointer to string) type which
is the equivelent of a "char *" in C/C++. But, as you have surmised, you
should not return a char * as a reference a local string in the DLL back to
the EXE, but this would be true no matter what lanaguage the executable was
written in (not just Delphi). For any function that needs to return a char
*, you should have it accept two arguments: a char *, to which you would
copy the string, and a size parameter that specifies how big the buffer is:
void SomeFunc( char * str, int size )
{
strncpy( str, "some text", size-1 ); // don't write past the end of the
given buffer!
str[size-1] = '\0'; // make sure the string is null-terminated!
}
The Delphi program would then call this function passing a 'pchar' (pointer
to some buffer) and a buffer size argument. The Delphi program would own
the memory to the buffer, so you don't have to worry about memory
allocations going across the DLL/EXE boundary.
Then of course, you have to make sure the function is exported by the DLL
and usable by Delphi. To do this, the function must use the standard
calling convention, and must be declared as exportable:
extern __declspec( dllexport ) void __stdcall SomeFunc( char * str, int
size );
To use this function, the Delphi program must define an equivelent prototype
and import the function from the DLL:
SomeFunc : procedure( str : pchar; size : integer ); stdcall;
SomeFunc := GetProcAddress(GetModuleHandle('yourdll.dll'), 'SomeFunc');
I am a C++Builder developer (but by no means a Delphi expert) and none of
the code above has been tested, but it should provide you with enough
information to get you started.
- Dennis