Solved

BSTR data type Problem

Posted on 2001-09-17
8
276 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 185
lucky13 challenge 11 145
wordappend challenge 8 179
Path of Workbook 3 78
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

777 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