converting a BSTR to a double.

Posted on 2004-11-08
Last Modified: 2008-02-01
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)
  LASER_PACKET                      packet;
  int returnValue;
  int dumbyInt;

   //Check to make sure we are still connected
      if (connectedG){
            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;
      return returnValue;

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

Question by:axnst2
    LVL 19

    Expert Comment

    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


    2) Convert the CString to a double

    double dValue = atof(csMyString);

    LVL 19

    Expert Comment


    >> csMyString(MyBStr);

    should be

    CString csMyString(MyBStr);
    LVL 86

    Accepted Solution

    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.

    LVL 16

    Expert Comment

    You can use the VARIANT and make a conversion function

    VARIANT var;

    var.vt = VT_BSTR;
    var.bstrVal = y1Cordinate;
    VariantChangeType(&var, &var, 0, VT_R8);
    itemP->y1 = vt.dblVal;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now