2008-10-03 87 views

回答

6

谷歌建议VarI4FromStr

HRESULT VarI4FromStr(
    _In_ LPCOLESTR strIn, 
    _In_ LCID lcid, 
    _In_ ULONG dwFlags, 
    _Out_ LONG *plOut 
); 
0
BSTR s = SysAllocString(L"42"); 
int i = _wtoi(s); 
2

您应该使用:: VarI4FromStr(...)。

1

您应该能够使用boost :: lexical_cast的<>

#include <boost/lexical_cast.hpp> 
#include <iostream> 

int main() 
{ 
    wchar_t  plop[] = L"123"; 
    int value = boost::lexical_cast<int>(plop); 

    std::cout << value << std::endl; 
} 

很酷的事情是,lexical_cast的<>
它将为可通过流传递和类型安全的任何类型的工作

1

这是我用来解析字符串中的值的方法。它类似于Boost's lexical cast

std::wistringstream iss(mybstr); // Should convert from bstr to wchar_t* for the constructor 
iss >> myint;      // Puts the converted string value in to myint 
if(iss.bad() || iss.fail()) 
{ 
    // conversion failed 
} 
0

您应该像其他人一样使用VarI4FromStrBSTR不是wchar_t*,因为它们的差异在NULL semanticsSysStringLen(NULL)没问题,但wcslen(NULL)不是)。