8 Replies - 5465 Views - Last Post: 14 June 2012 - 10:54 AM Rate Topic: -----

#1 xXTyMeador73Xx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-May 12

Using STD:Strings with the windows API

Posted 13 June 2012 - 02:14 PM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Using STD:Strings with the windows API

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 876
  • View blog
  • Posts: 2,312
  • Joined: 28-March 11

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?
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,714
  • Joined: 03-August 09

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.

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) 


Was This Post Helpful? 1
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

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)
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
Was This Post Helpful? 2
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,714
  • Joined: 03-August 09

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
std::vector<char> data(str.begin(), str.end());
data.push_back('\0');
SomeCharArrayFunction(data.data());


Was This Post Helpful? 1
  • +
  • -

#6 xXTyMeador73Xx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-May 12

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.)
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Using STD:Strings with the windows API

Posted 13 June 2012 - 10:17 PM

You may want to find out what a LPSTR actually is by looking at the documentation at MSDN. This is a pointer to an array of characters. So to append to this array you will probably need to use something like strcat().

Jim

This post has been edited by jimblumberg: 13 June 2012 - 10:18 PM

Was This Post Helpful? 3
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: Using STD:Strings with the windows API

Posted 14 June 2012 - 04:30 AM

View PostxXTyMeador73Xx, on 14 June 2012 - 05:52 AM, said:

i CANNOT USE std::string with ANY windows API functions.
What code have you tried? and what errors are you getting?
Was This Post Helpful? 0
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,714
  • Joined: 03-August 09

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1