473,396 Members | 1,608 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,396 software developers and data experts.

passing objects through dll boundary?

Hi!

I have seen in a couple of libraries that object are never passed
through dll boundary, instead the results are returned through an
output parameter reference like:

void f(std::string &s)
{
s = "foo";
}

Why is that? Is it a better contruct?? I find this much cleaner:

std::string f(void)
{
return "foo";
}

Another argument could be that it's not safe to return an object, but I
don't understand why. I checked it under Linux and everything is fine.
Lately I have joined the development of a program written in c++ under
MSVC++ 7.1. This code is in a pretty bad shape, so it may not be
connected, but when I try to return a string through dll boundary it
throws an exception.

Any comments?

Szabi

Feb 21 '06 #1
2 9883
* szabi:
Hi!

I have seen in a couple of libraries that object are never passed
through dll boundary, instead the results are returned through an
output parameter reference like:

void f(std::string &s)
{
s = "foo";
}

Why is that? Is it a better contruct?? I find this much cleaner:

std::string f(void)
{
return "foo";
}

Another argument could be that it's not safe to return an object, but I
don't understand why. I checked it under Linux and everything is fine.
Lately I have joined the development of a program written in c++ under
MSVC++ 7.1. This code is in a pretty bad shape, so it may not be
connected, but when I try to return a string through dll boundary it
throws an exception.

Any comments?


Standard C++ does not (yet) officially recognize the existence of shared
libraries or DLLs.

So technically your question is off-topic, I guess.

However, since it pertains to real-life application of C++:

With Windows DLLs you are not guaranteed that the DLL and code using
that DLL are emploing the same heap, or, indeed, any other runtime
library state or even the same runtime library. To return a std::string
from a DLL function you need to ensure that, because the std::string is
likely to just contain a pointer to dynamically allocated memory. And
with most tools the way to do that is to use a DLL version of the
runtime library, both in the DLL in question and in the calling code,
and to be very sure it's the same DLL runtime library version.

Generally it's safer to forego the use of C++ classes entirely in a DLL
interface, and to never have allocation responsibility on one side of
the interface and deallocation responsibility on the other side.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 21 '06 #2
In addition to all sensible replies may I also suggest you define your
"DLL boundary" through (pure) abstract class, including the passing of
arguments. This way all it needs to deal with is just pointers to
abstract base class that does not come with many other implications.

Frankly, this is one of but many plausible solutions. However I found
this way introducing far fewer troubles than the others.

Ben
Feb 23 '06 #3

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

Similar topics

0
by: lawrence | last post by:
I've read that objects should always be passed by reference to other objects. I've also read that future versions of PHP may not support runtime passing by reference (default passing by reference...
6
by: Bryan Martin | last post by:
I have a object that is created in a seperate domain which needs to be passed back to the parent class. Because this object is created in a seperate domain if I try to pass the object back to the...
5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
6
by: Garma | last post by:
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so? Sometimes some objects or their pointers have to be shared among...
0
by: hawklord451 | last post by:
To get a foot hold in the area of web services I have created the following small project. I have created a group of classes to describe a overall Request class. the Request Class is made from a...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
2
by: szabi | last post by:
Hi! I have seen in a couple of libraries that object are never passed through dll boundary, instead the results are returned through an output parameter reference like: void f(std::string &s)...
4
by: Deckarep | last post by:
Hello fellow C# programmers, This question is more about general practice and convention so here goes: I got into a discussion with a co-worker who insisted that as a general practice all...
1
by: Ilyas | last post by:
Hi all What are the best practises for passing custom objects to and from from services? In particular: 1)Should datasets be passed, or custom objects be passed? 2)When it comes to passing...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.