?
Solved

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

Posted on 2003-10-21
4
Medium Priority
?
251 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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