如何将BSTR数据类型转换到_bstr_t数据类型?
Julia 2000-09-14 04:03:00
The program of ATL COM can run succeed. But at last it shows that the executive file of this COM is runtime error and abnormal programer
termination. Then I notice that the parameter of the function shows errors CXX0030 and CXX0017. Why? The code is the following:
.........
HRESULT hr = S_OK;
long id = 123;
BSTR pwd;
//initial a BSTR varialbe
wchar_t * sss = L"abc";
SysReAllocString(&pwd,sss);
_ConnectionPtr pObjADO_Connection = NULL;
// Create ADO connection
hr = pObjADO_Connection.CreateInstance(__uuidof(Connection));
hr = pObjADO_Connection->Open(
_bstr_t L"Provider=Microsoft.Jet.OLEDB.3.51;
Data Source=E:\\ZYHU\\Test_ado\\dbCustomerPasswordC.mdb;"),
_bstr_t(L""),
_bstr_t(L""),
adModeUnknown);
// String I/O stream to write SQL statement
basic_stringstream<wchar_t> strSQLStatement;
// Write the SQL statement into a string
// Cast variant dates to bstr_t so they will be converted into a string
strSQLStatement << L"INSERT INTO Customers (CustomerID, Password) VALUES ("
<< id << L",' "
<< pwd << L"')" << ends;
bstr_t strSQLStmt = strSQLStatement.str().c_str();
_variant_t vntRecordsAffected(0L);
// Execute the SQL statement to add a record
hr = pObjADO_Connection->Execute(strSQLStmt, &vntRecordsAffected,
adOptionUnspecified);
SysFreeString(pwd);
SysFreeString(strSQLStmt);
pObjADO_Connection->Close();
.........
Besides,I want to know how I can convert a variable of the BSTR data type to a
variable of the _bstr_t data type,vice
versa. I look up the mothed in the msdn,but it doesn't work. Can you help me? Thanks.
huzhiyuan2000@sina.com