473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

DLL Returning char *

Hey, I've got a bit of a strange problem caused by a situation I'm in.
I'm writing a DLL for a friend of mine in C++, and he plans to use it
in delphi. The DLL needs to return strings, and we doubt that a string
would return well to a delphi program, so the only thing I could think
of was returning a char*, which obviously would be very dangerous as
the array would be being declared as a local variable and would only be
returning it's address.
Anyway, anyone know a solution to such a problem? I can't think of
anything myself, as I'm not that an experienced C++ programmer as it
is, and the whole delphi aspect complicates things for me, as I know
nothing of it.

Jan 18 '07 #1
7 4595

Joey Sabey napsal:
Hey, I've got a bit of a strange problem caused by a situation I'm in.
I'm writing a DLL for a friend of mine in C++, and he plans to use it
in delphi. The DLL needs to return strings, and we doubt that a string
would return well to a delphi program, so the only thing I could think
of was returning a char*, which obviously would be very dangerous as
the array would be being declared as a local variable and would only be
returning it's address.
Anyway, anyone know a solution to such a problem? I can't think of
anything myself, as I'm not that an experienced C++ programmer as it
is, and the whole delphi aspect complicates things for me, as I know
nothing of it.
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++.

Jan 18 '07 #2

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...

Jan 18 '07 #3

Joey Sabey napsal:
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...
I tried to search for it with google and have found this:

http://www.rvelthuis.de/articles/articles-cppobjs.html

Jan 18 '07 #4

Ondra Holub wrote:
Joey Sabey napsal:
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...

I tried to search for it with google and have found this:

http://www.rvelthuis.de/articles/articles-cppobjs.html
Thanks.
I don't really get that, though... Theres a lot of delphi and stuff...
And, if the class functions are accessed as functions, what happens to
the class variables? =/ It's pretty confusing...

Jan 18 '07 #5

"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
Jan 18 '07 #6

Dennis Jones wrote:
>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:
Problem is that the EXE doesn't know how big the data back is gonna be,
it could be small, or a lot bigger, meaning you'd have to pass a very
large, and inefficient, buffer... I doubt that'd would help at all.

Would packing the data into a struct or something help at all..?

Jan 18 '07 #7

"Joey Sabey" <Ga***********@googlemail.comwrote in message
news:11*********************@51g2000cwl.googlegrou ps.com...
>
Dennis Jones wrote:
>>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:

Problem is that the EXE doesn't know how big the data back is gonna be,
it could be small, or a lot bigger, meaning you'd have to pass a very
large, and inefficient, buffer... I doubt that'd would help at all.

Would packing the data into a struct or something help at all..?

No, but you could do like some Windows API functions do and have the
function return the size of the needed buffer. So something like this very
simple example:

int SomeFunc( char *str, int size )
{
char text[] = "some text";
int reqsize = strlen( text ) + 1;

if ( str != NULL && size >= reqsize )
{
strncpy( str, text, size-1 );
str[size-1] = '\0';
}
return reqsize;
}

Now the caller can call SomeFunc() with either a null pointer or an
insufficiently sized buffer and your function will return the necessary
buffer size:

int bufsize = 0;
char *somebuffer = NULL;

int reqsize = SomeFunc( NULL, 0 ); // how much space do we need?
bufsize = reqsize;
somebuffer = new char[bufsize]; // allocate it

reqsize = SomeFunc( somebuffer, bufsize );
if ( reqsize <= bufsize ) // somebuffer was both non-NULL and big enough
to hold the data
{
// use somebuffer
}

- Dennis
Jan 18 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
4
by: jt | last post by:
I'm getting a compiler error: warning C4172: returning address of local variable or temporary Here is the function that I have giving this error: I'm returning a temporary char string and its...
5
by: shyam | last post by:
Hi All I have to write a function which basically takes in a string and returns an unknown number( at compile time) of strings i hav the following syntax in mind char *tokenize(char *) ...
29
by: Gregc. | last post by:
G'day I was wondering if someone can explain the concept of 'returning a Char from a Double' . For example, I have the following code: char getGrade(double mark) { if (mark>= 85) return...
2
by: Beorne | last post by:
I have to call a c++ library funtion returning a string with the following signature: char *get_identifier(); Usually when I have to marshal a function with a char* output parameter I do: ...
1
by: krishna81m | last post by:
In the following code, I am trying to return a char, a char* (a type of non-const without using new, what do we call this type of pointer?) and char* created using new operator. What I do not know at...
7
by: TBass | last post by:
Hi. I wrote a "tag" class to store values. The user gets to store most any type he would need. Instead of getting too complicatated, I decided I would store the value as a stringstream, then...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.