Link to home
Start Free TrialLog in
Avatar of kesri
kesri

asked on

BSTR data type Problem

I have the following problem:

char *rdr;
rdr=new char[];
strcpy(rdr,"Name is Kesri");


BSTR     bstrName     = SysAllocString(L rdr);

The value of rdr changes during runtime i.e. it is a input to the program. I want rdr to be passed to SysAllocString function as a argument but it doesn't work.
Please help.

Regards
Kesri
Avatar of jhance
jhance

What you have here:

char *rdr;
rdr=new char[];
strcpy(rdr,"Name is Kesri");

is in error.  The line:

rdr = new char[];

allocates space for a char[], not a char[14] as needed.

After that, I don't understand the rest of your point.

Please clarify...
Avatar of kesri

ASKER

the string above "Name is Kesri" is just a example, the value of rdr could be anything and the value is got during runtime.

Regards
Kesri
You've missed my point.  The line:

rdr = new char[];

is NOT a suitable allocation for a string.  It allocates ONE char[], and is roughly equivalent to:

rdr = new char *;

If you store anything here that is larger than the size of a "char *" (which is 4 bytes under WIN32) you will have a problem.


Why you do not use CString?

Avatar of kesri

ASKER

ok,if i say
rdr = new char[50];

then how do i achieve my result.

Regards
Kesri
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hey you can use a class _bstr_t which takes care of allocating and deallocating of memory itself and it is very easy in deed
amit
Avatar of kesri

ASKER

Thanks jhance

It worked!

Regards
Kesri