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

x
?
Solved

BSTR to Char *

Posted on 2000-03-12
4
Medium Priority
?
777 Views
Last Modified: 2013-11-25
Actually this is related to ATL. I have a dll( say a.dll) having a function (say f)which has an input parameter char * . I want to call this function from an ATL object's( say obj) method( say m)having a bstr as its input parameter. I am passing a bstr from the m's method as:(pseudocodes in VB)
dim db as new obj
dim str as string
db.m(str)

This method will load the dll a,
 and call the function f, and pass the parameter of method m, "str" , to f.

My question is , will there be any problem as BSTR and char * are different datatypes? If there is problem, what should be done for explicit conversion of cast?
0
Comment
Question by:Subhasundar
[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
  • 3
4 Comments
 

Accepted Solution

by:
kishk91 earned 100 total points
ID: 2609366
Hi...
try using A2BSTR or BSTR2A macro...
very helpfull

Good luck
kishk91
0
 

Author Comment

by:Subhasundar
ID: 2610947
Hi Kishk91,
I couldn't find these macros in MSDN. Can you please tell me how they are defined; ie the exact definition  of these macros?
Subha
0
 

Expert Comment

by:kishk91
ID: 2611284
Hi..
this is from msdn:
(btw do you use vc6 or 5??)


String Conversion Macros
The string conversion macros discussed here are valid for both ATL and MFC. For more information on MFC string conversion, see TN059: Using MFC MBCS/Unicode Conversion Macros and MFC Macros and Globals.

The syntax of the string-conversion macros is:

MACRONAME( string_address )

For example:

A2W(lpa)

In the macro names, the source string type is on the left (for example, A) and the destination string type is on the right (for example, W). A stands for LPSTR, OLE stands for LPOLESTR, T stands for LPTSTR, and W stands for LPWSTR.

Thus, A2W converts an LPSTR to an LPWSTR, OLE2T converts an LPOLESTR to an LPTSTR, and so on.

The destination string is created using _alloca, except when the destination type is BSTR. Using _alloca allocates memory off the stack, so that when your function returns, it is automatically cleaned up.

If there is a C in the macro name, the macro converts to a const string. For example, W2CA converts an LPWSTR to an LPCSTR.

Note   When using an ATL string conversion macro, specify the USES_CONVERSION macro at the beginning of your function in order to avoid compiler errors. For example:

void func( LPSTR lpsz )
{
   USES_CONVERSION;
   ...
   LPWSTR x = A2W(lpsz)
   // Do something with x
   ...
}

The behavior of the ATL string conversion macros depends on the compiler directive in effect, if any. If the source and destination types are the same, no conversion takes place. Compiler directives change T and OLE as follows:

Compiler directive in effect T becomes OLE becomes
none A W
_UNICODE W W
OLE2ANSI A A
_UNICODE and OLE2ANSI W A


The following table lists the ATL string conversion macros.

ATL String Conversion Macros

A2BSTR OLE2A T2A W2A
A2COLE OLE2BSTR T2BSTR W2BSTR
A2CT OLE2CA T2CA W2CA
A2CW OLE2CT T2COLE W2COLE
A2OLE OLE2CW T2CW W2CT
A2T OLE2T T2OLE W2OLE
A2W OLE2W T2W W2T

0
 

Expert Comment

by:kishk91
ID: 2622420
Hi..
you still there????
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

722 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