converting a BSTR to a double.

Hi all.  Here is my problem.

I am passing a string from VB into a C++ dll as a BSTR and now I have to pass it along as a double into a vendor's c++ app.  The vendor's function won't execute properly.  I was told that I might not be passing it the double right.  The vendor tested his version of the client and it worked fine.  This is what I did to convert my BSTR:

__declspec(dllexport) int _stdcall ChangeOrientation(BSTR x1Cordinate, BSTR y1Cordinate, BSTR x2Cordinate, BSTR y2Cordinate)
{
  AFX_MANAGE_STATE(AfxGetStaticModuleState());
  LASER_PACKET                      packet;
  PACKET_CHANGE_ITEM_ORIENTATION    *itemP;
  USES_CONVERSION;
  int returnValue;
  int dumbyInt;

   //Check to make sure we are still connected
      dumbyInt=forceLoop(1000,1);
      if (connectedG){
            connectedG=false;
            packet.timestamp = getTimeDouble();
            packet.type      = LaserPacketChangeItemOrientation;
            itemP = (PACKET_CHANGE_ITEM_ORIENTATION*)packet.dataP;
            itemP->x1 = atoi((char*)x1Cordinate);
            itemP->x2 = atoi((char*)x2Cordinate);
            itemP->y1 = atoi((char*)y1Cordinate);
            itemP->y2 = atoi((char*)y1Cordinate);
            tcpClientG.sendData((char*)&packet, sizeof(packet)-(MAX_COMMAND_SIZE-sizeof(PACKET_CHANGE_ITEM_ORIENTATION)));
            currentCommandStatusG = LaserCommandPending;
            returnValue=forceLoop(1000,0);
      }
      else{
            returnValue=3;
      }
      return returnValue;
}

I think this should work fine but maybe I'm wrong.  PLease let me know what you guys think.

Thanks,
axnst2
axnst2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrwad99Commented:
Hmm, this may not be the best solution, but in order to go from a BSTR to a double you could:

1) Convert BSTR to CString

csMyString(MyBStr);

2) Convert the CString to a double

double dValue = atof(csMyString);

HTH
0
mrwad99Commented:
Sorry,

>> csMyString(MyBStr);

should be

CString csMyString(MyBStr);
0
jkrCommented:
I'd use

#include <comdef.h>

_bstr_t bsX1 (x1Cordinate);
_bstr_t bsY1 (y1Cordinate);
_bstr_t bsX2 (x2Cordinate);
_bstr_t bsY2 (y2Cordinate);

//...

         itemP->x1 = atoi((char*)bsX1 );
         itemP->x2 = atoi((char*)bsY1 );
         itemP->y1 = atoi((char*)bsX2 );
         itemP->y2 = atoi((char*)bsY2 );

A _bstr_t object encapsulates the BSTR data type and provides operators for conversion such as '_bstr_toperator char*( ) const throw( _com_error );'. Once you have a 'char*', you can call 'atoi()' or any other conversion function.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nonubikCommented:
You can use the VARIANT and make a conversion function

VARIANT var;

VariantInit(&var);
var.vt = VT_BSTR;
var.bstrVal = y1Cordinate;
VariantChangeType(&var, &var, 0, VT_R8);
itemP->y1 = vt.dblVal;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.