Im having trouble using Std::Strings's with the windows API because it wants you to use its stupid built in datatypes (LPCSTR, LPSTR). my question is this. how do i either, A: use Std::Strings instead of LPSTR in the windows API, or B: how do i append a string literal or another LPSTR onto an LPSTR?
Using STD:Strings with the windows API
Page 1 of 18 Replies - 1657 Views - Last Post: 14 June 2012 - 10:54 AM
Replies To: Using STD:Strings with the windows API
#2
Re: Using STD:Strings with the windows API
Posted 13 June 2012 - 02:26 PM
Quote
stupid built in datatypes
What is so stupid about passing a string pointer to a function? All a pointer is, is the address of the first character of a string. How to apoend a string to another string?
What API are you trying to use?
#3
Re: Using STD:Strings with the windows API
Posted 13 June 2012 - 02:26 PM
for LPCSTR(constant character pointer) you can use the c_str() method.
for LSTR you will need to create a new string
WindowsFunction(str.c_str());
for LSTR you will need to create a new string
char* new_c_str = new char[str.size() + 1] new_c_str[str.size()] = '\0'; std::copy(str.begin(), str.end(), new_c_str); //copy the string over WindowsFunction(new_c_str); //...what ever you need to do with new_c_str delete new_c_str(ALWAYS needs to be done)
#4
Re: Using STD:Strings with the windows API
Posted 13 June 2012 - 03:06 PM
If you need a writable character buffer to use with a C-like API, you can use a std::vector<char> whose underlying representation is guaranteed to be contiguous (i.e. an array)
(If you're using a C++11 implementation, you can use std::vector<>::data() to gain access to the array buffer instead of that ugly &[0] syntax)
for string manipulation, you're probably still better off swapping in and out of std::stringstream or std::string
std::vector<char> buffer(100); SomeCharArrayFunction( &buffer[0], buffer.size() ); std::string str( &buffer[0] );
(If you're using a C++11 implementation, you can use std::vector<>::data() to gain access to the array buffer instead of that ugly &[0] syntax)
std::vector<char> buffer(100); SomeCharArrayFunction( buffer.data(), buffer.size() ); std::string str( buffer.data() );
for string manipulation, you're probably still better off swapping in and out of std::stringstream or std::string
#5
Re: Using STD:Strings with the windows API
Posted 13 June 2012 - 03:37 PM
bench: that's a good solution!
here it is for null terminated strings
here it is for null terminated strings
std::vector<char> data(str.begin(), str.end());
data.push_back('\0');
SomeCharArrayFunction(data.data());
#6
Re: Using STD:Strings with the windows API
Posted 13 June 2012 - 09:52 PM
none of these do me any good. as i stated before. im using the windows API which requires me to use their built in data types with their functions. (IE LPSTR instead of std::string. i CANNOT USE std::string with ANY windows API functions. how do i append to a LPSTR? this is what i am asking.)
#7
Re: Using STD:Strings with the windows API
Posted 13 June 2012 - 10:17 PM
#8
Re: Using STD:Strings with the windows API
Posted 14 June 2012 - 04:30 AM
#9
Re: Using STD:Strings with the windows API
Posted 14 June 2012 - 10:54 AM
we showed you how std::string can be used where C strings are expected; so you *can* use C++ strings with the windows API functions.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote










|