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

Using string where a char* required

P: n/a
I am working with a C API which often requires a char* or char buffer. If a
C function returns a char* I can't use string? Or can I? I realise I can
pass a char* using c_str() but what about receiving a char buffer into a
string?

Reason I ask is otherwise I have to guess at what size buffer to create?
And then copy buffer to a string. Doesn't seem ideal.

Jul 28 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"Angus" writes:
>I am working with a C API which often requires a char* or char buffer. If
a
C function returns a char* I can't use string? Or can I? I realise I can
pass a char* using c_str() but what about receiving a char buffer into a
string?

Reason I ask is otherwise I have to guess at what size buffer to create?
And then copy buffer to a string. Doesn't seem ideal.
One of the constructors for std::string will, in effect, accept a char* as
an parameter.

Thus:

char* foo { /*stuff*/ }

std::string s;
s= foo();

Or at least something similar to that.
Jul 28 '07 #2

P: n/a

"osmium" <r1********@comcast.netwrote in message
news:5h*************@mid.individual.net...
"Angus" writes:
I am working with a C API which often requires a char* or char buffer.
If
a
C function returns a char* I can't use string? Or can I? I realise I
can
pass a char* using c_str() but what about receiving a char buffer into a
string?

Reason I ask is otherwise I have to guess at what size buffer to create?
And then copy buffer to a string. Doesn't seem ideal.

One of the constructors for std::string will, in effect, accept a char* as
an parameter.

Thus:

char* foo { /*stuff*/ }

std::string s;
s= foo();

Or at least something similar to that.
For example in Windows there is a GetUserName function like this:
BOOL WINAPI GetUserName ( LPWSTR lpBuffer, LPDWORD nSize )

if you do this:
std::string str;

DWORD bufsize = 256;

GetUserName(str, &bufsize);

you get compile error:

error C2664: 'GetUserNameA' : cannot convert parameter 1 from 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char' to 'char *'
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called
I suppose I could cast? But wondering what best way to tackle is.

Jul 28 '07 #3

P: n/a
Angus wrote:
"osmium" <r1********@comcast.netwrote in message
news:5h*************@mid.individual.net...
>"Angus" writes:
>>I am working with a C API which often requires a char* or char
buffer. If a
C function returns a char* I can't use string? Or can I? I
realise I can pass a char* using c_str() but what about receiving a
char buffer into a string?

Reason I ask is otherwise I have to guess at what size buffer to
create? And then copy buffer to a string. Doesn't seem ideal.

One of the constructors for std::string will, in effect, accept a
char* as an parameter.

Thus:

char* foo { /*stuff*/ }

std::string s;
s= foo();

Or at least something similar to that.

For example in Windows there is a GetUserName function like this:
BOOL WINAPI GetUserName ( LPWSTR lpBuffer, LPDWORD nSize )

if you do this:
std::string str;

DWORD bufsize = 256;

GetUserName(str, &bufsize);

you get compile error:

error C2664: 'GetUserNameA' : cannot convert parameter 1 from 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char' to 'char *'
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called
I suppose I could cast? But wondering what best way to tackle is.
The best way is to allocate a temporary (local) array of TCHAR (or
whatever type is LPWSTR points to) and then _after_ you call the
function that would fill in that array, create a string from it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '07 #4

P: n/a
"Angus" writes:
"osmium" <r1********@comcast.netwrote in message
news:5h*************@mid.individual.net...
>"Angus" writes:
>I am working with a C API which often requires a char* or char buffer.
If
>a
C function returns a char* I can't use string? Or can I? I realise I
can
pass a char* using c_str() but what about receiving a char buffer into
a
string?

Reason I ask is otherwise I have to guess at what size buffer to
create?
And then copy buffer to a string. Doesn't seem ideal.

One of the constructors for std::string will, in effect, accept a char*
as
an parameter.

Thus:

char* foo { /*stuff*/ }

std::string s;
s= foo();

Or at least something similar to that.

For example in Windows there is a GetUserName function like this:
BOOL WINAPI GetUserName ( LPWSTR lpBuffer, LPDWORD nSize )

if you do this:
std::string str;

DWORD bufsize = 256;

GetUserName(str, &bufsize);

you get compile error:

error C2664: 'GetUserNameA' : cannot convert parameter 1 from 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char' to 'char *'
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called
I suppose I could cast? But wondering what best way to tackle is.
I wonder if LPWSTR is, by chance, Microsoft gobbledygook for wide string?

Your question asked about a char* and char pointer points at a *byte*. So
all bets are off.

I get a headache just thinking about that crappy Microsoft stuff. If my
guess is right about the 'W', I guess the *right* way to proceed is to
fiddle with locales or facets or traits or some of that other high faultin'
stuff. But that's not the way I would proceed. I would find out what that
damn acronym represented in the real world and convert/cast/whatever to make
the thing work.
Jul 28 '07 #5

P: n/a

