By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

String Access

P: n/a
I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...

TIA.

Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hakan wrote:
I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...

You give us little to go on.

Here is one idea.
#include <string>
#include <cassert>

void BustedApi( void *, int * length );

int const MAXIMUM_SIZE = 1024;

void MyWrapper( std::string & fill_me_in )
{

fill_me_in.resize( MAXIMUM_SIZE );

int length;

BustedApi( static_cast<void *>( &(fill_me_in[0]) ), & length );

assert( length <= MAXIMUM_SIZE );

fill_me_in.resize( length );

}

Jul 22 '05 #2

P: n/a
Hakan escribió:
If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...


None exist, the standard does not guarantee that the string are stored
in a continuous block of memory.

You can use a vector of char instead of manually allocated memory.
Current implementations use a block of memory, and a revision of the
standard will make mandatory this.

Regards.
Jul 22 '05 #3

P: n/a
Thanks, worked great!

On 23 Nov 2003 06:54:37 GMT, Gianni Mariani wrote:
Hakan wrote:
I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...

You give us little to go on.

Here is one idea.
#include <string>
#include <cassert>

void BustedApi( void *, int * length );

int const MAXIMUM_SIZE = 1024;

void MyWrapper( std::string & fill_me_in )
{

fill_me_in.resize( MAXIMUM_SIZE );

int length;

BustedApi( static_cast<void *>( &(fill_me_in[0]) ), & length );

assert( length <= MAXIMUM_SIZE );

fill_me_in.resize( length );

}


Jul 22 '05 #4

P: n/a
Hakan wrote:
Thanks, worked great!

On 23 Nov 2003 06:54:37 GMT, Gianni Mariani wrote:

Hakan wrote:
I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...

You give us little to go on.

Here is one idea.
#include <string>
#include <cassert>

void BustedApi( void *, int * length );

int const MAXIMUM_SIZE = 1024;

void MyWrapper( std::string & fill_me_in )
{

fill_me_in.resize( MAXIMUM_SIZE );

int length;

BustedApi( static_cast<void *>( &(fill_me_in[0]) ), & length );

assert( length <= MAXIMUM_SIZE );

fill_me_in.resize( length );

}


I believe that you need to read Julian Albo's post. I thought that the
upcoming "contiguous" requirement applied to strings as well as vectors.

In other words, this may not be portable across all compliant string
implementations - I have yet to meet one that this does not work for but
you'll find out soon enough.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.