Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Want to assign a double value to BSTR string.
Are there any macros /functions for this
Posted

You may just represent a double value with a BSTR, for instance
C++
#include <windows.h>
#include <tchar.h>
#include <sstream>
using namespace std;

int main()
{
  wostringstream wos;
  double d = 0.57;
  wos << d;
  BSTR bstr = SysAllocString(wos.str().c_str());
  // enjoy with bstr
  // ...
  SysFreeString(bstr);
}
 
Share this answer
 
C++
#include "windows.h"
#include "comutil.h"

void your_func(BSTR bstr);

int main()
{
   _bstr_t str(_variant_t(double(123.456)));
   
   // str is castable to BSTR
   // use it like this

   your_func(str);

   // or simply
   your_func(_bstr_t(_variant_t(double(123.456))));
   
   return 0;
}
 
Share this answer
 
Why not use VarBstrFromR8[^] Windows API?

There are many other Data Type Conversion Functions[^] provided with Windows too.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Sep-11 20:03pm    
My 5.
--SA
Espen Harlinn 7-Sep-11 4:49am    
Thanks Sergey!
1、use _gcvt to convert double to char*
2、(BSTR)(CString)your char* var
 
Share this answer
 
Comments
Member 12726815 8-Mar-18 18:32pm    
is curious the sample code in solution 1.

prints me a memory address, example:
I add a cout << myvarbstr << end;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900