Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now