Passing variants in Fire_xxx methods
-
Hi, I am working on this ATL dll, which has one object that implements connection points. I added some methods in the event interface that take VARIANT as parameters, so that I may receive the variants in events when I use the same from VB. In IDL file it is like //////////////////////////////////////////////////// [id(3), helpstring("method SampleRecordPkt")] HRESULT SampleRecordPkt([in] VARIANT vBytes); //////////////////////////////////////////////////// Now, I am trying to fire this from my code as follows - (All the hard coded stuff I did for debug only.) //////////////////////////////////////////////////// BYTE bytes[] = {5, 123, 9, 0, 5, 0, 0, 191, 157}; COleSafeArray saBytes1; saBytes1.CreateOneDim(VT_UI1,sizeof(bytes)); for(long i = 0; i < sizeof(bytes);i++) { BYTE val = bytes[i]; saBytes1.PutElement(&i,&val); } file://fire off the appropriate event VARIANT var = saBytes1.Detach(); Fire_SampleRecordPkt(var); ///////////////////////////////////////////////////// However, inside "Fire_SampleRecordPkt(var)", the application crashes. The crash occurs at the line given below (wizard generated code, when "implement connection point" is chosen.) //////////////////////////////////////////////////// DISPPARAMS disp = { pvars, NULL, 1, 0 }; pDispatch->Invoke(0x3, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL); //////////////////////////////////////////////////// Can some body help me identify where I am wrong? Any help is appreciated. Thank you aby
-
Hi, I am working on this ATL dll, which has one object that implements connection points. I added some methods in the event interface that take VARIANT as parameters, so that I may receive the variants in events when I use the same from VB. In IDL file it is like //////////////////////////////////////////////////// [id(3), helpstring("method SampleRecordPkt")] HRESULT SampleRecordPkt([in] VARIANT vBytes); //////////////////////////////////////////////////// Now, I am trying to fire this from my code as follows - (All the hard coded stuff I did for debug only.) //////////////////////////////////////////////////// BYTE bytes[] = {5, 123, 9, 0, 5, 0, 0, 191, 157}; COleSafeArray saBytes1; saBytes1.CreateOneDim(VT_UI1,sizeof(bytes)); for(long i = 0; i < sizeof(bytes);i++) { BYTE val = bytes[i]; saBytes1.PutElement(&i,&val); } file://fire off the appropriate event VARIANT var = saBytes1.Detach(); Fire_SampleRecordPkt(var); ///////////////////////////////////////////////////// However, inside "Fire_SampleRecordPkt(var)", the application crashes. The crash occurs at the line given below (wizard generated code, when "implement connection point" is chosen.) //////////////////////////////////////////////////// DISPPARAMS disp = { pvars, NULL, 1, 0 }; pDispatch->Invoke(0x3, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL); //////////////////////////////////////////////////// Can some body help me identify where I am wrong? Any help is appreciated. Thank you aby
Hello Aby, I have some clues as to what might have gine wrong but I need some more information from you first : Is the events proxy code implemented somewhat like the following ? : HRESULT Fire_SampleRecordPkt(VARIANT vBytes) { CComVariant varResult; T* pT = static_cast(this); int nConnectionIndex; CComVariant* pvars = new CComVariant[1]; int nConnections = m_vec.GetSize(); for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++) { pT->Lock(); CComPtr sp = m_vec.GetAt(nConnectionIndex); pT->Unlock(); IDispatch* pDispatch = reinterpret_cast(sp.p); if (pDispatch != NULL) { VariantClear(&varResult); pvars[0] = &vBytes; // <-- important. DISPPARAMS disp = { pvars, NULL, 1, 0 }; pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL); } } delete[] pvars; return varResult.scode; } Please note the line in which I added the comment "// <-- important.". Please let me know because I suspect your problem may be a case of wrong code generated by the wizard. Please revert, Bio.