How to pass var length array of string to event in VB 
Author Message
 How to pass var length array of string to event in VB

I want to fire event in Visual Basic and pass varlen array of strings to it.
I added event to interface like this :
  [id(3)] HRESULT SendStrings([in]VARIANT a);
Generated proxy and Fire_SendStrings looks like
 HRESULT Fire_SendStrings(VARIANT a)
when I compile i got some errors that 'a' is converted from struct variant *
into bool
if i leave all like as it is , I get bool value into visual basic event, but
if i fix this compile error by removing unreferencing of 'a' visual basic
crashes on trying to access any member ...
I have searched msdn and there are few articles about how to get and return
array of strings ,,, but not how to pass it to event.

--
Best regards,
Andrey Koubychev



Mon, 23 Aug 2004 22:39:33 GMT  
 How to pass var length array of string to event in VB
Hi Andrey,


Quote:
> I want to fire event in visual basic and pass varlen array of strings to
it.
> I added event to interface like this :
>   [id(3)] HRESULT SendStrings([in]VARIANT a);
> Generated proxy and Fire_SendStrings looks like
>  HRESULT Fire_SendStrings(VARIANT a)
> when I compile i got some errors that 'a' is converted from struct variant
*
> into bool
> if i leave all like as it is , I get bool value into visual basic event,
but
> if i fix this compile error by removing unreferencing of 'a' visual basic
> crashes on trying to access any member ...
> I have searched msdn and there are few articles about how to get and
return
> array of strings ,,, but not how to pass it to event.

Could you show the generated proxy and how you call Fire_SendStrings - I
seem to remember that I found some error in a generated proxy in an older
project, too.

Bye,
Sven



Mon, 23 Aug 2004 22:59:45 GMT  
 How to pass var length array of string to event in VB
Hi Sven,

I have seen this problem in proxy , this bug is described in MSN , i fixed
it accordingly to ms requirements.
Another articlle Q297699 Q290611 about failing posting safearrays on COM+ ,
but they it was fixed already in W2K.
My problem looks so, that when i fire event with VARIANT holding SAFEARRAY,
VB sees its size and type or array correctly (in watch). But doest show
elements values. Doesnt matter is its string or longs.
When i specify SAFEARRAY in .IDL , all looks very good and even VB generates
event that looks like
a_MyEvent (ByVal a() as String)  (array of strings) but when you try to run
it , VB says that he doesnt support this type of parameters. Very {*filter*}
teasing. :)
--
Best regards,
Andrey Koubychev



Tue, 24 Aug 2004 20:36:12 GMT  
 How to pass var length array of string to event in VB
Check the KB article, Q264985:
BUG: ATL Connection Point Wizard-Generated Code for Event
with [out]VARIANT* or [out]long* Argument Gives C4305
Warning

The bool conversion is an error in wizard-generated code.
Fix the wizard-generated code in Fire_...

VariantClear(&varResult);
pvars[0] = &a;
DISPPARAMS disp = { pvars, NULL, 1, 0 };
pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &disp, &varResult, NULL, NULL);

The offending line is "pVars[0] = &a".

If you read the documentation of CComVariant, you will see
that there is not a constructor of CComVariant that
accepts a VARIANT*. The compiler does some strange
gyrations that end trying to invoke the constructor that
takes a bool...

You must instead declare a VARIANT, not a CComVariant, and
set it with the correct values for invoking the function.

// CComVariant* pVars = new...
VARIANT Var;
...
// pVars[0] = ...
Var.vt = VT_VARIANT | VT_BYREF;
Var.pvarVal = &a;
...
// DISPPARAMS disp = { pvars, ...
DISPPARAMS disp = { &Var, NULL, 1, 0 };

Quote:
>-----Original Message-----
>I want to fire event in visual basic and pass varlen

array of strings to it.
Quote:
>I added event to interface like this :
>  [id(3)] HRESULT SendStrings([in]VARIANT a);
>Generated proxy and Fire_SendStrings looks like
> HRESULT Fire_SendStrings(VARIANT a)
>when I compile i got some errors that 'a' is converted

