[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

converting a BSTR to a double.

Posted on 2004-11-08
4
Medium Priority
?
2,010 Views
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)
{
  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
0
Comment
Question by:axnst2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 12523819
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
 
LVL 19

Expert Comment

by:mrwad99
ID: 12523827
Sorry,

>> csMyString(MyBStr);

should be

CString csMyString(MyBStr);
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 12523888
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
 
LVL 16

Expert Comment

by:nonubik
ID: 12525924
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

865 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