convert CString to int and passing BSTR to VB

protugy
protugy used Ask the Experts™
on
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.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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.

Commented:
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      
);

Commented:
CString str;
int i;

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

// Convert to BSTR
BSTR bstr = str.AllocSysString();
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Author

Commented:
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.
Commented:
>> I would like to know if there is any way to pass a BSTR to VB program from VC dll

Try here: http://www.flipcode.com/articles/article_vbdlls.shtml

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

str.format("%d", &i) - don't forget the quotes.
http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_cstring.3a3a.format.asp

Commented:
Sorry guys, I was mistaken.  Format() does the conversion other way.

Since I got the points wrongly, I offer the following:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20402835.html (Points for scientist)
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20402837.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. ... ?

Author

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial