1

I have a function that takes only one LPWSTR as argument. However, I need to add integer to the end of it.

Here's an example pseudocode:

int theNumber = 5;
LPWSTR myLPWSTR = L"Number is: ";
/* Add theNumber to myLPWSTR */
myFunction(myLPWSTR);

How to do this?

I have already tried converting int to LPWSTR and then concatenate them but I didn't succeed. I've tried this for the whole morning but just can't figure out how to do this.

I've tried eg. wsprintf, _itow_s, c_str and pretty much everything I know of - but just can't do it and it's getting really frustrating.

Someone please help.

user2404495
  • 195
  • 1
  • 6
  • 17

2 Answers2

1

Assuming the function really takes a LPWSTR and not a LPCWSTR (i.e. it can modify the string passed in), you'll have to do something like this:

int theNumber = 5;
std::wostringstream s;
s << L"Number is: " << theNumber;
std::wstring str = s.str();
std::vector<wchar_t> buf(str.begin(), str.end());
buf.push_back(0);
myFunctions(&buf[0]);

This is necessary because you need to supply a properly nul-terminated and modifiable buffer of wchar_ts. str.c_str() is nul-terminated but not modifiable. &str[0] is modifiable but not (guaranteed to be) nul-terminated.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • @chris Of course, thanks; edited. I originally had a `+ 1` in there, then decided to go with `push_back()` and didn't update it. Apparently you can get code smell even in an 8-line example. – Angew is no longer proud of SO Jan 06 '14 at 10:19
  • Hehe :) And a small note on the last sentence only applying to pre-C++11. As of C++11, it's a valid C string and modifiable up to and not including the implicit null. Pre-C++11 also has the possible discontiguity. – chris Jan 06 '14 at 10:22
  • @chris Do you have a quote for the C++11 stuff? All I could find is that `s[s.size()]` references a `NUL`, and that `&*(s.begin() + n) == &*s.begin() + n` holds for `0 <= n < s.size()`. – Angew is no longer proud of SO Jan 06 '14 at 11:00
  • That's both the implicit null and contiguity right there, but you might find my related [question](http://stackoverflow.com/questions/14290795/why-is-modifying-a-string-through-a-retrieved-pointer-to-its-data-not-allowed) helpful. – chris Jan 06 '14 at 11:03
  • @chris Thanks, I think I get it now. I was just confused by the fact that it does not (explicitly) require `&*(s.begin() + s.size()) == &*s.begin() + s.size()` to hold. – Angew is no longer proud of SO Jan 06 '14 at 11:08
0

If you can use C++11:

std::wstring str = L"Number is: ";
str += std::to_wstring(42);
myFunction( str.data() );

if not:

std::wstringstream ss;
ss << L"Number is: " << 42;
myFunction( ss.str().c_str() );

Don't forget to include string and sstream.

user1233963
  • 1,450
  • 15
  • 41
  • Not if the function takes a `LPWSTR`. You have to use `&str[0]` and not let it modify the implicit null. Note that this method only works for C++11 and up. Also note that `data()` is equivalent to `c_str()` in C++11. – chris Jan 06 '14 at 10:10
  • This would be short and good, but I got an error: error C2664: 'myFunction' : cannot convert parameter 1 from 'const wchar_t *' to 'LPWSTR' . I will check now the other suggestions, thank you :) – user2404495 Jan 06 '14 at 10:23
  • 1
    Does your function have to take LPWSTR ? can you not change it to LPCWSTR ? If not, use Angews version to copy the new string in a vector and use that. – user1233963 Jan 06 '14 at 10:28
  • What headers do I need to include for Visual Studio to recognise "std"? – ygoe Feb 21 '15 at 12:14