Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

BSTR data type Problem

Posted on 2001-09-17
8
Medium Priority
?
287 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:kesri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6487136
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...
0
 

Author Comment

by:kesri
ID: 6487162
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
0
 
LVL 32

Expert Comment

by:jhance
ID: 6487172
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.
0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 1

Expert Comment

by:TarekEslim
ID: 6487176


Why you do not use CString?

0
 

Author Comment

by:kesri
ID: 6487212
ok,if i say
rdr = new char[50];

then how do i achieve my result.

Regards
Kesri
0
 
LVL 32

Accepted Solution

by:
jhance earned 300 total points
ID: 6487229
What is:

SysAllocString(L rdr);

the "L" doing here?  "L" is a specifier for declaring UNICODE string constants and "rdr" is NOT a string constant.  So you have an illegal statement here.

SysAllocString() takes an OLECHAR * as input so you need to convert rdr, which is a char *.

Since you are asking this in the MFC topic area, I'll have to assume you are using MFC.  Include the AFXPRIV.H header and then you can use the MFC/ATL conversion macros like:

void myfunction(char * rdr)
{
USES_CONVERSION;

BSTR x = SysAllocString(A2OLE(rdr));
}

Or similar.  If you don't want to use this method, use the Windows API MultiByteToWideChar() function.
0
 
LVL 4

Expert Comment

by:AmitAgarwal
ID: 6488112
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
0
 

Author Comment

by:kesri
ID: 6489487
Thanks jhance

It worked!

Regards
Kesri
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question