"osmium" <r1********@comcast.netwrote in message
news:5h*************@mid.individual.net...
"Angus" writes:
"osmium" <r1********@comcast.netwrote in message
news:5h*************@mid.individual.net...
"Angus" writes:

I am working with a C API which often requires a char* or char buffer.
If
a
C function returns a char* I can't use string? Or can I? I realise
I
can
pass a char* using c_str() but what about receiving a char buffer
into
a
string?

Reason I ask is otherwise I have to guess at what size buffer to
create?
And then copy buffer to a string. Doesn't seem ideal.

One of the constructors for std::string will, in effect, accept a char*
as
an parameter.

Thus:

char* foo { /*stuff*/ }

std::string s;
s= foo();

Or at least something similar to that.
For example in Windows there is a GetUserName function like this:
BOOL WINAPI GetUserName ( LPWSTR lpBuffer, LPDWORD nSize )

if you do this:
std::string str;

DWORD bufsize = 256;

GetUserName(str, &bufsize);

you get compile error:

error C2664: 'GetUserNameA' : cannot convert parameter 1 from 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char' to 'char *'
No user-defined-conversion operator available that can perform
this
conversion, or the operator cannot be called
I suppose I could cast? But wondering what best way to tackle is.

I wonder if LPWSTR is, by chance, Microsoft gobbledygook for wide string?

Your question asked about a char* and char pointer points at a *byte*. So
all bets are off.

I get a headache just thinking about that crappy Microsoft stuff. If my
guess is right about the 'W', I guess the *right* way to proceed is to
fiddle with locales or facets or traits or some of that other high
faultin'
stuff. But that's not the way I would proceed. I would find out what that
damn acronym represented in the real world and convert/cast/whatever to
make
the thing work.
No I copied it wrong - should have been
BOOL WINAPI GetUserName ( LPSTR lpBuffer, LPDWORD nSize )
Jul 28 '07 #6

P: n/a
"Angus" writes:
No I copied it wrong - should have been
BOOL WINAPI GetUserName ( LPSTR lpBuffer, LPDWORD nSize )
Well, this works. I don't know what else to say. This is exactly what I
thought you meant when I read your first post.

----------------
#include <iostream>

#define STOP while(1) cin.get();

using namespace std;

char* foo()
{
return "hello";
}
//============
int main()
{
string s;
s = foo();
cout << s << endl;

STOP;
}
Jul 28 '07 #7

P: n/a

Angus <no****@gmail.comwrote in message...
>
No I copied it wrong - should have been
BOOL WINAPI GetUserName ( LPSTR lpBuffer, LPDWORD nSize )

// includes here; <iostream>, <vector>, <cctype>, "windows.h", etc.
{ // main() or ?
std::vector<charBuff( 100, '\0' );
DWORD DSize( static_cast<DWORD>( Buff.size()-1 ) );
std::string User1;
if( GetUserName( &Buff.at(0), &DSize ) ){
std::cout<<"GetUserName()=";
for( std::size_t a(0); a < Buff.size(); ++a ){
if( not std::isprint( Buff.at(a) ) ){ break;} // <cctype>
std::cout<<Buff.at(a);
User1.push_back( Buff.at(a) );
} // for(a)
} // if(Get)
else{
std::cout<<"GetUserName() FAILED"<<std::endl;
}
std::cout<<'\n'<<User1<<std::endl;
}

Clunky? Yup!!

If it doesn't work for you, post back with first 3 errors.

--
Bob R
POVrookie
Jul 28 '07 #8

P: n/a
On Jul 29, 2:18 am, "Angus" <nos...@gmail.comwrote:
"osmium" <r124c4u...@comcast.netwrote in message

news:5h*************@mid.individual.net...
"Angus" writes:
"osmium" <r124c4u...@comcast.netwrote in message
>news:5h*************@mid.individual.net...
>"Angus" writes:
>I am working with a C API which often requires a char* or char buffer.
If
>a
C function returns a char* I can't use string? Or can I? I realise
I
can
pass a char* using c_str() but what about receiving a char buffer
into
a
string?
Reason I ask is otherwise I have to guess at what size buffer to
create?
And then copy buffer to a string. Doesn't seem ideal.
>One of the constructors for std::string will, in effect, accept a char*
>as
>an parameter.
>Thus:
>char* foo { /*stuff*/ }
>std::string s;
>s= foo();
>Or at least something similar to that.
For example in Windows there is a GetUserName function like this:
BOOL WINAPI GetUserName ( LPWSTR lpBuffer, LPDWORD nSize )
if you do this:
std::string str;
DWORD bufsize = 256;
GetUserName(str, &bufsize);
you get compile error:
error C2664: 'GetUserNameA' : cannot convert parameter 1 from 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char' to 'char *'
No user-defined-conversion operator available that can perform
this
conversion, or the operator cannot be called
I suppose I could cast? But wondering what best way to tackle is.
I wonder if LPWSTR is, by chance, Microsoft gobbledygook for wide string?
Your question asked about a char* and char pointer points at a *byte*. So
all bets are off.
I get a headache just thinking about that crappy Microsoft stuff. If my
guess is right about the 'W', I guess the *right* way to proceed is to
fiddle with locales or facets or traits or some of that other high
faultin'
stuff. But that's not the way I would proceed. I would find out what that
damn acronym represented in the real world and convert/cast/whatever to
make
the thing work.

