ComponentOne VSView Reporting Edition
Using VSReport8 in VC++

Using VSReport8 in Visual C++ is not very different form using it in Visual Basic, thanks to the great COM support built into Visual C++ 6.0.

Before Visual Studio 6, the only practical way to use COM objects in Visual C++ was through MFC. You would insert a COM object into your project, and MFC would create wrapper classes that provided access to all methods, properties, and events. This method had a couple of drawbacks, however. The wrappers created by MFC were automation-based, which is not terribly efficient. They also relied on MFC classes such as CString, and provided no support for optional parameters. Finally, the wrappers included all methods, properties, and events, but omitted custom types and enumerations.

Visual C++ version 6 introduced native COM support via classes that encapsulate COM pointers, strings, variants, and more. Most importantly, the new #import statement can be used to build powerful and efficient wrapper classes that let you write C++ code that is as readable as Visual Basic code.

To use VSReport8 in your C++ projects, add the following #import statements to your stdafx.h file (make sure each #import statement is on a single line, and remember to include the full path to the OCX files):

Example Title
Copy Code
#import  "VSRpt8.ocx"    no_namespace

#import  "VSPrinter8.ocx"  no_namespace exclude("PictureAlignSettings", _

"BorderStyleSettings")

These statements will create wrappers for the VSReport8 and VSPrinter8 controls (the wrapper classes are saved into TLH and TLI files in your current build directory). Once that is done, you can write code such as:

Example Title
Copy Code
IVSReportPtr spReport; // declare a pointer to a VSReport8 control

spReport.CreateInstance(__uuidof(VSReport)); // create control

spReport->Load(szFileIn, szReport); // load report definition

spReport->RenderToFile(szFileOut, vsrHTMLPaged); // render report

The #import statement handles the control's entire object model, so you also get classes that encapsulate the DataSource and Layout objects, Groups, Sections, Fields, etc. For example, the following code enumerates all fields in a report:

Example Title
Copy Code
// get field collection

IFieldCollectionPtr spFields = spReport->Fields;

 

// enumerate all fields

long iField, lFieldCnt = spFields->Count;

for (iField = 0; iField < lFieldCnt; iField++)

{

  // get ith field

  IFieldPtr spField = spFields->GetItem(iField);

 

  // show the field's name

  _bstr_t strName = spField->Name;

  strName += "\n";

  OutputDebugString((char*)strName);

}

Using the #import statement does not mean you cannot use MFC and the AppWizard to create your controls and connect events, etc. Both approaches can coexist. If you have an instance of a control that was created by MFC using the MFC wrappers, you can easily convert it into the #import-flavor wrappers using the following construct:

Example Title
Copy Code
IVSReportPtr spReport = mfcReportControl.GetControlUnknown();

The IVSReportPtr class will take care of querying the new value for the appropriate interface and will do the right thing.

One final note on using the #import statement is related to error-handling. The high-level wrappers have built-in error handling. If something goes wrong they will throw an exception, and if you don't handle it your program will die. So, to be safe, you should always use the high-level wrappers in try/catch blocks. For example:

Example Title
Copy Code
try {

  spReport->Load(strFileIn, strReportName);

  spReport->Render(spPrinter);

} catch (...) {

  MessageBox("Failed to render the report ");

}

Alternatively, you can use the lower-level wrappers, which return HRESULTS like traditional COM. The lower-level functions have a "raw_" prefix and can be used as follows:

Example Title
Copy Code
HRESULT hr = spReport->raw_Load(strFileIn, strReportName);

if (!FAILED(hr))

  hr = spReport->raw_Render(spPrinter);

if (FAILED(hr))

  MessageBox("Failed to render the report ");

This information should be enough to help you get started. If you are unsure about the syntax for a particular method or property, start by looking at the TLH and TLI files generated by the #import statement, or check the VC++ samples on the distribution CD or on the ComponentOne HelpCentral Web site (http://helpcentral.componentone.com/ProductResources.aspx?View=Samples).

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback