ConfigurationTraversal.cpp Written in C++
// ConfigurationTraversal.cpp : Defines the entry point
for the console application
// Traverses the configurations in the active model
document
// Demonstrates ModelDoc2::GetConfigurationNames taking
a VARIANT
#include "stdafx.h"
// Add the path to the SolidWorks type libraries to
the "Additional Include Directories"
#import "sldworks.tlb" raw_interfaces_only,
raw_native_types, no_namespace, named_guids //
SolidWorks type library
#import "swconst.tlb" raw_interfaces_only,
raw_native_types, no_namespace, named_guids //
SolidWorks constants type library
#include "smartvars.h"
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize COM:
// Do this before using ATL smart pointers
so COM is available
CoInitialize(NULL);
// Use a block, so the smart pointers are
destructed when the scope of this block is left
{
// Use ATL smart pointers
CComPtr<ISldWorks> swApp;
if(swApp.CoCreateInstance(__uuidof(SldWorks),
NULL, CLSCTX_LOCAL_SERVER) != S_OK) {
return(0);
}
swApp->put_UserControl(VARIANT_TRUE);
swApp->put_Visible(VARIANT_TRUE);
CComPtr<IModelDoc2> swModel;
swApp->get_IActiveDoc2(&swModel);
if (! swModel) {
return(0);
}
CComBSTR strModelTitle;
long nDocumentType;
// swDocumentTypes_e
swModel->GetTitle(&strModelTitle);
swModel->GetType(&nDocumentType);
VARIANT vConfigurationNames;
// This is an out-of-process client so you
cannot use ModelDoc2::IGetConfigurationNames
swModel->GetConfigurationNames(&vConfigurationNames);
SafeBSTRArray saConfigurationNames(vConfigurationNames);
for (int i = 0; i < saConfigurationNames.getSize();
i++) {
CComBSTR bstrConfigurationName(saConfigurationNames[i]);
}
} // ATL smart pointers are destructed,
so all COM objects you held on to are released
//
Now you can safely shut down COM as you do not need it any longer
// Stop COM
CoUninitialize();
return(0);
}