What Is the Matrix? -- Part 2

Environment: Visual Studio, All Windows platforms

Introduction

The Matrix, in its simplest form, simplifies working with arrays. This is the second of several articles that explains how to use the different features of the Matrix. In the previous article, I described how to use the basic features of the Matrix. In this article, I will explain how to use the built-in conversions to and from VARIANTs.

I would first recommend reading the main Matrix article and the first article, 'Basic Features'

The Matrix can be downloaded from CodeGuru, along with a brief overview of what it does.

The first article, 'Basic Features,' can be downloaded from here.

Converting to and from VARIANTs

The following code shows how to create a basic two-dimensional array

CMatrix<long> matrix;
matrix.cell(0,0)=100;
matrix.cell(1,0)=200;
matrix.cell(0,1)=300;
matrix.cell(1,1)=400;

The matrix now looks like this...

100 200
300 400

Now, suppose you had a method in a COM object that wanted an array. The method looks like this...

HRESULT SomeMethod(VARIANT vArray)

To pass the Matrix to this method, simply pass the Matrix like so...

HRESULT hr = SomeMethod(matrix);

This works because the Matrix is implicitly converted to a VARIANT by its operator VARIANT function.

Now, let's look at the SomeMethod function and see how this can benefit from using the Matrix...

HRESULT SomeMethod(VARIANT vArray)
{
    CMatrix<_variant_t> matrix (vArray);
    long theLong=(long)m.cell(1,0) ;    // should give us 200
}

Here, the array is passed in as a VARIANT. The VARIANT contains a SAFEARRAY. We construct a Matrix from the VARIANT that was passed in. The matrix looks like this...

100 200
300 400

We now have an exact copy of the Matrix in our COM server object. Passing arrays back to the client is just as easy...

HRESULT SomeMethod(VARIANT vArray, VARIANT* pvArray)
{
  CMatrix<_variant_t> m (vArray ) ; 
  for ( ULONG l = 0 ; l < m.width( ) ; l++ )
  {
    long lNum (m.cell(l,0));
    m.cell(l,0) = lNum * 2 ;
  }

  *pvArray = m ;
}

The code above, in just a few lines of code, takes a 2D SAFEARRAY, multiplies every number in the array by 2, and returns a new SAFEARRAY.

Multilingual

Let's say you've used the Matrix in your COM object. You needn't (nor shouldn't) restrict the clients of your object to be C++ clients. The following snippet of VB passes arrays to and from a COM object that uses the Matrix...

Dim a As New Calculator

Dim moreInts(0 To 9) As Variant
Dim looper As Integer

For looper = 1 To 10
  moreInts(looper - 1) = looper
Next

Dim arr As Variant
arr = a.MultiplyItems(moreInts, 2)

Dim s As String

For looper = 0 To 9
  s = s & arr(looper, 0) & ", "
Next

MsgBox s

The Examples

The examples presented with this article constitute a COM Local Server object (Calculator) written in C++, a client application written in C++, and another client application written in VB.

This Server contains one object (Calculator) that has one method (MultiplyItems). This method takes three parameters: an input array, an output array, and a Factorial integer (the amount to multiply the numbers by). The C++ client simply fills an array with numbers and outputs the result from the MultiplyItems method on the server. The Visual Basic client does the same thing. One thing to note in the VB examples is that the arrays are 0 based (rather than 1 based).

Conclusion

Hopefully, this article has shown you how to use the Matrix to create and pass arrays. Please feel free to leave comments and feedback. In my next article, I will explain how to use stream operators with the Matrix.

Downloads

Download demo project - 34 Kb
Download source - 17 Kb


Comments

  • A bug in overloaded operator=(VARIANT& v)

    Posted by yangzhisui on 06/01/2004 04:55pm

    The indices used in SafeArrayGetElement are wrong. It should be 
    
       ix[0] = c+xLow ;		ix[1] = r+yLow ;
    
    Also, I don't know why I got an element with type VT_R8 while the actural type is VT_I4. Could someone please help me?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds