Solved

BSTR data type Problem

Posted on 2001-09-17
8
280 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 218
Expand data scrubbing tool 13 35
Capture logon name 13 87
I could not set window to top 4 20
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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