473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Jul 28 '07 #1
11 2664
"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

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

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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Senthilraja | last post by:
I am able to compile the following code and get "hello" as ouput everytime I execute it:- #include <stdio.h> int main (void) { char *str; str="hello"; puts(str); return 0;
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
35
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
0
by: Cause | last post by:
In the following code to call my website for serial registration reasons, I want to test on localhost. The code works on the internet, but this code sends me a bunch of 400 bad request errors when I...
41
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
9
by: weirdwoolly | last post by:
Hopefully someone will be able to help. I have written a stored procedure in C++ called from a Java test harness to validate the graphic data types in C++ and their use. I have declared the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.