1

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,

1
  • 4
    Be warned stl types on dll APIs is not a good idea. If you are using a different compiler from the other company you will have problems. Commented Jun 22, 2010 at 22:57

4 Answers 4

4
#include <algorithm>

void foo(char* buffer)
{
    std::string str;
    methodA(str);
    std::copy(str.begin(), str.end(), buffer);
    buffer[str.size()] = 0;
}
Sign up to request clarification or add additional context in comments.

Comments

3

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

6 Comments

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, I am assuming that the input value is meaningful. If it isn't then there is no need to copy.
@Evan which way is better? yours or Noah Roberts'? I am learning. I don't want you guys fight each other, only discuss.
@5YrsLaterDBA: just about the same really. my strncpy is possibly overly cautious.
@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.
|
2

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.

5 Comments

which way is better? yours or Evan Teran's?
@5YrsLaterDBA: both are just about the same. @Noah: +1 for a decent answer.
@Evan, wouldn't it be informative to clarify that this is not secure
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.
@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.
1

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 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.