Solved

BSTR data type Problem

Posted on 2001-09-17
8
271 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
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
 
LVL 1

Expert Comment

by:TarekEslim
ID: 6487176


Why you do not use CString?

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 83
ffmpeg - "rtsp://...... Operation not permitted" 4 68
bigDiff challenge 17 75
sumHeights  challenge 17 61
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now