如何使用VARIANT指针传递字符串给客户
编号:QA003947
建立日期: 2001年3月1日 最后修改日期:2001年3月1日
所属类别:
Mario Geng:
I'm building an ActiveX component that using a VARIANT point to return a string to the client.
I used the codes like the following but they don't work.
STDMETHODIMP CCR::GetCRField(VARIANT *pCRNO, short LengthofFieldName,VARIANT
*pFieldName, VARIANT *pFieldValue)
{
/*
do something here
*/
pFieldValue->vt = VT_BSTR;
pFieldValue->bstrVal = A2OLE("the result");
return S_OK;
}
Why? How to resolve this problem?
回答:
the answer is:
1. A2OLE allocates memory off the stack, which is automatically freed when your method exits. You need to use SysAllocString
2. *(pFieldValue->pbstrVal) seems like a very strange way of setting the value.
I have never seen that before. Use pFieldValue->bstrVal instead (since vt == VT_BSTR, see documentation)
So, rewrite you method like this:
pFieldValue->vt = VT_BSTR;
pFieldValue->bstrVal = SysAllocString(A2OLE(rtValue));
(rtValue and &rtValue[0] is the same thing)
BTW: Why not use BSTR instead of VARIANT if you are only going to use strings as parameters? And why pointers for input values?
此问题由Mario Geng回答。
| |
|
|
| |
|
|