from struct variant *
Quote:
>into bool
>if i leave all like as it is , I get bool value into

visual basic event, but
Quote:
>if i fix this compile error by removing unreferencing
of 'a' visual basic
>crashes on trying to access any member ...
>I have searched msdn and there are few articles about how
to get and return
>array of strings ,,, but not how to pass it to event.

>--
>Best regards,
>Andrey Koubychev

>.



Wed, 25 Aug 2004 04:22:12 GMT  
 How to pass var length array of string to event in VB
Thanks to everyone,

After 3 days of trying , i finally did it :) There are dozens post in news
groups about this topic, I think i will do
some simple sample and put it on web :)
The problem was not in bad proxy generator (this is well-known problem) but
also that I thought that something wrong with my VB :) VB doesnt work with
SAFEARRAY(BSTR) OLE type. However in 50% of answers in newsgroup gives such
advice :)
I also tryed to pass SAFEARRAY containing variants in Variant. This didnt
work also, VB shows elements incorrectly.
Ufff, so much time spent on it ...

--
Best regards,
Andrey Koubychev



Fri, 27 Aug 2004 19:05:20 GMT  
 How to pass var length array of string to event in VB

Quote:
> The problem was not in bad proxy generator (this is well-known
problem) but
> also that I thought that something wrong with my VB :) VB doesnt work
with
> SAFEARRAY(BSTR) OLE type.

Does too. Just made a very simple object with one method and one event.
Here are relevant parts:

// .IDL

 interface ITestObj : IDispatch
 {
  [id(1), helpstring("method Fire")] HRESULT Fire([in] SAFEARRAY(BSTR)
*pSA);
 };

 dispinterface _ITestObjEvents
 {
  properties:
  methods:
  [id(1), helpstring("method OnFire")] HRESULT OnFire([in]
SAFEARRAY(BSTR) *pSA);
 };

// .CPP for the object

STDMETHODIMP CTestObj::Fire(SAFEARRAY **pSA)
{
 Fire_OnFire(pSA);
 return S_OK;

Quote:
}

// .CPP for the proxy - manually fixed bad wizard code

 HRESULT Fire_OnFire(SAFEARRAY * * pSA)
 {
  CComVariant varResult;
  T* pT = static_cast<T*>(this);
  int nConnectionIndex;
  CComVariant* pvars = new CComVariant[1];
  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)
   {
    VariantClear(&varResult);
    V_VT(&pvars[0]) = VT_BSTR | VT_ARRAY;
    V_ARRAY(&pvars[0]) = *pSA;
    DISPPARAMS disp = { pvars, NULL, 1, 0 };
    pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
   }
  }
  V_VT(&pvars[0]) = VT_EMPTY;
  delete[] pvars;
  return varResult.scode;

 }

// VB code

Private WithEvents t As TestObj

Private Sub Command1_Click()
Dim A() As String
ReDim A(0 To 2)
A(0) = "a"
A(1) = "b"
A(2) = "c"
If t Is Nothing Then
    Set t = New TestObj
End If
t.Fire A
End Sub

Private Sub t_OnFire(pSA() As String)
MsgBox pSA(0) & " " & pSA(1) & " " & pSA(2)
End Sub

Shows a message box with "a b c", as expected.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Sat, 28 Aug 2004 01:28:56 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How To pass 2D String array to VB from VC++ Using Safe array

2. C++ Event needs string arg that can be passed to VB

3. Ax Events passing Strings to VB Container

4. With C99 var length arrays, why linked lists?

5. Passing string Arrays between VB and VC

6. function parameters to pass a VB string array to a C++ DLL

7. passing Array of Strings between a C/C++ DLL and VB

8. passing (string) arrays from VB to C-DLL and vice versa

9. Passing array of Strings between VB and VC

10. How String Array passed to VB ActiveX Control

11. passing string array from VB to C++ DLL

12. How to pass a string array from VB to VC

 

 
Powered by phpBB® Forum Software