Link to home
Start Free TrialLog in
Avatar of protugy
protugy

asked on

convert CString to int and passing BSTR to VB

Hi there,

Does anyone know how to convert from CString to short ( integer )?

Also, I have to pass a BSTR string from VC dll to VB. Does any know the syntax?

extern "C" BSTR __stdcall GetVitalSign (int iteration)
{
     iteration--;
     sVital_sign_info VS;
     VS = vitalSigns.GetAt ( iteration );
     CString code = ChangeDoubleToString(VS.code);
     CString info = VS.name + "," + code;
     LPCTSTR txt = _T (info);
     int iLen = _tcslen(txt);
         
     #ifdef _UNICODE      
          return SysAllocStringLen(txt, iLen);
    #else
          return SysAllocStringByteLen (txt, iLen );      
     #endif
}

Does this function work? or I have to pass by reference?

Thank you.

Avatar of scientist060700
scientist060700

use atoi function.
Since CString is array of character it is easy.
Here is example:
char string[] = "123";
        int i = atoi(string);
        int j = atoi("456");
I am proposing couple of books for reference, you can try them for more details. The fist book has section called Strings in idex click it to see more.
http://www.eskimo.com/~scs/cclass/notes/top.html
http://www.strath.ac.uk/IT/Docs/Ccourse/

Directly to String page with example/explanation.
http://www.eskimo.com/~scs/cclass/notes/sx8.html

Best regards,
--scientist.
1) Concerning CString conversion you might need to use operator LPCTSTR  which directly accesses characters stored in a CString object as a C-style string.

2) Concerning BSTR
Maybe you need to use the following functions from Oleauto.h and link Oleauto32.lib

BSTR and vector conversion

2A)VectorFromBstr
Returns a vector, assigning each character in the BSTR to an element of the vector.

HRESULT VectorFromBstr(
  BSTR  bstr,                
  SAFEARRAY FAR* FAR*  ppsa  
);


2B)BstrFromVector

Returns a BSTR, assigning each element of the vector to a character in the BSTR.

HRESULT BstrFromVector(
  SAFEARRAY FAR*  psa,  
  BSTR FAR*  pbstr      
);
CString str;
int i;

// Convert to int
str.Format(%d, &i);

// Convert to BSTR
BSTR bstr = str.AllocSysString();
Avatar of protugy

ASKER

Actually, I would like to know if there is any way to pass a BSTR to VB program from VC dll.

Alexo, does str.format ( %d, &i ) really work?

thanks.
ASKER CERTIFIED SOLUTION
Avatar of alexo
alexo
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry guys, I was mistaken.  Format() does the conversion other way.

Since I got the points wrongly, I offer the following:
https://www.experts-exchange.com/questions/20402835/Points-for-scientist.html (Points for scientist)
https://www.experts-exchange.com/questions/20402837/Points-for-Mafalda.html (Points for Mafalda)
i don't think either scientist or mafalda answered the question fully.  i don't believe you can convert a cstring to a string directly as far as i know.  so using LPCTSTR wouldn't enable you to use atoi directly either.  does anyone know an easy way to convert a cstring to a string?  cstring::getbuffer returns an LPTSTR.  i guess the easiest way would be to use GetAt and read chars into an array one at a time. ... ?
Avatar of protugy

ASKER

Actually, atoi did work for my function.

My problem now is that whenever I use this function below, it works for executable file created in VB. However, it doesn't work in the VB compiler. In other words, it would crash when it compiled from VB when it read the string.
The error message is something like the memory couldn't be read and cause an assertion problem.

extern "C" void __stdcall GetVitalSign (int iteration, BSTR &info )
{
    iteration--;
    sVital_sign_info VS;
    VS = vitalSigns.GetAt ( iteration );
    CString code = ChangeDoubleToString(VS.code);
    CString info = VS.name + "," + code;
    LPCTSTR txt = _T (info);
    int iLen = _tcslen(txt);
         
    #ifdef _UNICODE      
         info =  SysAllocStringLen(txt, iLen);
   #else
         info =  SysAllocStringByteLen (txt, iLen );      
    #endif
}


What I did is to pass by reference to VC from VB.
I need help immediately because we need to debug the code and test the code using Rational Tools.

So, can anybody help me???

thanks.