Using string where a char* required

A

Angus

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.
 
O

osmium

Angus said:
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.
 
A

Angus

osmium said:
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.
 
V

Victor Bazarov

Angus said:
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
 
O

osmium

Angus said:
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.
 
A

Angus

osmium said:
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 )
 
O

osmium

Angus said:
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;
}
 
B

BobR

Angus said:
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<char> Buff( 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.
 
S

sss.zhou

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 {
...
}
 
T

tragomaskhalos

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.
 
S

Steve Folly

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?"
 
J

James Kanze

On 29/7/07 13:57, in article
(e-mail address removed), "(e-mail address removed)"
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,290
Messages
2,571,453
Members
48,129
Latest member
DianneCarn

Latest Threads

Top