The Wayback Machine - https://web.archive.org/web/20080601040837/http://www.codeguru.com:80/cpp/com-tech/activex/misc/article.php/c5539/

CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
Find Software
Logo Design
Corporate Awards
GPS
KVM over IP
KVM Switches
Hurricane Shutters
PDA Phones & Cases
Imprinted Promotions
Online Education
Web Design
Promotional Pens
Shop
Promos and Premiums


RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Visual C++ / C++ >> COM-based Technologies >> ActiveX Programming >> Misc.

Access the RIA Development Resource Center: Get the latest news, insights, tips & resources to help you get up to speed in this emerging & exciting software development category.

Taking the Complexity Out of Creating COM Arrays'—The Matrix Class Utility
Rating: none

Steve Dunn (view profile)
January 29, 2003

Environment: Visual Studio 6 with latest service pack, Visual Studio .NET

What Is Matrix?


(continued)

Click Here

Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.

Matrix is a templatised utility class with many uses. One of it best features is it takes away the complexity of creating COM arrays (SAFEARRAYS). Matrix was initially designed as a 2D array helper class. It later became far more useful when changed to a template class to store different types. A Matrix object can be treated as if it were a VARIANT data type. Anything that can be stored in a VARIANT, including arrays, can be stored in a Matrix. Anything you previously marshalled between apartment/process/machine boundaries can use Matrix.

I will begin describing Matrix in its simplest form and then progressing to more advanced and useful features.

What Can Be Stored in a Matrix?

In its simplest form, the Matrix can be used like so:

CMatrix<long>
matrixLong ;
matrixLong.cell(1,1)=111 ;
matrixLong.cell(2,2)=222 ;

printf("Contents of cell 1,1 is %d and cell 2,2 is %d",
        matrixLong.cell(1,1),
        matrixLong.cell(2,2));

This outputs:

Contents of cell 1,1 is 111 and cell 2,2 is 222

Another example uses a user-defined object:

class CMyObject
{
    long  m_lNumber ;

    public:
            CMyObject( long l ) { m_lNumber = l; }
            long getNumber( )const { return m_lNumber; }
};

CMatrix<CMyObject*> m ;
for ( int i = 0 ; i < 100 ; i++ )
    m.cell(i,0)=new CMyObject( i*2 ) ;

// The Matrix above now contains 100 pointers to CMyObject objects.
// Each cell can now be treated as if it's a CMyObject pointer.
// For example:
long    l = m.cell(5,0)->getNumber( );
printf("Contents of cell 5,5 is %d\n", l ) ;

The example above creates a Matrix and fills it with CMyObjects. It then retrieves a cell at position 5,0. This cell contains a .CMyObject* (or whatever else in specified in the template declaration).

What Else Does Matrix Do?

One of the most interesting features of Matrix is it's ability to hide the details of creating and using SAFEARRAYs. The following example creates a two-dimensional array and passes it to a function in a VARIANT form. The function then creates a Matrix from the VARIANT.

// The function below expects a VARIANT. Since VARIANTS are
// marshaled automatically, the function below can be in a
// different project or module, or even on a different machine
void giveMeAVariant(VARIANT v )
{
  CMatrix<_variant_t> t(v);
  printf("matrix is %d wide & %d high\n", t.width( ),
                                          t.height( ) ) ;
  printf("Contents of cell 5,5 is %d", (long)t.cell(5,5) ) ;
}

CMatrix<_variant_t> m;
for ( long i = 0 ; i < 100 ; i++ )
      m.cell(i,i)=i;

giveMeAVariant( m ) ;

This outputs:

matrix is 100 wide & 100 high
Contents of cell 5,5 is 5

In this example, we have created a SAFEARRAY packaged in a VARIANT in 3 lines of code. Remember, the function .giveMeAVariant. can be in a different apartment, module, application, or even machine. Hopefully, you can see that this utility class is very useful in hiding the complexities of SAFEARRAYs as well as handling two-dimensional arrays easily.

How Do I Integrate It with My Existing Code?

The complete implementation of Matrix is surprisingly small. It is self-contained within 1 header file (matrix.h). To include the file in your project, ensure the path where you store the file is in your path look-up settings for your project. Then, simply include the header file wherever you want to use a Matrix class. Because it is simply a header file, it will inherit whatever project settings you have specified, such as warning level and UNICODE setting.

With What Compilers/Tools Can I Use It?

Because Matrix is a simple header file, it can (in theory, I haven't tried it) be used with any compiler that supports templates. I have used Matrix on Visual Studio 6 and Visual Studio .Net.

Roundup

There are many features in Matrix that make it a valuable aid in your day-to-day programming. In the near future, I will provide more examples of how to use this powerful class.

Downloads

Download demo applications - 167 Kb
Download demo source - 54 Kb
Download header file - 17 Kb

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

Intel Go Parallel Portal: Translating Multicore Power into Application Performance
Download: Intel AMT Developer's Tool Kit (DTK)
Download: Intel AMT Setup and Configuration Service (SCS)
Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.
Download: Intel AMT Software Development Kit (SDK)


RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Hmm... - Legacy CodeGuru (04/04/2003)
Nice - Legacy CodeGuru (04/03/2003)
Comments can be posted. - Legacy CodeGuru (04/01/2003)
Matrix calculation - Legacy CodeGuru (03/01/2003)
Matrix calculation - Legacy CodeGuru (03/01/2003)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES