Solved

how can i return multiple values from vc++  dll

Posted on 2008-10-23
5
269 Views
Last Modified: 2012-05-05
i have written a dll in vc++ 6.
i have two variables to return one is _bstr_t and another one is short
how do i retun these variables
0
Comment
Question by:hp746
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22793551
It's only possible to return one variable value.

When you have to return multiple values, the only way to do it is to pass a parameter to the function by reference and set the value of that parameter inside the function.
0
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 22794342
There are several choices that you could use.

If you only have a small amount of data, like you do, you could use a struct. If your returning large objects, remember that they get copied each time you return them.



// Using a struct, or class:
struct MYDATA
{
   int valueA;
   int valueB;
}
 
MYDATA DoSomething()
{
   MYDATA result;
   result.valueA = 1;
   result.valueB = 2;
   return result;
}
 
void foo()
{
   MYDATA result = DoSomething();
   // result.valueA == 1 && result.valueB == 2
}
 
// Using attributes
void DoSomething(int *pValueA, int *pValueB)
{
   *pValueA = 1;
   *pValueB = 2;
}
 
void foo()
{
   int valueA = 0, valueB = 0;   // Don't forget to initialize here!
   DoSomething(&valueA, &valueB);
   // valueA == 1 && valueB == 2
}
 
Or you could use references instead of pointers.
 
void DoSomething(int& rValueA, int& rValueB)
{
   rValueA = 1;
   rValueB = 2;
}
 
void foo()
{
   int valueA = 0, valueB = 0;   // Don't forget to initialize here!
   DoSomething(valueA, valueB);
   // valueA == 1 && valueB == 2
}

Open in new window

0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22794374
If your two variables are global, and your just trying to access them from your executable (or another library) then you need to export them.

// .h Header of Library
extern MY_API int globalA;
extern MY_API int globalB;

// .cpp Source of Library
int globalA = 1;
int globalB = 2;
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 22794817
>>

MYDATA DoSomething()
{
   MYDATA result;
   result.valueA = 1;
   result.valueB = 2;
   return result;
}

I would personally pass a MYDATA into that function, then modify it.  This saves the copying on return.

void DoSomething ( MYDATA& result )
{
   result.valueA = 1;
   result.valueB = 2;
}

But that is just me :)
0
 
LVL 5

Expert Comment

by:Kimpan
ID: 23811726
Just wondering. Since you are using _bstr_t, is it a COM-specific solution you need? Maybe even want it to be OLE Automation?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ syntax question 9 58
Websocket through haproxy reused connection causes "Not a valid frame" error. 4 129
designing in object programming 12 110
No module found pypyodbc, 3 45
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question