事件接口如下:
dispinterface _ITestEvents
{
properties:
methods:
[id(1), helpstring("method TestEvent")] void TestEvent([in] long a, [in] long b, [in] BSTR Text);
};在连接点代理类中:
template <class T>
class CProxy_ITestEvents : public IConnectionPointImpl<T, &DIID__ITestEvents, CComDynamicUnkArray>
{
//Warning this class may be recreated by the wizard.
public:
VOID Fire_TestEvent()
{
T* pT = static_cast<T*>(this); int nConnectionIndex;
CComVariant* pvars = new CComVariant[3];
int nConnections = m_vec.GetSize(); for( nConnectionIndex=0; nConnectionIndex<nConnections; ++nConnectionIndex)
{
pT->Lock(); CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);
pT->Unlock();
IDispatch* pDispatch = reinterpret_cast<IDispatch*>(sp.p);
if( pDispatch!=NULL )
{
pvars[0].vt = VT_I4;
pvars[0].lVal = pInfo->IsSuccessful();
pvars[1].vt = VT_I4;
pvars[1].lVal = pInfo->ErrorCode(); //通过下面的pDispatch->Invoke(0x01, ...)回调客户的事件代码,但是当有中文字符时却无法触发客户端的事件,错误:Warning: automation argument coercion //方法1:
pvars[2] = "abbbbdd"; //不行, Warning: automation argument coercion failed.
//方法2:
pvars[2].vt = VT_BSTR;
pvars[2].bstrVal = _com_util::ConvertStringToBSTR("23456"); //可以,没有中文字符。 //方法3:
pvars[2].vt = VT_BSTR;
pvars[2].bstrVal = _com_util::ConvertStringToBSTR("哈哈"); //不可以,有中文字符。Warning: automation argument coercion failed.
DISPPARAMS disp = { pvars, NULL, 3, 0 };
pDispatch->Invoke(0x01, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, NULL, NULL, NULL);
}
}
delete [] pvars;
}
谢谢!!!
dispinterface _ITestEvents
{
properties:
methods:
[id(1), helpstring("method TestEvent")] void TestEvent([in] long a, [in] long b, [in] BSTR Text);
};在连接点代理类中:
template <class T>
class CProxy_ITestEvents : public IConnectionPointImpl<T, &DIID__ITestEvents, CComDynamicUnkArray>
{
//Warning this class may be recreated by the wizard.
public:
VOID Fire_TestEvent()
{
T* pT = static_cast<T*>(this); int nConnectionIndex;
CComVariant* pvars = new CComVariant[3];
int nConnections = m_vec.GetSize(); for( nConnectionIndex=0; nConnectionIndex<nConnections; ++nConnectionIndex)
{
pT->Lock(); CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);
pT->Unlock();
IDispatch* pDispatch = reinterpret_cast<IDispatch*>(sp.p);
if( pDispatch!=NULL )
{
pvars[0].vt = VT_I4;
pvars[0].lVal = pInfo->IsSuccessful();
pvars[1].vt = VT_I4;
pvars[1].lVal = pInfo->ErrorCode(); //通过下面的pDispatch->Invoke(0x01, ...)回调客户的事件代码,但是当有中文字符时却无法触发客户端的事件,错误:Warning: automation argument coercion //方法1:
pvars[2] = "abbbbdd"; //不行, Warning: automation argument coercion failed.
//方法2:
pvars[2].vt = VT_BSTR;
pvars[2].bstrVal = _com_util::ConvertStringToBSTR("23456"); //可以,没有中文字符。 //方法3:
pvars[2].vt = VT_BSTR;
pvars[2].bstrVal = _com_util::ConvertStringToBSTR("哈哈"); //不可以,有中文字符。Warning: automation argument coercion failed.
DISPPARAMS disp = { pvars, NULL, 3, 0 };
pDispatch->Invoke(0x01, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, NULL, NULL, NULL);
}
}
delete [] pvars;
}
谢谢!!!
解决方案 »
- VC 地图偏移,怎么解决?
- 如何解决关于socket编程中客户端界面的刷新问题?
- 如何停止消息循环
- 并口电压控制发光管发光的问题
- 高分请教关于 NtDeviceIoControlFile 的一个问题
- 关于Windows窗口ID的问题
- 关于error BK1506的问题
- CSocket如何做到真正阻塞?
- 对操作系统了解比较深入的请进:win98,瘟酒吧,多线程程序前台运行反而比后台运行慢,为什么??
- 用什么东东导出SDK dll的lib(VC中使用)?
- 在自己的类中使用Timer,TimerProc是static,则处理函数中调用类的成员函数也必须是static?在线等,马上结!
- 建立了一个群:杭州c\c++ 程序员,望杭州的c\c++ 程序员捧捧场,群号15019812
改成:pvars[2].bstrVal = SysAllocString(L"哈哈");
试一试...
问题解决了: 参数的顺序颠倒了.接口声明为:[id(1), helpstring("method TestEvent")] void TestEvent([in] long a, [in] long b, [in] BSTR Text);pvars[0] 应该对应Test
pvars[1] 应该对应b
pvars[3] 应该对应a使用CComBSTR() 或 _com_util::ConvertStringToBSTR("哈哈") 或 SysAllocString(L"哈哈") 都可以。