Solved

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

Posted on 2003-10-21
4
246 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

785 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