tags:

views:

73

answers:

4

I have a C++ dll from other company. there is a method with a string& msg parameter, void methodA(string& msg);

What I have now is a char* with length which is big enough to take the msg from methodA. I want to call methodA to get message back from methodA.

Can I do it? how? thanks,

+2  A: 

Create a string out of your char* and pass it to methodA. That's how I'd do it.

Not sure what you're looking for here.

Note: Oh, I see it. Took me a moment.

std::string my_msg;
methodA(my_msg);
strcpy(my_char_star, my_msg.c_str());

I do have to say that this is basically exactly the opposite of the kind of code you should be writing. You should be using std::string or std::vector to provide char* buffer arguments, not char* to replace std::string.

Noah Roberts
@Noah which way is better? yours or Evan Teran's?
5YrsLaterDBA
@5YrsLaterDBA: both are just about the same. @Noah: +1 for a decent answer.
Evan Teran
@Evan, wouldn't it be informative to clarify that this is *not* **secure**
Gollum
indeed. The OP said that the `char` buffer was "big enough" so I figured it didn't need mentioning, but you are of course right.
Evan Teran
@5Yrs - Evan's. Strictly speaking, strncpy is the better function to use. I simply never use the C api anymore so it slipped my mind. Neither answer is particularly good since, like I said, you're basically doing exactly the opposite that good C++ standards would imply. The only redeeming quality of either is that they answer the question you asked.
Noah Roberts
+3  A: 

Sounds like you will need to use a pattern like this. It is unclear whether the paramter is an in/out or simply an out parameter. So you'll need one of these...

IN/OUT:

const char s[BIG_ENOUGH] = "whatever";
std::string str(s);
methodA(str);
// str should now have the response according to your API description

OUT:

std::string str;
methodA(str);
// str should now have the response according to your API description

// if you need to result in `s`...
strncpy(s, str.c_str(), BIG_ENOUGH);
s[BIG_ENOUGH - 1] = '\0'; // just to be safe
Evan Teran
why there is a need of copying "s"? its anyway going to be overwritten and string would do a complete new allocation for whatever its putting in (if length exceeds).
Gollum
@Gollum, I am assuming that the input value is meaningful. If it isn't then there is no need to copy.
Evan Teran
@Evan which way is better? yours or Noah Roberts'? I am learning. I don't want you guys fight each other, only discuss.
5YrsLaterDBA
@5YrsLaterDBA: just about the same really. my `strncpy` is possibly overly cautious.
Evan Teran
@5YrsLaterDBA using algorithm is more 'C++y', but you're mixing C strings and std::strings anyway. There's a possibility that strncpy will have a more optimal implementation, as it can assume that it is copying plain data rather than objects, whereas the stl could work that out via specialisation, but might not. This (revision 2) is safer if BIG_ENOUGH isn't, though both cases could add something report that case as an error.
Pete Kirkham
@5YrsLaterDBA, its not a fight :D. just learning by asking, and its a good habit.I like when people ask me questions, makes me think consciously of what I am doing. So in return I ask too ;).
Gollum
+4  A: 
#include <algorithm>

void foo(char* buffer)
{
    std::string str;
    methodA(str);
    std::copy(str.begin(), str.end(), buffer);
    buffer[str.size()] = 0;
}
FredOverflow
+1  A: 

Yes you can.

#include <string>

void yourfunc( char * mymsg, int len ) {
  string msg;
  methodA(msg);
  if( msg.length() < len ) {
    strncpy( mymsg, msg.c_str(), len );
  } else { // FAIL }
}
Greg Domjan