Solved

BSTR data type Problem

Posted on 2001-09-17
8
284 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

617 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