No I copied it wrong - should have been
BOOL WINAPI GetUserName ( LPSTR lpBuffer, LPDWORD nSize )
Even you can write this:

DWORD dwLen = 256;
std::string strUsername(dwLen '\0');
char* pData = const_cast<char*>(strUsername.c_str());
if (GetUserName(pData, &dwLen)
{
std::cout << "Get User name success." << strUsername;
}
else
{
std::cout << "Get user name failed"
}

This may work OK when strUsername is just a temporary variant, but
when copy a std::string to another std::string, it just increase it
reference counts and really did copy on changed.

When you wanted to call Windows API, just using CHAR and TCHAR, or
CString(if you application supports MFC), IMO, std didn't fill any
cases.

DWORD dwLen = 256;
CHAR* szUserName = new CHAR[dwLen]; //if GetUserName parameter is
LPWSTR, using TCHAR.
if (GetUserName(szUserName, &dwLen) {
...
} else {
...
}

Jul 29 '07 #9

P: n/a
On 29 Jul, 13:57, "sss.z...@gmail.com" <sss.z...@gmail.comwrote:
Even you can write this:

DWORD dwLen = 256;
std::string strUsername(dwLen '\0');
char* pData = const_cast<char*>(strUsername.c_str());
if (GetUserName(pData, &dwLen)
:
This may work OK when strUsername is just a temporary variant, but
when copy a std::string to another std::string, it just increase it
reference counts and really did copy on changed.
No mays or buts, this is wrong. You can't go scribbling over
the value returned from c_str(). Even if it happens to work on your
compiler and standard library implementation today it may not
tomorrow.

When you wanted to call Windows API, just using CHAR and TCHAR, or
CString(if you application supports MFC), IMO, std didn't fill any
cases.

DWORD dwLen = 256;
CHAR* szUserName = new CHAR[dwLen]; //if GetUserName parameter is
LPWSTR, using TCHAR.
if (GetUserName(szUserName, &dwLen) {
...
This is better, but if you're using a constant dwLen then
there's no point in dynamically allocating the buffer.
Note that according to MSDN the dwLen parameter is in/out,
so there's no risk of a buffer overflow here.

Jul 30 '07 #10

P: n/a
On 29/7/07 13:57, in article
11**********************@x40g2000prg.googlegroups. com, "ss******@gmail.com"
<ss******@gmail.comwrote:
When you wanted to call Windows API, just using CHAR and TCHAR, or
CString(if you application supports MFC), IMO, std didn't fill any
cases.

DWORD dwLen = 256;
CHAR* szUserName = new CHAR[dwLen]; //if GetUserName parameter is
LPWSTR, using TCHAR.
if (GetUserName(szUserName, &dwLen) {
...
} else {
...
}
What's wrong with

DWORD dwLen = 256;
std::string userName( dwLen, '\0' );
if (GetUserName(userName.data(), &dwLen) {
userName.resize( dwLen );
...
} else {
...
}
(You may need to use std::wstring if you're using Unicode)

--
Regards,
Steve

"...which means he created the heaven and the earth... in the DARK! How good
is that?"

Aug 2 '07 #11

P: n/a
On Aug 2, 7:52 am, Steve Folly <moderatedn...@spfweb.co.ukwrote:
On 29/7/07 13:57, in article
1185713820.179219.134...@x40g2000prg.googlegroups. com, "sss.z...@gmail.com"
<sss.z...@gmail.comwrote:
When you wanted to call Windows API, just using CHAR and TCHAR, or
CString(if you application supports MFC), IMO, std didn't fill any
cases.
DWORD dwLen = 256;
CHAR* szUserName = new CHAR[dwLen]; //if GetUserName parameter is
LPWSTR, using TCHAR.
if (GetUserName(szUserName, &dwLen) {
...
} else {
...
}
What's wrong with
DWORD dwLen = 256;
std::string userName( dwLen, '\0' );
if (GetUserName(userName.data(), &dwLen) {
userName.resize( dwLen );
...
} else {
...
}
If GetUserName takes a char const* as first parameter, nothing.
Somehow, though, I doubt that this is the case.

In practice, you can write:

DWORD dwLen = 256 ;
std::string userName( dwLen, '\0' ) ;
if ( GetUserName( &userName[ 0 ], &dwLen ) ) ...

While formally undefined behavior according to the current
standard, it will work in actual practice, and will be
guaranteed in the next version of the standard (in which
std::string will also have a non-const data() function).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 2 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.