Solved

How do I return a string from C++ DLL?

Posted on 2003-10-21
4
249 Views
Last Modified: 2010-04-01
Hello,

I have the following C++ code in a DLL which all works fine. However, it is horribly inefficient. Please help to simplify.  

(1) Here is the working C++ function:

__declspec(dllexport) LPSTR __stdcall PopStrData2(const char *object, char *retval){
      if(object == NULL) object = "";
      BPCHAR p;
      if(!BeginTransaction()) return FALSE;
      if(!Pop(&pSharedData->pStrStacks, object, &p)) {EndTransaction();return FALSE;}
      strcpy(retval, p);
      hfree(p);
      EndTransaction();
      return retval;
}

(2) I am calling the function from another program in the format:

Dim Buffer="                                            "
MyString = PopStrData2("OBJECT", Buffer)


(3) QUESTION

Is there someway to get rid of the "char *retval" parameter in the C++ function PopStrData2(const char *object, char *retval) ?

In otherwords, I want to simply call:

Dim MyMsg as String
MyMsg = PopStrData2("OBJECT")

What changes do I need to make to my C++ dll function?

0
Comment
Question by:deming
[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
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
dualsoul earned 125 total points
ID: 9597292
VB can't work directly with char* datatype in C++.
So you shoul use BSTR type in your C++ code and using global memory allocation for it.You can do it by using
family of function: SysAllocString

for instance: (creating a string):

   BSTR string=SysAllocString(OLESTR("some string")); //note BSTR is unicode string, not as char*
  *result=&string; //return result to VB
   
0
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 9601519
BSTR is unicode too btw
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 9601569
> BSTR is unicode too btw

it's just what i've said :)
BSTR is the pointer to sequence 16bit character
0
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 9601606
upsi ;) Didnt see that.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

724 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