Using string where a char* required | | |
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. | | | | re: Using string where a char* required
"Angus" writes: Quote:
>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. | | | | re: Using string where a char* required
"osmium" <r124c4u102@comcast.netwrote in message
news:5h1bnuF3gkh4bU1@mid.individual.net... Quote:
"Angus" writes:
> Quote:
I am working with a C API which often requires a char* or char buffer.
If Quote: Quote:
a
C function returns a char* I can't use string? Or can I? I realise I
can Quote: Quote:
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. | | | | re: Using string where a char* required
Angus wrote: Quote:
"osmium" <r124c4u102@comcast.netwrote in message
news:5h1bnuF3gkh4bU1@mid.individual.net... Quote:
>"Angus" writes:
>> Quote:
>>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 | | | | re: Using string where a char* required
"Angus" writes: Quote:
"osmium" <r124c4u102@comcast.netwrote in message
news:5h1bnuF3gkh4bU1@mid.individual.net... Quote:
>"Angus" writes:
>> Quote:
>I am working with a C API which often requires a char* or char buffer.
If Quote: Quote:
>a
C function returns a char* I can't use string? Or can I? I realise I
can Quote: Quote:
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. | | | | re: Using string where a char* required
"osmium" <r124c4u102@comcast.netwrote in message
news:5h1f10F3ibetsU1@mid.individual.net... Quote:
"Angus" writes:
> Quote:
"osmium" <r124c4u102@comcast.netwrote in message
news:5h1bnuF3gkh4bU1@mid.individual.net... Quote:
"Angus" writes:
>
I am working with a C API which often requires a char* or char buffer.
If Quote:
a
C function returns a char* I can't use string? Or can I? I realise
I Quote: Quote:
can Quote:
pass a char* using c_str() but what about receiving a char buffer
into Quote: Quote: Quote:
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 Quote: Quote:
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' Quote:
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 No I copied it wrong - should have been
BOOL WINAPI GetUserName ( LPSTR lpBuffer, LPDWORD nSize ) | | | | re: Using string where a char* required
"Angus" writes: Quote:
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;
} | | | | re: Using string where a char* required
Angus <nospam@gmail.comwrote in message... Quote:
>
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 | | | | re: Using string where a char* required
On Jul 29, 2:18 am, "Angus" <nos...@gmail.comwrote: Quote:
"osmium" <r124c4u...@comcast.netwrote in message
>
news:5h1f10F3ibetsU1@mid.individual.net...
>
>
> > Quote: Quote:
"osmium" <r124c4u...@comcast.netwrote in message
>news:5h1bnuF3gkh4bU1@mid.individual.net...
>"Angus" writes:
> Quote: Quote:
>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 Quote: Quote:
can
pass a char* using c_str() but what about receiving a char buffer
into > Quote: Quote:
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.
> Quote: Quote:
>One of the constructors for std::string will, in effect, accept a char*
>as
>an parameter.
> > Quote: Quote:
>char* foo { /*stuff*/ }
> Quote: Quote:
>std::string s;
>s= foo();
> Quote: Quote:
>Or at least something similar to that.
> Quote: Quote:
For example in Windows there is a GetUserName function like this:
BOOL WINAPI GetUserName ( LPWSTR lpBuffer, LPDWORD nSize )
> Quote: Quote:
if you do this:
std::string str;
> Quote: Quote:
DWORD bufsize = 256;
> Quote: Quote:
GetUserName(str, &bufsize);
> Quote: Quote:
you get compile error:
> Quote: Quote:
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 Quote: Quote:
conversion, or the operator cannot be called
> Quote: Quote:
I suppose I could cast? But wondering what best way to tackle is.
> Quote:
I wonder if LPWSTR is, by chance, Microsoft gobbledygook for wide string?
> Quote:
Your question asked about a char* and char pointer points at a *byte*. So
all bets are off.
> Quote:
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' Quote:
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 >
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 {
...
} | | | | re: Using string where a char* required
On 29 Jul, 13:57, "sss.z...@gmail.com" <sss.z...@gmail.comwrote: Quote:
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. Quote:
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. | | | | re: Using string where a char* required
On 29/7/07 13:57, in article 1185713820.179219.134040@x40g2000prg.googlegroups. com, "sss.zhou@gmail.com"
<sss.zhou@gmail.comwrote: Quote:
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?" | | | | re: Using string where a char* required
On Aug 2, 7:52 am, Steve Folly <moderatedn...@spfweb.co.ukwrote: Quote:
On 29/7/07 13:57, in article
1185713820.179219.134...@x40g2000prg.googlegroups. com, "sss.z...@gmail.com"
Quote:
<sss.z...@gmail.comwrote: Quote:
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.
Quote: Quote:
DWORD dwLen = 256;
CHAR* szUserName = new CHAR[dwLen]; //if GetUserName parameter is
LPWSTR, using TCHAR.
if (GetUserName(szUserName, &dwLen) {
...
} else {
...
}
Quote:
DWORD dwLen = 256;
std::string userName( dwLen, '\0' );
if (GetUserName(userName.data(), &dwLen) {
userName.resize( dwLen );
...
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:james.kanze@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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|